568bcd2f31fda9649c582c0e01cf412152a7f035
[yaffs-website] / web / core / modules / views / src / Plugin / views / relationship / EntityReverse.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\relationship;
4
5 use Drupal\views\Plugin\ViewsHandlerManager;
6 use Drupal\views\Views;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * A relationship handlers which reverse entity references.
11  *
12  * @ingroup views_relationship_handlers
13  *
14  * @ViewsRelationship("entity_reverse")
15  */
16 class EntityReverse extends RelationshipPluginBase {
17
18   /**
19    * Constructs an EntityReverse object.
20    *
21    * @param \Drupal\views\Plugin\ViewsHandlerManager $join_manager
22    *   The views plugin join manager.
23    */
24   public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewsHandlerManager $join_manager) {
25     parent::__construct($configuration, $plugin_id, $plugin_definition);
26     $this->joinManager = $join_manager;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
33     return new static(
34       $configuration,
35       $plugin_id,
36       $plugin_definition,
37       $container->get('plugin.manager.views.join')
38     );
39   }
40
41   /**
42    * Called to implement a relationship in a query.
43    */
44   public function query() {
45     $this->ensureMyTable();
46     // First, relate our base table to the current base table to the
47     // field, using the base table's id field to the field's column.
48     $views_data = Views::viewsData()->get($this->table);
49     $left_field = $views_data['table']['base']['field'];
50
51     $first = [
52       'left_table' => $this->tableAlias,
53       'left_field' => $left_field,
54       'table' => $this->definition['field table'],
55       'field' => $this->definition['field field'],
56       'adjusted' => TRUE
57     ];
58     if (!empty($this->options['required'])) {
59       $first['type'] = 'INNER';
60     }
61
62     if (!empty($this->definition['join_extra'])) {
63       $first['extra'] = $this->definition['join_extra'];
64     }
65
66     if (!empty($def['join_id'])) {
67       $id = $def['join_id'];
68     }
69     else {
70       $id = 'standard';
71     }
72     $first_join = $this->joinManager->createInstance($id, $first);
73
74     $this->first_alias = $this->query->addTable($this->definition['field table'], $this->relationship, $first_join);
75
76     // Second, relate the field table to the entity specified using
77     // the entity id on the field table and the entity's id field.
78     $second = [
79       'left_table' => $this->first_alias,
80       'left_field' => 'entity_id',
81       'table' => $this->definition['base'],
82       'field' => $this->definition['base field'],
83       'adjusted' => TRUE
84     ];
85
86     if (!empty($this->options['required'])) {
87       $second['type'] = 'INNER';
88     }
89
90     if (!empty($def['join_id'])) {
91       $id = $def['join_id'];
92     }
93     else {
94       $id = 'standard';
95     }
96     $second_join = $this->joinManager->createInstance($id, $second);
97     $second_join->adjusted = TRUE;
98
99     // use a short alias for this:
100     $alias = $this->definition['field_name'] . '_' . $this->table;
101
102     $this->alias = $this->query->addRelationship($alias, $second_join, $this->definition['base'], $this->relationship);
103   }
104
105 }