Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / views.views.inc
index 0bd5f270057f80519186480d35c257f50e5516d9..633ce9b9da802408b922f3b16478b90f125438bf 100644 (file)
@@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
 use Drupal\Core\Render\Markup;
+use Drupal\field\Entity\FieldConfig;
 use Drupal\field\FieldConfigInterface;
 use Drupal\field\FieldStorageConfigInterface;
 use Drupal\system\ActionConfigEntityInterface;
@@ -170,7 +171,6 @@ function views_views_data() {
     }
   }
 
-
   // Field modules can implement hook_field_views_data() to override the default
   // behavior for adding fields.
   $module_handler = \Drupal::moduleHandler();
@@ -265,7 +265,7 @@ function views_entity_field_label($entity_type, $field_name) {
   // Sort the field labels by it most used label and return the most used one.
   // If the counts are equal, sort by the label to ensure the result is
   // deterministic.
-  uksort($label_counter, function($a, $b) use ($label_counter) {
+  uksort($label_counter, function ($a, $b) use ($label_counter) {
     if ($label_counter[$a] === $label_counter[$b]) {
       return strcmp($a, $b);
     }
@@ -352,6 +352,49 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
     ];
   }
 
+  // Determine if the fields are translatable.
+  $bundles_names = $field_storage->getBundles();
+  $translation_join_type = FALSE;
+  $fields = [];
+  $translatable_configs = [];
+  $untranslatable_configs = [];
+  $untranslatable_config_bundles = [];
+
+  foreach ($bundles_names as $bundle) {
+    $fields[$bundle] = FieldConfig::loadByName($entity_type->id(), $bundle, $field_name);
+  }
+  foreach ($fields as $bundle => $config_entity) {
+    if (!empty($config_entity)) {
+      if ($config_entity->isTranslatable()) {
+        $translatable_configs[$bundle] = $config_entity;
+      }
+      else {
+        $untranslatable_configs[$bundle] = $config_entity;
+      }
+    }
+    else {
+      // https://www.drupal.org/node/2451657#comment-11462881
+      \Drupal::logger('views')->error(
+        t('A non-existent config entity name returned by FieldStorageConfigInterface::getBundles(): field name: %field, bundle: %bundle',
+          ['%field' => $field_name, '%bundle' => $bundle]
+        ));
+    }
+  }
+
+  // If the field is translatable on all the bundles, there will be a join on
+  // the langcode.
+  if (!empty($translatable_configs) && empty($untranslatable_configs)) {
+    $translation_join_type = 'language';
+  }
+  // If the field is translatable only on certain bundles, there will be a join
+  // on langcode OR bundle name.
+  elseif (!empty($translatable_configs) && !empty($untranslatable_configs)) {
+    foreach ($untranslatable_configs as $config) {
+      $untranslatable_config_bundles[] = $config->getTargetBundle();
+    }
+    $translation_join_type = 'language_bundle';
+  }
+
   // Build the relationships between the field table and the entity tables.
   $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT]['alias'];
   if ($data_table) {
@@ -362,7 +405,6 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
       'field' => 'entity_id',
       'extra' => [
         ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
-        ['left_field' => 'langcode', 'field' => 'langcode'],
       ],
     ];
   }
@@ -378,6 +420,24 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
     ];
   }
 
+  if ($translation_join_type === 'language_bundle') {
+    $data[$table_alias]['table']['join'][$data_table]['join_id'] = 'field_or_language_join';
+    $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
+      'left_field' => 'langcode',
+      'field' => 'langcode',
+    ];
+    $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
+      'field' => 'bundle',
+      'value' => $untranslatable_config_bundles,
+    ];
+  }
+  elseif ($translation_join_type === 'language') {
+    $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
+      'left_field' => 'langcode',
+      'field' => 'langcode',
+    ];
+  }
+
   if ($supports_revisions) {
     $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
     if ($entity_revision_data_table) {
@@ -388,7 +448,6 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
         'field' => 'revision_id',
         'extra' => [
           ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
-          ['left_field' => 'langcode', 'field' => 'langcode'],
         ],
       ];
     }
@@ -403,6 +462,23 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
         ],
       ];
     }
+    if ($translation_join_type === 'language_bundle') {
+      $data[$table_alias]['table']['join'][$entity_revision_data_table]['join_id'] = 'field_or_language_join';
+      $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
+        'left_field' => 'langcode',
+        'field' => 'langcode',
+      ];
+      $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
+        'value' => $untranslatable_config_bundles,
+        'field' => 'bundle',
+      ];
+    }
+    elseif ($translation_join_type === 'language') {
+      $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
+        'left_field' => 'langcode',
+        'field' => 'langcode',
+      ];
+    }
   }
 
   $group_name = $entity_type->getLabel();