You are here

public static function ViewsFormMainForm::preRenderViewsForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Form/ViewsFormMainForm.php \Drupal\views\Form\ViewsFormMainForm::preRenderViewsForm()

Replaces views substitution placeholders.

Parameters

array $element: An associative array containing the properties of the element. Properties used: #substitutions, #children.

Return value

array The $element with prepared variables ready for #theme 'form' in views_form_views_form.

1 call to ViewsFormMainForm::preRenderViewsForm()
ViewsHooksTest::testViewsFormMainFormPreRender in core/modules/views/tests/src/Kernel/ViewsHooksTest.php
Tests how hook_views_form_substitutions() makes substitutions.

File

core/modules/views/src/Form/ViewsFormMainForm.php, line 35

Class

ViewsFormMainForm

Namespace

Drupal\views\Form

Code

public static function preRenderViewsForm(array $element) {

  // Placeholders and their substitutions (usually rendered form elements).
  $search = [];
  $replace = [];

  // Add in substitutions provided by the form.
  foreach ($element['#substitutions']['#value'] as $substitution) {
    $field_name = $substitution['field_name'];
    $row_id = $substitution['row_id'];
    $search[] = $substitution['placeholder'];
    $replace[] = isset($element[$field_name][$row_id]) ? \Drupal::service('renderer')
      ->render($element[$field_name][$row_id]) : '';
  }

  // Add in substitutions from hook_views_form_substitutions().
  $substitutions = \Drupal::moduleHandler()
    ->invokeAll('views_form_substitutions');
  foreach ($substitutions as $placeholder => $substitution) {
    $search[] = Html::escape($placeholder);

    // Ensure that any replacements made are safe to make.
    if (!$substitution instanceof MarkupInterface) {
      $substitution = Html::escape($substitution);
    }
    $replace[] = $substitution;
  }

  // Apply substitutions to the rendered output.
  $output = str_replace($search, $replace, \Drupal::service('renderer')
    ->render($element['output']));
  $element['output'] = [
    '#markup' => ViewsRenderPipelineMarkup::create($output),
  ];
  return $element;
}