views_handler_relationship_entity_reverse.inc

Definition of views_handler_relationship_entity_reverse.

File

modules/field/views_handler_relationship_entity_reverse.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_relationship_entity_reverse.
  5. */
  6. /**
  7. * A relationship handlers which reverse entity references.
  8. *
  9. * @ingroup views_relationship_handlers
  10. */
  11. class views_handler_relationship_entity_reverse extends views_handler_relationship {
  12. function init(&$view, &$options) {
  13. parent::init($view, $options);
  14. $this->field_info = field_info_field($this->definition['field_name']);
  15. }
  16. /**
  17. * Called to implement a relationship in a query.
  18. */
  19. function query() {
  20. $this->ensure_my_table();
  21. // First, relate our base table to the current base table to the
  22. // field, using the base table's id field to the field's column.
  23. $views_data = views_fetch_data($this->table);
  24. $left_field = $views_data['table']['base']['field'];
  25. $first = array(
  26. 'left_table' => $this->table_alias,
  27. 'left_field' => $left_field,
  28. 'table' => $this->definition['field table'],
  29. 'field' => $this->definition['field field'],
  30. );
  31. if (!empty($this->options['required'])) {
  32. $first['type'] = 'INNER';
  33. }
  34. if (!empty($this->definition['join_extra'])) {
  35. $first['extra'] = $this->definition['join_extra'];
  36. }
  37. if (!empty($this->definition['join_handler']) && class_exists($this->definition['join_handler'])) {
  38. $first_join = new $this->definition['join_handler'];
  39. }
  40. else {
  41. $first_join = new views_join();
  42. }
  43. $first_join->definition = $first;
  44. $first_join->construct();
  45. $first_join->adjusted = TRUE;
  46. $this->first_alias = $this->query->add_table($this->definition['field table'], $this->relationship, $first_join);
  47. // Second, relate the field table to the entity specified using
  48. // the entity id on the field table and the entity's id field.
  49. $second = array(
  50. 'left_table' => $this->first_alias,
  51. 'left_field' => 'entity_id',
  52. 'table' => $this->definition['base'],
  53. 'field' => $this->definition['base field'],
  54. );
  55. if (!empty($this->options['required'])) {
  56. $second['type'] = 'INNER';
  57. }
  58. if (!empty($this->definition['join_handler']) && class_exists($this->definition['join_handler'])) {
  59. $second_join = new $this->definition['join_handler'];
  60. }
  61. else {
  62. $second_join = new views_join();
  63. }
  64. $second_join->definition = $second;
  65. $second_join->construct();
  66. $second_join->adjusted = TRUE;
  67. // use a short alias for this:
  68. $alias = $this->definition['field_name'] . '_' . $this->table;
  69. $this->alias = $this->query->add_relationship($alias, $second_join, $this->definition['base'], $this->relationship);
  70. }
  71. }