You are here

public static function RenderExampleController::preRenderAddSuffix in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/render_example/src/Controller/RenderExampleController.php \Drupal\render_example\Controller\RenderExampleController::preRenderAddSuffix()

Example '#pre_render' function.

Pre render callbacks are triggered prior to rendering an element to HTML and are given the chance to manipulate the renderable array. Any changes they make will be reflected in the final rendered HTML.

We need to wrap suffix in a Markup object. Otherwise, style attribute will be removed by Xss

This function is used as a post render callback in \Drupal\render_example\Controller\RenderExampleController::arrays().

Parameters

array $element: The element which will be rendered.

Return value

array The altered element. In this case we add a #prefix to it.

See also

\Drupal\Component\Utility\Xss::filter()

\Drupal\render_example\Controller\RenderExampleController::arrays()

File

render_example/src/Controller/RenderExampleController.php, line 546

Class

RenderExampleController
Provides module description page and examples of building render arrays.

Namespace

Drupal\render_example\Controller

Code

public static function preRenderAddSuffix(array $element) {
  $element['#suffix'] = Markup::create('<div style="color:red">' . t('This #suffix was added by a #pre_render callback.') . '</div>');
  return $element;
}