Views join handler plugins

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/join/JoinPluginBase.php \Drupal\views\Plugin\views\join\views_join_handlers
  2. 10 core/modules/views/src/Plugin/views/join/JoinPluginBase.php \Drupal\views\Plugin\views\join\views_join_handlers
  3. 11.x core/modules/views/src/Plugin/views/join/JoinPluginBase.php \Drupal\views\Plugin\views\join\views_join_handlers

Handler plugins for Views table joins.

Handler plugins help build the view query object. Join handler plugins handle table joins.

Views join handlers extend \Drupal\views\Plugin\views\join\JoinPluginBase. They must be annotated with \Drupal\views\Annotation\ViewsJoin annotation, and they must be in namespace directory Plugin\views\join.

Here are some examples of configuration for the join plugins.

For this SQL:


LEFT JOIN {two} ON one.field_a = two.field_b

Use this configuration:

$configuration = array(
    'table' => 'two',
    'field' => 'field_b',
    'left_table' => 'one',
    'left_field' => 'field_a',
    'operator' => '=',
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

Note that the default join type is a LEFT join when 'type' is not supplied in the join plugin configuration.

If an SQL expression is needed for the first part of the left table join condition, 'left_formula' can be used instead of 'left_field'. For this SQL:


LEFT JOIN {two} ON MAX(one.field_a) = two.field_b AND one.field_c = 'some_val'

Use this configuration:

$configuration = array(
    'table' => 'two',
    'field' => 'field_b',
    'left_table' => 'one',
    'left_formula' => 'MAX(one.field_a)',
    'operator' => '=',
    'extra' => array(
        0 => array(
            'left_field' => 'field_c',
            'value' => 'some_val',
        ),
    ),
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

For this SQL:


INNER JOIN {two} ON one.field_a = two.field_b AND one.field_c = 'some_val'

Use this configuration:

$configuration = array(
    'type' => 'INNER',
    'table' => 'two',
    'field' => 'field_b',
    'left_table' => 'one',
    'left_field' => 'field_a',
    'operator' => '=',
    'extra' => array(
        0 => array(
            'left_field' => 'field_c',
            'value' => 'some_val',
        ),
    ),
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

For this SQL:


INNER JOIN {two} ON one.field_a = two.field_b AND two.field_d = 'other_val'

Use this configuration:

$configuration = array(
    'type' => 'INNER',
    'table' => 'two',
    'field' => 'field_b',
    'left_table' => 'one',
    'left_field' => 'field_a',
    'operator' => '=',
    'extra' => array(
        0 => array(
            'field' => 'field_d',
            'value' => 'other_val',
        ),
    ),
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

For this SQL:


INNER JOIN {two} ON one.field_a = two.field_b AND one.field_c = two.field_d

Use this configuration:

$configuration = array(
    'type' => 'INNER',
    'table' => 'two',
    'field' => 'field_b',
    'left_table' => 'one',
    'left_field' => 'field_a',
    'operator' => '=',
    'extra' => array(
        0 => array(
            'left_field' => 'field_c',
            'field' => 'field_d',
        ),
    ),
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);

Here is an example of a more complex join:

class JoinComplex extends JoinPluginBase {
    public function buildJoin($select_query, $table, $view_query) {
        // Add an additional hardcoded condition to the query.
        $this->extra = 'foo.bar = baz.boing';
        parent::buildJoin($select_query, $table, $view_query);
    }

}

See also

Plugin API

Parent topics

File

core/modules/views/src/Plugin/views/join/JoinPluginBase.php, line 8

Classes

Title Sort descending File name Summary
FieldOrLanguageJoin core/modules/views/src/Plugin/views/join/FieldOrLanguageJoin.php Implementation for the "field OR language" join.
JoinPluginBase core/modules/views/src/Plugin/views/join/JoinPluginBase.php Represents a join and creates the SQL necessary to implement the join.
Standard core/modules/views/src/Plugin/views/join/Standard.php Default implementation of the join plugin.
Subquery core/modules/views/src/Plugin/views/join/Subquery.php Join handler for relationships that join with a subquery as the left field.
ViewsJoin core/modules/views/src/Annotation/ViewsJoin.php Defines a Plugin annotation object for views join plugins.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.