You are here

public static function EntityBrowser::postRender in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/display/EntityBrowser.php \Drupal\entity_browser\Plugin\views\display\EntityBrowser::postRender()

Post render callback that moves form elements into the view.

Form elements need to be added out of view to be correctly detected by Form API and then added into the view afterwards. Views use the same approach for bulk operations.

Parameters

string $content: Rendered content.

array $element: Render array.

Return value

string Rendered content.

File

src/Plugin/views/display/EntityBrowser.php, line 193

Class

EntityBrowser
The plugin that handles entity browser display.

Namespace

Drupal\entity_browser\Plugin\views\display

Code

public static function postRender($content, 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[] = $placeholder;
    $replace[] = $substitution;
  }

  // We cannot render exposed form within the View, as nested forms are not
  // standard and will break entity selection.
  $search[] = '<form';
  $replace[] = '<div';
  $search[] = '</form>';
  $replace[] = '</div>';
  $content = str_replace($search, $replace, $content);
  return $content;
}