RelationshipPluginBase.php in Views (for Drupal 7) 8.3
File
lib/Drupal/views/Plugin/views/relationship/RelationshipPluginBase.php
View source
<?php
namespace Drupal\views\Plugin\views\relationship;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\views\Join;
use Drupal\Core\Annotation\Plugin;
abstract class RelationshipPluginBase extends HandlerBase {
public function init(ViewExecutable $view, &$options) {
$this
->setOptionDefaults($this->options, $this
->defineOptions());
parent::init($view, $options);
if (isset($this->definition['relationship table'])) {
$this->table = $this->definition['relationship table'];
}
if (isset($this->definition['relationship field'])) {
$this->realField = $this->field = $this->definition['relationship field'];
}
}
function label() {
if (!isset($this->options['label'])) {
return $this
->adminLabel();
}
return $this->options['label'];
}
protected function defineOptions() {
$options = parent::defineOptions();
if (!empty($this->definition['label'])) {
$label = $this->definition['label'];
}
else {
$label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
}
$options['label'] = array(
'default' => $label,
'translatable' => TRUE,
);
$options['required'] = array(
'default' => FALSE,
'bool' => TRUE,
);
return $options;
}
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Identifier'),
'#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
'#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
'#required' => TRUE,
);
$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']),
);
}
public function query() {
$table_data = views_fetch_data($this->definition['base']);
$base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
$this
->ensureMyTable();
$def = $this->definition;
$def['table'] = $this->definition['base'];
$def['field'] = $base_field;
$def['left_table'] = $this->tableAlias;
$def['left_field'] = $this->realField;
$def['adjusted'] = TRUE;
if (!empty($this->options['required'])) {
$def['type'] = 'INNER';
}
if (!empty($this->definition['extra'])) {
$def['extra'] = $this->definition['extra'];
}
if (!empty($def['join_id'])) {
$id = $def['join_id'];
}
else {
$id = 'standard';
}
$join = drupal_container()
->get('plugin.manager.views.join')
->createInstance($id, $def);
$alias = $def['table'] . '_' . $this->table;
$this->alias = $this->query
->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
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'];
$this->query
->add_tag($access_tag);
}
}
public function usesGroupBy() {
return FALSE;
}
}
Classes
Name |
Description |
RelationshipPluginBase |
Simple relationship handler that allows a new version of the primary table
to be linked in. |