You are here

class FieldOrLanguageJoin in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/join/FieldOrLanguageJoin.php \Drupal\views\Plugin\views\join\FieldOrLanguageJoin

Implementation for the "field OR language" join.

If the extra conditions contain either ".langcode" or ".bundle", they will be grouped and joined with OR instead of AND. The entire group will then be joined to the other conditions with AND.

This is needed for configurable fields that are translatable on some bundles and untranslatable on others. The correct field values to fetch in this case have a langcode that matches the entity record *or* have a bundle on which the field is untranslatable. Thus, the entity base table (or data table, or revision data table, respectively) must join the field data table (or field revision table) on a matching langcode *or* a bundle where the field is untranslatable. The following example views data achieves this for a node field named 'field_tags' which is translatable on an 'article' node type, but not on the 'news' and 'page' node types:

$data['node__field_tags']['table']['join']['node_field_data'] = [
  'join_id' => 'field_or_language_join',
  'table' => 'node__field_tags',
  'left_field' => 'nid',
  'field' => 'entity_id',
  'extra' => [
    [
      'field' => 'deleted',
      'value' => 0,
      'numeric' => TRUE,
    ],
    [
      'left_field' => 'langcode',
      'field' => 'langcode',
    ],
    [
      'field' => 'bundle',
      'value' => [
        'news',
        'page',
      ],
    ],
  ],
];

The resulting join condition for this example would be the following:


  ON node__field_tags.deleted = 0
    AND (
      node_field_data.langcode = node__field_tags.langcode
      OR node__field.tags.bundle IN ['news', 'page']
    )

Plugin annotation

@ViewsJoin("field_or_language_join");

Hierarchy

Expanded class hierarchy of FieldOrLanguageJoin

See also

views_field_default_views_data()

Related topics

1 file declares its use of FieldOrLanguageJoin
FieldOrLanguageJoinTest.php in core/modules/views/tests/src/Kernel/Plugin/FieldOrLanguageJoinTest.php

File

core/modules/views/src/Plugin/views/join/FieldOrLanguageJoin.php, line 64

Namespace

Drupal\views\Plugin\views\join
View source
class FieldOrLanguageJoin extends JoinPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function joinAddExtra(&$arguments, &$condition, $table, SelectInterface $select_query, $left_table = NULL) {
    if (empty($this->extra)) {
      return;
    }
    if (is_array($this->extra)) {
      $extras = [];
      foreach ($this->extra as $extra) {
        $extras[] = $this
          ->buildExtra($extra, $arguments, $table, $select_query, $left_table);
      }

      // Remove and store the langcode OR bundle join condition extra.
      $language_bundle_conditions = [];
      foreach ($extras as $key => $extra) {
        if (strpos($extra, '.langcode') !== FALSE || strpos($extra, '.bundle') !== FALSE) {
          $language_bundle_conditions[] = $extra;
          unset($extras[$key]);
        }
      }
      if (count($extras) > 1) {
        $condition .= ' AND (' . implode(' ' . $this->extraOperator . ' ', $extras) . ')';
      }
      elseif ($extras) {
        $condition .= ' AND ' . array_shift($extras);
      }

      // Tack on the langcode OR bundle join condition extra.
      if (!empty($language_bundle_conditions)) {
        $condition .= ' AND (' . implode(' OR ', $language_bundle_conditions) . ')';
      }
    }
    elseif (is_string($this->extra)) {
      $condition .= " AND ({$this->extra})";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FieldOrLanguageJoin::joinAddExtra protected function Adds the extras to the join condition. Overrides JoinPluginBase::joinAddExtra
JoinPluginBase::$adjusted public property Defines whether a join has been adjusted.
JoinPluginBase::$configuration public property The configuration array passed by initJoin. Overrides PluginBase::$configuration
JoinPluginBase::$extra public property An array of extra conditions on the join.
JoinPluginBase::$extraOperator public property How all the extras will be combined. Either AND or OR.
JoinPluginBase::$field public property The field to join on (right field).
JoinPluginBase::$leftField public property The field we join to.
JoinPluginBase::$leftFormula public property A formula to be used instead of the left field.
JoinPluginBase::$leftTable public property The table we join to.
JoinPluginBase::$table public property The table to join (right table).
JoinPluginBase::$type public property The join type, so for example LEFT (default) or INNER.
JoinPluginBase::buildExtra protected function Builds a single extra condition.
JoinPluginBase::buildJoin public function Builds the SQL for the join this object represents. Overrides JoinPluginInterface::buildJoin 2
JoinPluginBase::__construct public function Constructs a Drupal\views\Plugin\views\join\JoinPluginBase object. Overrides PluginBase::__construct 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.