function ViewsConfigUpdater::processRevisionFieldHyphenFix

Replaces hyphen on historical data (revision) fields.

This replaces hyphens with double underscores in twig assertions.

Parameters

\Drupal\views\ViewEntityInterface $view: The view entity.

Return value

bool Whether the handler was updated.

See also

https://www.drupal.org/project/drupal/issues/2831233

2 calls to ViewsConfigUpdater::processRevisionFieldHyphenFix()
ViewsConfigUpdater::needsRevisionFieldHyphenFix in core/modules/views/src/ViewsConfigUpdater.php
Checks each display in a view to see if it needs the hyphen fix.
ViewsConfigUpdater::updateAll in core/modules/views/src/ViewsConfigUpdater.php
Performs all required updates.

File

core/modules/views/src/ViewsConfigUpdater.php, line 397

Class

ViewsConfigUpdater
Provides a BC layer for modules providing old configurations.

Namespace

Drupal\views

Code

public function processRevisionFieldHyphenFix(ViewEntityInterface $view) : bool {
  // Regex to search only for token with machine name '-revision_id'.
  $old_part = '/{{([^}]+)(-revision_id)/';
  $new_part = '{{$1__revision_id';
  $old_field = '-revision_id';
  $new_field = '__revision_id';
  /** @var \Drupal\views\ViewEntityInterface $view */
  $is_update = FALSE;
  $displays = $view->get('display');
  foreach ($displays as &$display) {
    if (isset($display['display_options']['fields'])) {
      foreach ($display['display_options']['fields'] as $field_name => $field) {
        if (!empty($field['alter']['text'])) {
          // Fixes replacement token references in rewritten fields.
          $alter_text = $field['alter']['text'];
          if (preg_match($old_part, $alter_text) === 1) {
            $is_update = TRUE;
            $field['alter']['text'] = preg_replace($old_part, $new_part, $alter_text);
          }
        }
        if (!empty($field['alter']['path'])) {
          // Fixes replacement token references in link paths.
          $alter_path = $field['alter']['path'];
          if (preg_match($old_part, $alter_path) === 1) {
            $is_update = TRUE;
            $field['alter']['path'] = preg_replace($old_part, $new_part, $alter_path);
          }
        }
        if (str_contains($field_name, $old_field)) {
          // Replaces the field name and the view id.
          $is_update = TRUE;
          $field['id'] = str_replace($old_field, $new_field, $field['id']);
          $field['field'] = str_replace($old_field, $new_field, $field['field']);
          // Replace key with save order.
          $field_name_update = str_replace($old_field, $new_field, $field_name);
          $fields = $display['display_options']['fields'];
          $keys = array_keys($fields);
          $keys[array_search($field_name, $keys)] = $field_name_update;
          $display['display_options']['fields'] = array_combine($keys, $fields);
          $display['display_options']['fields'][$field_name_update] = $field;
        }
      }
    }
  }
  if ($is_update) {
    $view->set('display', $displays);
  }
  return $is_update;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.