views_handler_filter_entity_bundle.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  21. * {@inheritdoc}
  22. */
  23. public function init(&$view, &$options) {
  24. parent::init($view, $options);
  25. $this->get_entity_type();
  26. }
  27. /**
  28. * Set and returns the entity_type.
  29. *
  30. * @return string
  31. * The entity type on the filter.
  32. */
  33. public function get_entity_type() {
  34. if (!isset($this->entity_type)) {
  35. $data = views_fetch_data($this->table);
  36. if (isset($data['table']['entity type'])) {
  37. $this->entity_type = $data['table']['entity type'];
  38. }
  39. // If the current filter is under a relationship you can't be sure that
  40. // the entity type of the view is the entity type of the current filter
  41. // For example a filter from a node author on a node view does have users
  42. // as entity type.
  43. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  44. $relationships = $this->view->display_handler->get_option('relationships');
  45. if (!empty($relationships[$this->options['relationship']])) {
  46. $options = $relationships[$this->options['relationship']];
  47. $data = views_fetch_data($options['table']);
  48. $this->entity_type = $data['table']['entity type'];
  49. }
  50. }
  51. }
  52. return $this->entity_type;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function get_value_options() {
  58. if (!isset($this->value_options)) {
  59. $info = entity_get_info($this->entity_type);
  60. $types = $info['bundles'];
  61. $this->value_title = t('@entity types', array('@entity' => $info['label']));
  62. $options = array();
  63. foreach ($types as $type => $info) {
  64. $options[$type] = t($info['label']);
  65. }
  66. asort($options);
  67. $this->value_options = $options;
  68. }
  69. }
  70. /**
  71. * All entity types beside comment and taxonomy terms have a proper implement
  72. * bundle, though these two need an additional join to node/vocab table
  73. * to work as required.
  74. */
  75. public function query() {
  76. $this->ensure_my_table();
  77. // Adjust the join for the comment case.
  78. if ($this->entity_type == 'comment') {
  79. $join = new views_join();
  80. $def = array(
  81. 'table' => 'node',
  82. 'field' => 'nid',
  83. 'left_table' => $this->table_alias,
  84. 'left_field' => 'nid',
  85. );
  86. $join->definition = $def;
  87. $join->construct();
  88. $join->adjusted = TRUE;
  89. $this->table_alias = $this->query->add_table('node', $this->relationship, $join);
  90. $this->real_field = 'type';
  91. // Replace the value to match the node type column.
  92. foreach ($this->value as &$value) {
  93. $value = str_replace('comment_node_', '', $value);
  94. }
  95. }
  96. elseif ($this->entity_type == 'taxonomy_term') {
  97. $join = new views_join();
  98. $def = array(
  99. 'table' => 'taxonomy_vocabulary',
  100. 'field' => 'vid',
  101. 'left_table' => $this->table_alias,
  102. 'left_field' => 'vid',
  103. );
  104. $join->definition = $def;
  105. $join->construct();
  106. $join->adjusted = TRUE;
  107. $this->table_alias = $this->query->add_table('taxonomy_vocabulary', $this->relationship, $join);
  108. $this->real_field = 'machine_name';
  109. }
  110. else {
  111. $entity_info = entity_get_info($this->entity_type);
  112. $this->real_field = $entity_info['bundle keys']['bundle'];
  113. }
  114. parent::query();
  115. }
  116. }