function render_example_preprocess_block

Same name in other branches
  1. 8.x-1.x render_example/render_example.module \render_example_preprocess_block()
  2. 4.0.x modules/render_example/render_example.module \render_example_preprocess_block()

Implements hook_preprocess_block().

Related topics

File

modules/render_example/render_example.module, line 164

Code

function render_example_preprocess_block(&$variables) {
    // Only modify the 'altering' page.
    if (\Drupal::routeMatch()->getRouteName() !== 'render_example.altering') {
        return;
    }
    $config = \Drupal::config('render_example.settings');
    // This example shows how you can manipulate an existing renderable array. In
    // this case by adding #prefix and #suffix properties to the block in order to
    // wrap a <div> around it.
    if ($config->get('wrap_blocks')) {
        $variables['content']['#prefix'] = '<div class="block-prefix"><p>' . t('Prefixed') . '</p>';
        $variables['content']['#suffix'] = '<span class="block-suffix">' . t('Block suffix') . '</span></div>';
    }
    // Show the render array used to build each block if the Devel module is
    // installed and the feature is enabled.
    if (Drupal::moduleHandler()->moduleExists('devel') && $config->get('show_block')) {
        $variables['content']['block_render_array'] = [
            '#type' => 'markup',
            '#prefix' => '<h2>' . t('The block render array for @block_id.', [
                '@block_id' => $variables['plugin_id'],
            ]) . '</h2>',
            'dump' => \Drupal::service('devel.dumper')->exportAsRenderable($variables, $variables['plugin_id']),
        ];
    }
}