You are here

public function DisplayPluginBase::elementPreRender in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/display/DisplayPluginBase.php \Drupal\views\Plugin\views\display\DisplayPluginBase::elementPreRender()

#pre_render callback for view display rendering.

Parameters

array $element: The element to #pre_render

Return value

array The processed element.

Overrides DisplayPluginInterface::elementPreRender

See also

self::render()

File

core/modules/views/src/Plugin/views/display/DisplayPluginBase.php, line 2218

Class

DisplayPluginBase
Base class for views display plugins.

Namespace

Drupal\views\Plugin\views\display

Code

public function elementPreRender(array $element) {
  $view = $element['#view'];
  $empty = empty($view->result);

  // Force a render array so CSS/JS can be attached.
  if (!is_array($element['#rows'])) {
    $element['#rows'] = [
      '#markup' => $element['#rows'],
    ];
  }
  $element['#header'] = $view->display_handler
    ->renderArea('header', $empty);
  $element['#footer'] = $view->display_handler
    ->renderArea('footer', $empty);
  $element['#empty'] = $empty ? $view->display_handler
    ->renderArea('empty', $empty) : [];
  $element['#exposed'] = !empty($view->exposed_widgets) ? $view->exposed_widgets : [];
  $element['#more'] = $view->display_handler
    ->renderMoreLink();
  $element['#feed_icons'] = !empty($view->feedIcons) ? $view->feedIcons : [];
  if ($view->display_handler
    ->renderPager()) {
    $exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
    $element['#pager'] = $view
      ->renderPager($exposed_input);
  }
  if (!empty($view->attachment_before)) {
    $element['#attachment_before'] = $view->attachment_before;
  }
  if (!empty($view->attachment_after)) {
    $element['#attachment_after'] = $view->attachment_after;
  }

  // If form fields were found in the view, reformat the view output as a form.
  if ($view
    ->hasFormElements()) {

    // Only render row output if there are rows. Otherwise, render the empty
    // region.
    if (!empty($element['#rows'])) {
      $output = $element['#rows'];
    }
    else {
      $output = $element['#empty'];
    }
    $form_object = ViewsForm::create(\Drupal::getContainer(), $view->storage
      ->id(), $view->current_display, $view->args);
    $form = \Drupal::formBuilder()
      ->getForm($form_object, $view, $output);

    // The form is requesting that all non-essential views elements be hidden,
    // usually because the rendered step is not a view result.
    if ($form['show_view_elements']['#value'] == FALSE) {
      $element['#header'] = [];
      $element['#exposed'] = [];
      $element['#pager'] = [];
      $element['#footer'] = [];
      $element['#more'] = [];
      $element['#feed_icons'] = [];
    }
    $element['#rows'] = $form;
  }
  return $element;
}