You are here

protected function ViewsEntitySchemaSubscriber::dataTableAddition in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber::dataTableAddition()
  2. 9 core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber::dataTableAddition()

Updates views if a data table is added.

Parameters

\Drupal\views\Entity\View[] $all_views: All views.

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type.

string $new_data_table: The new data table.

string $base_table: The base table.

File

core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php, line 358

Class

ViewsEntitySchemaSubscriber
Reacts to changes on entity types to update all views entities.

Namespace

Drupal\views\EventSubscriber

Code

protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) {

  /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
  $entity_type_id = $entity_type
    ->id();
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $storage
    ->setEntityType($entity_type);
  $table_mapping = $storage
    ->getTableMapping();
  $data_table_fields = $table_mapping
    ->getFieldNames($new_data_table);
  $base_table_fields = $table_mapping
    ->getFieldNames($base_table);
  $data_table = $new_data_table;
  $this
    ->processHandlers($all_views, function (&$handler_config, ViewEntityInterface $view) use ($entity_type_id, $base_table, $data_table, $base_table_fields, $data_table_fields) {
    if (isset($handler_config['entity_type']) && isset($handler_config['entity_field']) && $handler_config['entity_type'] == $entity_type_id) {

      // Move all fields which just exists on the data table.
      if ($handler_config['table'] == $base_table && in_array($handler_config['entity_field'], $data_table_fields) && !in_array($handler_config['entity_field'], $base_table_fields)) {
        $handler_config['table'] = $data_table;
        $this->viewsToSave[$view
          ->id()] = $view;
      }
    }
  });
}