Join Handler
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 attributed with \Drupal\views\Attribute\ViewsJoin attribute, and they must be in namespace directory Plugin\views\join.
Here are some examples of configuration for the join plugins.
For this SQL:
Use this configuration:
$configuration = [
'table' => 'two',
'field' => 'field_b',
'left_table' => 'one',
'left_field' => 'field_a',
'operator' => '=',
];
$join = \Drupal::service->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:
Use this configuration:
$configuration = [
'table' => 'two',
'field' => 'field_b',
'left_table' => 'one',
'left_formula' => 'MAX(one.field_a)',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_c',
'value' => 'some_val',
],
],
];
$join = \Drupal::service->createInstance('standard', $configuration);
For this SQL:
Use this configuration:
$configuration = [
'type' => 'INNER',
'table' => 'two',
'field' => 'field_b',
'left_table' => 'one',
'left_field' => 'field_a',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_c',
'value' => 'some_val',
],
],
];
$join = \Drupal::service->createInstance('standard', $configuration);
For this SQL:
Use this configuration:
$configuration = [
'type' => 'INNER',
'table' => 'two',
'field' => 'field_b',
'left_table' => 'one',
'left_field' => 'field_a',
'operator' => '=',
'extra' => [
0 => [
'field' => 'field_d',
'value' => 'other_val',
],
],
];
$join = \Drupal::service->createInstance('standard', $configuration);
For this SQL:
Use this configuration:
$configuration = [
'type' => 'INNER',
'table' => 'two',
'field' => 'field_b',
'left_table' => 'one',
'left_field' => 'field_a',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_c',
'field' => 'field_d',
],
],
];
$join = \Drupal::service->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);
}
}