You are here

function example_views_post_render in Views Layout 8

Implementation of hook_views_post_render().

Another way to add content to skipped regions. This works only if the regions were not skipped using the 'Skip' setting, since in that case the regions won't exist at all in $output.

This method will work with any selected region, so could also be used to replace, or append values to, either populated or skipped regions.

File

./views_layout.api.php, line 64
Documentation for Views Layout API.

Code

function example_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {

  // The view that contains skipped regions.
  $view_id = 'view_name';
  $display_id = 'block_1';
  if ($view
    ->id() == $view_id && $view->current_display == $display_id) {

    // The skipped regions in this layout.
    $skipped_regions = [
      'three',
      'seven',
    ];

    // The render array that will be inserted.
    $renderArray = [
      '#type' => 'markup',
      '#markup' => 'Some text',
    ];

    // Iterate over views results.
    foreach ($output['#rows']['#rows'] as $delta => $row) {
      foreach ($row as $region => $value) {

        // Inject the render array into skipped regions.
        if (in_array($region, $skipped_regions)) {
          $output['#rows']['#rows'][$delta][$region] = [
            $renderArray,
          ];
        }
      }
    }
  }
}