RelationshipPluginBase.php 5.05 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\relationship\RelationshipPluginBase.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\relationship;
9

10
use Drupal\views\ViewExecutable;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\Plugin\views\HandlerBase;
13
use Drupal\views\Join;
14
use Drupal\views\Views;
15

merlinofchaos's avatar
merlinofchaos committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/**
 * @defgroup views_relationship_handlers Views relationship handlers
 * @{
 * Handlers to tell Views how to create alternate relationships.
 */

/**
 * Simple relationship handler that allows a new version of the primary table
 * to be linked in.
 *
 * The base relationship handler can only handle a single join. Some relationships
 * are more complex and might require chains of joins; for those, you must
 * utilize a custom relationship handler.
 *
 * Definition items:
 * - base: The new base table this relationship will be adding. This does not
 *   have to be a declared base table, but if there are no tables that
 *   utilize this base table, it won't be very effective.
 * - base field: The field to use in the relationship; if left out this will be
 *   assumed to be the primary field.
 * - relationship table: The actual table this relationship operates against.
 *   This is analogous to using a 'table' override.
 * - relationship field: The actual field this relationship operates against.
 *   This is analogous to using a 'real field' override.
 * - label: The default label to provide for this relationship, which is
 *   shown in parentheses next to any field/sort/filter/argument that uses
 *   the relationship.
 *
 * @ingroup views_relationship_handlers
 */
46
abstract class RelationshipPluginBase extends HandlerBase {
dawehner's avatar
dawehner committed
47

merlinofchaos's avatar
merlinofchaos committed
48
  /**
49 50
   * Overrides \Drupal\views\Plugin\views\HandlerBase::init().
   *
merlinofchaos's avatar
merlinofchaos committed
51 52 53
   * Init handler to let relationships live on tables other than
   * the table they operate on.
   */
54 55 56
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

merlinofchaos's avatar
merlinofchaos committed
57 58 59 60
    if (isset($this->definition['relationship table'])) {
      $this->table = $this->definition['relationship table'];
    }
    if (isset($this->definition['relationship field'])) {
61 62 63
      // Set both realField and field so custom handler can rely on the old
      // field value.
      $this->realField = $this->field = $this->definition['relationship field'];
merlinofchaos's avatar
merlinofchaos committed
64 65 66
    }
  }

67 68 69 70 71 72 73 74 75 76
  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
77 78
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
79 80 81

    // Relationships definitions should define a default label, but if they aren't get another default value.
    if (!empty($this->definition['label'])) {
82 83 84
      // Cast the label to a string since it is an object.
      // @see \Drupal\Core\StringTranslation\TranslationWrapper
      $label = (string) $this->definition['label'];
merlinofchaos's avatar
merlinofchaos committed
85 86 87 88 89
    }
    else {
      $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
    }

90
    $options['admin_label']['default'] = $label;
merlinofchaos's avatar
merlinofchaos committed
91 92 93 94 95 96
    $options['required'] = array('default' => FALSE, 'bool' => TRUE);

    return $options;
  }

  /**
97
   * {@inheritdoc}
merlinofchaos's avatar
merlinofchaos committed
98
   */
99 100
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
101 102 103

    unset($form['admin_label']['#fieldset']);
    $form['admin_label']['#weight'] = -1;
merlinofchaos's avatar
merlinofchaos committed
104 105 106 107 108 109 110 111 112 113

    $form['required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Require this relationship'),
      '#description' => t('Enable to hide items that do not contain this relationship'),
      '#default_value' => !empty($this->options['required']),
    );
  }

  /**
114
   * {@inheritdoc}
merlinofchaos's avatar
merlinofchaos committed
115
   */
116
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
117
    // Figure out what base table this relationship brings to the party.
118
    $table_data = Views::viewsData()->get($this->definition['base']);
merlinofchaos's avatar
merlinofchaos committed
119 120
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];

121
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
122 123 124 125

    $def = $this->definition;
    $def['table'] = $this->definition['base'];
    $def['field'] = $base_field;
126 127
    $def['left_table'] = $this->tableAlias;
    $def['left_field'] = $this->realField;
128
    $def['adjusted'] = TRUE;
merlinofchaos's avatar
merlinofchaos committed
129 130 131 132 133 134 135 136
    if (!empty($this->options['required'])) {
      $def['type'] = 'INNER';
    }

    if (!empty($this->definition['extra'])) {
      $def['extra'] = $this->definition['extra'];
    }

137 138
    if (!empty($def['join_id'])) {
      $id = $def['join_id'];
merlinofchaos's avatar
merlinofchaos committed
139 140
    }
    else {
141
      $id = 'standard';
merlinofchaos's avatar
merlinofchaos committed
142
    }
143
    $join = Views::pluginManager('join')->createInstance($id, $def);
merlinofchaos's avatar
merlinofchaos committed
144 145 146 147

    // use a short alias for this:
    $alias = $def['table'] . '_' . $this->table;

148
    $this->alias = $this->query->addRelationship($alias, $join, $this->definition['base'], $this->relationship);
merlinofchaos's avatar
merlinofchaos committed
149 150 151 152

    // Add access tags if the base table provide it.
    if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
      $access_tag = $table_data['table']['base']['access query tag'];
153
      $this->query->addTag($access_tag);
merlinofchaos's avatar
merlinofchaos committed
154 155 156 157 158 159 160 161
    }
  }

}

/**
 * @}
 */