3 namespace Drupal\views\Plugin\views\relationship;
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;
12 * @defgroup views_relationship_handlers Views relationship handlers
14 * Plugins for handling views relationships.
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.
21 * @ingroup views_plugins
26 * Simple relationship handler that allows a new version of the primary table
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.
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
47 * @ingroup views_relationship_handlers
49 abstract class RelationshipPluginBase extends HandlerBase {
52 * The relationship alias.
59 * Overrides \Drupal\views\Plugin\views\HandlerBase::init().
61 * Init handler to let relationships live on tables other than
62 * the table they operate on.
64 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
65 parent::init($view, $display, $options);
67 if (isset($this->definition['relationship table'])) {
68 $this->table = $this->definition['relationship table'];
70 if (isset($this->definition['relationship field'])) {
71 // Set both realField and field so custom handler can rely on the old
73 $this->realField = $this->field = $this->definition['relationship field'];
80 public function usesGroupBy() {
87 protected function defineOptions() {
88 $options = parent::defineOptions();
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'];
98 $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
101 $options['admin_label']['default'] = $label;
102 $options['required'] = ['default' => FALSE];
110 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
111 parent::buildOptionsForm($form, $form_state);
113 unset($form['admin_label']['#fieldset']);
114 $form['admin_label']['#weight'] = -1;
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']),
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'];
132 $this->ensureMyTable();
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';
144 if (!empty($this->definition['extra'])) {
145 $def['extra'] = $this->definition['extra'];
148 if (!empty($def['join_id'])) {
149 $id = $def['join_id'];
154 $join = Views::pluginManager('join')->createInstance($id, $def);
156 // use a short alias for this:
157 $alias = $def['table'] . '_' . $this->table;
159 $this->alias = $this->query->addRelationship($alias, $join, $this->definition['base'], $this->relationship);
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);
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;