views_handler_filter_entity_bundle.inc

Definition of views_handler_filter_entity_bundle

File

handlers/views_handler_filter_entity_bundle.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_entity_bundle
  5. */
  6. /**
  7. * Filter class which allows to filter by certain bundles of an entity.
  8. *
  9. * This class provides workarounds for taxonomy and comment.
  10. *
  11. * @ingroup views_filter_handlers
  12. */
  13. class views_handler_filter_entity_bundle extends views_handler_filter_in_operator {
  14. /**
  15. * Stores the entity type on which the filter filters.
  16. *
  17. * @var string
  18. */
  19. public $entity_type;
  20. function init(&$view, &$options) {
  21. parent::init($view, $options);
  22. $this->get_entity_type();
  23. }
  24. /**
  25. * Set and returns the entity_type.
  26. *
  27. * @return string
  28. * The entity type on the filter.
  29. */
  30. function get_entity_type() {
  31. if (!isset($this->entity_type)) {
  32. $data = views_fetch_data($this->table);
  33. if (isset($data['table']['entity type'])) {
  34. $this->entity_type = $data['table']['entity type'];
  35. }
  36. // If the current filter is under a relationship you can't be sure that the
  37. // entity type of the view is the entity type of the current filter
  38. // For example a filter from a node author on a node view does have users as entity type.
  39. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  40. $relationships = $this->view->display_handler->get_option('relationships');
  41. if (!empty($relationships[$this->options['relationship']])) {
  42. $options = $relationships[$this->options['relationship']];
  43. $data = views_fetch_data($options['table']);
  44. $this->entity_type = $data['table']['entity type'];
  45. }
  46. }
  47. }
  48. return $this->entity_type;
  49. }
  50. function get_value_options() {
  51. if (!isset($this->value_options)) {
  52. $info = entity_get_info($this->entity_type);
  53. $types = $info['bundles'];
  54. $this->value_title = t('@entity types', array('@entity' => $info['label']));
  55. $options = array();
  56. foreach ($types as $type => $info) {
  57. $options[$type] = t($info['label']);
  58. }
  59. asort($options);
  60. $this->value_options = $options;
  61. }
  62. }
  63. /**
  64. * All entity types beside comment and taxonomy terms have a proper implement
  65. * bundle, though these two need an additional join to node/vocab table
  66. * to work as required.
  67. */
  68. function query() {
  69. $this->ensure_my_table();
  70. // Adjust the join for the comment case.
  71. if ($this->entity_type == 'comment') {
  72. $join = new views_join();
  73. $def = array(
  74. 'table' => 'node',
  75. 'field' => 'nid',
  76. 'left_table' => $this->table_alias,
  77. 'left_field' => 'nid',
  78. );
  79. $join->definition = $def;
  80. $join->construct();
  81. $join->adjusted = TRUE;
  82. $this->table_alias = $this->query->add_table('node', $this->relationship, $join);
  83. $this->real_field = 'type';
  84. // Replace the value to match the node type column.
  85. foreach ($this->value as &$value) {
  86. $value = str_replace('comment_node_', '', $value);
  87. }
  88. }
  89. elseif ($this->entity_type == 'taxonomy_term') {
  90. $join = new views_join();
  91. $def = array(
  92. 'table' => 'taxonomy_vocabulary',
  93. 'field' => 'vid',
  94. 'left_table' => $this->table_alias,
  95. 'left_field' => 'vid',
  96. );
  97. $join->definition = $def;
  98. $join->construct();
  99. $join->adjusted = TRUE;
  100. $this->table_alias = $this->query->add_table('taxonomy_vocabulary', $this->relationship, $join);
  101. $this->real_field = 'machine_name';
  102. }
  103. else {
  104. $entity_info = entity_get_info($this->entity_type);
  105. $this->real_field = $entity_info['bundle keys']['bundle'];
  106. }
  107. parent::query();
  108. }
  109. }