View source
<?php
namespace Drupal\views;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\Schema\ArrayElement;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\DefaultTableMapping;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewsConfigUpdater implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $entityFieldManager;
protected $typedConfigManager;
protected $viewsData;
protected $multivalueBaseFieldsUpdateTableInfo;
protected $deprecationsEnabled = TRUE;
protected $triggeredDeprecations = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, TypedConfigManagerInterface $typed_config_manager, ViewsData $views_data) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->typedConfigManager = $typed_config_manager;
$this->viewsData = $views_data;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('config.typed'), $container
->get('views.views_data'));
}
public function setDeprecationsEnabled($enabled) {
$this->deprecationsEnabled = $enabled;
}
public function updateAll(ViewEntityInterface $view) {
return $this
->processDisplayHandlers($view, FALSE, function (&$handler, $handler_type, $key, $display_id) use ($view) {
$changed = FALSE;
if ($this
->processEntityLinkUrlHandler($handler, $handler_type, $view)) {
$changed = TRUE;
}
if ($this
->processOperatorDefaultsHandler($handler, $handler_type, $view)) {
$changed = TRUE;
}
if ($this
->processMultivalueBaseFieldHandler($handler, $handler_type, $key, $display_id, $view)) {
$changed = TRUE;
}
if ($this
->processSortFieldIdentifierUpdateHandler($handler, $handler_type)) {
$changed = TRUE;
}
return $changed;
});
}
protected function processDisplayHandlers(ViewEntityInterface $view, $return_on_changed, callable $handler_processor) {
$changed = FALSE;
$displays = $view
->get('display');
$handler_types = [
'field',
'argument',
'sort',
'relationship',
'filter',
];
foreach ($displays as $display_id => &$display) {
foreach ($handler_types as $handler_type) {
$handler_type_plural = $handler_type . 's';
if (!empty($display['display_options'][$handler_type_plural])) {
foreach ($display['display_options'][$handler_type_plural] as $key => &$handler) {
if ($handler_processor($handler, $handler_type, $key, $display_id)) {
$changed = TRUE;
if ($return_on_changed) {
return $changed;
}
}
}
}
}
}
if ($changed) {
$view
->set('display', $displays);
}
return $changed;
}
public function needsEntityLinkUrlUpdate(ViewEntityInterface $view) {
return $this
->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type) use ($view) {
return $this
->processEntityLinkUrlHandler($handler, $handler_type, $view);
});
}
protected function processEntityLinkUrlHandler(array &$handler, $handler_type, ViewEntityInterface $view) {
$changed = FALSE;
if ($handler_type === 'field') {
if (isset($handler['plugin_id']) && $handler['plugin_id'] === 'entity_link') {
if (!isset($handler['output_url_as_text'])) {
$handler['output_url_as_text'] = FALSE;
$changed = TRUE;
}
if (!isset($handler['absolute'])) {
$handler['absolute'] = FALSE;
$changed = TRUE;
}
}
elseif (isset($handler['plugin_id']) && $handler['plugin_id'] === 'node_path') {
$handler['plugin_id'] = 'entity_link';
$handler['field'] = 'view_node';
$handler['output_url_as_text'] = TRUE;
$changed = TRUE;
}
}
$deprecations_triggered =& $this->triggeredDeprecations['2857891'][$view
->id()];
if ($this->deprecationsEnabled && $changed && !$deprecations_triggered) {
$deprecations_triggered = TRUE;
@trigger_error(sprintf('The entity link url update for the "%s" view is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Module-provided Views configuration should be updated to accommodate the changes described at https://www.drupal.org/node/2857891.', $view
->id()), E_USER_DEPRECATED);
}
return $changed;
}
public function needsOperatorDefaultsUpdate(ViewEntityInterface $view) {
return $this
->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type) use ($view) {
return $this
->processOperatorDefaultsHandler($handler, $handler_type, $view);
});
}
protected function processOperatorDefaultsHandler(array &$handler, $handler_type, ViewEntityInterface $view) {
$changed = FALSE;
if ($handler_type === 'filter') {
if (!isset($handler['expose']['operator_limit_selection'])) {
$handler['expose']['operator_limit_selection'] = FALSE;
$changed = TRUE;
}
if (!isset($handler['expose']['operator_list'])) {
$handler['expose']['operator_list'] = [];
$changed = TRUE;
}
}
$deprecations_triggered =& $this->triggeredDeprecations['2869168'][$view
->id()];
if ($this->deprecationsEnabled && $changed && !$deprecations_triggered) {
$deprecations_triggered = TRUE;
@trigger_error(sprintf('The operator defaults update for the "%s" view is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Module-provided Views configuration should be updated to accommodate the changes described at https://www.drupal.org/node/2869168.', $view
->id()), E_USER_DEPRECATED);
}
return $changed;
}
public function needsMultivalueBaseFieldUpdate(ViewEntityInterface $view) {
if ($this
->getMultivalueBaseFieldUpdateTableInfo()) {
return $this
->processDisplayHandlers($view, TRUE, function (&$handler, $handler_type, $key, $display_id) use ($view) {
return $this
->processMultivalueBaseFieldHandler($handler, $handler_type, $key, $display_id, $view);
});
}
return FALSE;
}
protected function getMultivalueBaseFieldUpdateTableInfo() {
$table_info =& $this->multivalueBaseFieldsUpdateTableInfo;
if (!isset($table_info)) {
$table_info = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type
->hasHandlerClass('views_data') && $entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$base_field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type_id);
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$table_mapping = $entity_storage
->getTableMapping($base_field_definitions);
if (!$table_mapping instanceof DefaultTableMapping) {
continue;
}
foreach ($base_field_definitions as $field_name => $base_field_definition) {
$base_field_storage_definition = $base_field_definition
->getFieldStorageDefinition();
if (!$base_field_storage_definition
->isMultiple() || $base_field_storage_definition
->hasCustomStorage()) {
continue;
}
$table_name = $table_mapping
->getFieldTableName($field_name);
$main_property_name = $base_field_storage_definition
->getMainPropertyName();
$table_info[$table_name][$field_name] = $table_mapping
->getFieldColumnName($base_field_storage_definition, $main_property_name);
}
}
}
}
return $table_info;
}
protected function processMultivalueBaseFieldHandler(array &$handler, $handler_type, $key, $display_id, ViewEntityInterface $view) {
$changed = FALSE;
$table_info = $this
->getMultivalueBaseFieldUpdateTableInfo();
if (!$table_info) {
return $changed;
}
if (isset($handler['table']) && isset($table_info[$handler['table']]) && isset($table_info[$handler['table']][$handler['field']])) {
$changed = TRUE;
$original_field_name = $handler['field'];
$handler['field'] = $table_info[$handler['table']][$original_field_name];
$handler['plugin_id'] = $this->viewsData
->get($handler['table'])[$table_info[$handler['table']][$original_field_name]][$handler_type]['id'];
$id = 'views.view.' . $view
->id();
$path_to_handler = "display.{$display_id}.display_options.{$handler_type}s.{$key}";
$view_config = $view
->toArray();
$keys = explode('.', $path_to_handler);
NestedArray::setValue($view_config, $keys, $handler);
$typed_view = $this->typedConfigManager
->createFromNameAndData($id, $view_config);
$typed_handler = $typed_view
->get($path_to_handler);
if ($handler_type === 'filter' && $typed_handler
->get('value') instanceof ArrayElement && is_string($handler['value'])) {
if ($handler['value'] === '') {
$handler['value'] = [];
}
else {
$handler['value'] = (array) $handler['value'];
}
$handler['operator'] = $this
->mapOperatorFromSingleToMultiple($handler['operator']);
}
foreach (array_keys($handler) as $handler_key) {
if (!isset($typed_handler
->getDataDefinition()['mapping'][$handler_key])) {
unset($handler[$handler_key]);
}
}
}
$deprecations_triggered =& $this->triggeredDeprecations['2900684'][$view
->id()];
if ($this->deprecationsEnabled && $changed && !$deprecations_triggered) {
$deprecations_triggered = TRUE;
@trigger_error(sprintf('The multivalue base field update for the "%s" view is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Module-provided Views configuration should be updated to accommodate the changes described at https://www.drupal.org/node/2900684.', $view
->id()), E_USER_DEPRECATED);
}
return $changed;
}
protected function mapOperatorFromSingleToMultiple($single_operator) {
switch ($single_operator) {
case '=':
return 'or';
case '!=':
return 'not';
default:
return $single_operator;
}
}
public function needsSortFieldIdentifierUpdate(ViewEntityInterface $view) : bool {
return $this
->processDisplayHandlers($view, TRUE, function (array &$handler, string $handler_type) : bool {
return $this
->processSortFieldIdentifierUpdateHandler($handler, $handler_type);
});
}
protected function processSortFieldIdentifierUpdateHandler(array &$handler, string $handler_type) : bool {
if ($handler_type === 'sort' && !isset($handler['expose']['field_identifier'])) {
$handler['expose']['field_identifier'] = $handler['id'];
return TRUE;
}
return FALSE;
}
}