Skip to content
Snippets Groups Projects
Commit fa0342f3 authored by aimai's avatar aimai Committed by Rich Gerdes
Browse files

Issue #3170810 by aimai, joaopauloscho: "Render #pre_render callbacks must be...

Issue #3170810 by aimai, joaopauloscho: "Render #pre_render callbacks must be methods of a class that implements \\Drupal\\Core\\Security\\TrustedCallbackInterface or be an anonymous function" PHP error after installation
parent 5c36ca26
Branches 8.x-1.x
Tags 8.x-1.3
No related merge requests found
......@@ -9,6 +9,7 @@ use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\redirect_node\RedirectNodeRenderCallback;
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
......@@ -16,29 +17,9 @@ use Drupal\node\Entity\Node;
* Add a prerender callback after all other callbacks to rewrite the displayed
* url in menus blocks.
*
* @See _redirect_node_block_render_alter().
*/
function redirect_node_block_view_system_menu_block_alter(array &$build, BlockPluginInterface $block) {
$build['#pre_render'][] = '_redirect_node_block_render_alter';
}
/**
* Pre render callback callback to alter urls for redirect nodes in menus.
*
* Calls _redirect_node_replace_node_url_walk_array() recursively on all menu
* items and their children. If the url for a menu item is rewritten, then a js
* script is attached that will inject edit links on each of the menu items.
*
* @See _redirect_node_replace_node_url_walk_array().
*/
function _redirect_node_block_render_alter(array $build) {
if (isset($build['content']['#items'])) {
$includeLibrary = _redirect_node_replace_node_url_walk_array($build['content']['#items']);
if ($includeLibrary) {
$build['#attached']['library'][] = 'redirect_node/redirect_node.edit_link';
}
}
return $build;
$build['#pre_render'][] = [RedirectNodeRenderCallback::class, 'preRender'];
}
/**
......
<?php
namespace Drupal\redirect_node;
use Drupal\Core\Render\Element\RenderCallbackInterface;
/**
* Implements a trusted preRender callback.
*/
class RedirectNodeRenderCallback implements RenderCallbackInterface {
/**
* Pre render callback callback to alter urls for redirect nodes in menus.
*
* @param array $build
* The render array.
*
* @See \Drupal\redirect_node\RedirectNodeRenderCallback::preRender().
*
* @return array
* The altered render array.
*/
public static function preRender(array $build) {
// Calls _redirect_node_replace_node_url_walk_array() recursively on all menu
// items and their children. If the url for a menu item is rewritten, then a js
// script is attached that will inject edit links on each of the menu items.
if (isset($build['content']['#items'])) {
$includeLibrary = _redirect_node_replace_node_url_walk_array($build['content']['#items']);
if ($includeLibrary) {
$build['#attached']['library'][] = 'redirect_node/redirect_node.edit_link';
}
}
return $build;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment