Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / relationship / RelationshipPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\relationship;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\Plugin\views\HandlerBase;
9 use Drupal\views\Views;
10
11 /**
12  * @defgroup views_relationship_handlers Views relationship handlers
13  * @{
14  * Plugins for handling views relationships.
15  *
16  * Relationship handlers extend
17  * \Drupal\views\Plugin\views\relationship\RelationshipPluginBase. They must
18  * be annotated with \Drupal\views\Annotation\ViewsRelationship annotation,
19  * and they must be in namespace directory Plugin\views\relationship.
20  *
21  * @ingroup views_plugins
22  * @see plugin_api
23  */
24
25 /**
26  * Simple relationship handler that allows a new version of the primary table
27  * to be linked in.
28  *
29  * The base relationship handler can only handle a single join. Some
30  * relationships are more complex and might require chains of joins; for those,
31  * you must use a custom relationship handler.
32  *
33  * Definition items:
34  * - base: The new base table this relationship will be adding. This does not
35  *   have to be a declared base table, but if there are no tables that
36  *   use this base table, it won't be very effective.
37  * - base field: The field to use in the relationship; if left out this will be
38  *   assumed to be the primary field.
39  * - relationship table: The actual table this relationship operates against.
40  *   This is analogous to using a 'table' override.
41  * - relationship field: The actual field this relationship operates against.
42  *   This is analogous to using a 'real field' override.
43  * - label: The default label to provide for this relationship, which is
44  *   shown in parentheses next to any field/sort/filter/argument that uses
45  *   the relationship.
46  *
47  * @ingroup views_relationship_handlers
48  */
49 abstract class RelationshipPluginBase extends HandlerBase {
50
51   /**
52    * The relationship alias.
53    *
54    * @var string
55    */
56   public $alias;
57
58   /**
59    * Overrides \Drupal\views\Plugin\views\HandlerBase::init().
60    *
61    * Init handler to let relationships live on tables other than
62    * the table they operate on.
63    */
64   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
65     parent::init($view, $display, $options);
66
67     if (isset($this->definition['relationship table'])) {
68       $this->table = $this->definition['relationship table'];
69     }
70     if (isset($this->definition['relationship field'])) {
71       // Set both realField and field so custom handler can rely on the old
72       // field value.
73       $this->realField = $this->field = $this->definition['relationship field'];
74     }
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function usesGroupBy() {
81     return FALSE;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   protected function defineOptions() {
88     $options = parent::defineOptions();
89
90     // Relationships definitions should define a default label, but if they
91     // aren't get another default value.
92     if (!empty($this->definition['label'])) {
93       // Cast the label to a string since it is an object.
94       // @see \Drupal\Core\StringTranslation\TranslatableMarkup
95       $label = (string) $this->definition['label'];
96     }
97     else {
98       $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
99     }
100
101     $options['admin_label']['default'] = $label;
102     $options['required'] = ['default' => FALSE];
103
104     return $options;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
111     parent::buildOptionsForm($form, $form_state);
112
113     unset($form['admin_label']['#fieldset']);
114     $form['admin_label']['#weight'] = -1;
115
116     $form['required'] = [
117       '#type' => 'checkbox',
118       '#title' => $this->t('Require this relationship'),
119       '#description' => $this->t('Enable to hide items that do not contain this relationship'),
120       '#default_value' => !empty($this->options['required']),
121     ];
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function query() {
128     // Figure out what base table this relationship brings to the party.
129     $table_data = Views::viewsData()->get($this->definition['base']);
130     $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
131
132     $this->ensureMyTable();
133
134     $def = $this->definition;
135     $def['table'] = $this->definition['base'];
136     $def['field'] = $base_field;
137     $def['left_table'] = $this->tableAlias;
138     $def['left_field'] = $this->realField;
139     $def['adjusted'] = TRUE;
140     if (!empty($this->options['required'])) {
141       $def['type'] = 'INNER';
142     }
143
144     if (!empty($this->definition['extra'])) {
145       $def['extra'] = $this->definition['extra'];
146     }
147
148     if (!empty($def['join_id'])) {
149       $id = $def['join_id'];
150     }
151     else {
152       $id = 'standard';
153     }
154     $join = Views::pluginManager('join')->createInstance($id, $def);
155
156     // use a short alias for this:
157     $alias = $def['table'] . '_' . $this->table;
158
159     $this->alias = $this->query->addRelationship($alias, $join, $this->definition['base'], $this->relationship);
160
161     // Add access tags if the base table provide it.
162     if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
163       $access_tag = $table_data['table']['base']['access query tag'];
164       $this->query->addTag($access_tag);
165     }
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function calculateDependencies() {
172     $dependencies = parent::calculateDependencies();
173     // Add the provider of the relationship's base table to the dependencies.
174     $table_data = $this->getViewsData()->get($this->definition['base']);
175     $dependencies['module'][] = $table_data['table']['provider'];
176     return $dependencies;
177   }
178
179 }
180
181 /**
182  * @}
183  */