3 namespace Drupal\views\Plugin\views\relationship;
5 use Drupal\views\Plugin\ViewsHandlerManager;
6 use Drupal\views\Views;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
10 * A relationship handlers which reverse entity references.
12 * @ingroup views_relationship_handlers
14 * @ViewsRelationship("entity_reverse")
16 class EntityReverse extends RelationshipPluginBase {
19 * Constructs an EntityReverse object.
21 * @param \Drupal\views\Plugin\ViewsHandlerManager $join_manager
22 * The views plugin join manager.
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;
32 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
37 $container->get('plugin.manager.views.join')
42 * Called to implement a relationship in a query.
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'];
52 'left_table' => $this->tableAlias,
53 'left_field' => $left_field,
54 'table' => $this->definition['field table'],
55 'field' => $this->definition['field field'],
58 if (!empty($this->options['required'])) {
59 $first['type'] = 'INNER';
62 if (!empty($this->definition['join_extra'])) {
63 $first['extra'] = $this->definition['join_extra'];
66 if (!empty($def['join_id'])) {
67 $id = $def['join_id'];
72 $first_join = $this->joinManager->createInstance($id, $first);
74 $this->first_alias = $this->query->addTable($this->definition['field table'], $this->relationship, $first_join);
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.
79 'left_table' => $this->first_alias,
80 'left_field' => 'entity_id',
81 'table' => $this->definition['base'],
82 'field' => $this->definition['base field'],
86 if (!empty($this->options['required'])) {
87 $second['type'] = 'INNER';
90 if (!empty($def['join_id'])) {
91 $id = $def['join_id'];
96 $second_join = $this->joinManager->createInstance($id, $second);
97 $second_join->adjusted = TRUE;
99 // use a short alias for this:
100 $alias = $this->definition['field_name'] . '_' . $this->table;
102 $this->alias = $this->query->addRelationship($alias, $second_join, $this->definition['base'], $this->relationship);