Converts Views block content to a renderable array with contextual links.
$block: An array representing the block, with the same structure as the return value of hook_block_view(). This will be modified so as to force $block['content'] to be a renderable array, containing the optional '#contextual_links' property (if there are any contextual links associated with the block).
$view: The view that was used to generate the block content.
$display_id: The ID of the display within the view that was used to generate the block content.
$block_type: The type of the block. If it's block it's a regular views display, but 'special_block_-exp' exist as well.
function views_add_block_contextual_links(&$block, ViewExecutable $view, $display_id, $block_type = 'block') {
// Do not add contextual links to an empty block.
if (!empty($block['content'])) {
// Contextual links only work on blocks whose content is a renderable
// array, so if the block contains a string of already-rendered markup,
// convert it to an array.
if (is_string($block['content'])) {
$block['content'] = array(
'#markup' => $block['content'],
);
}
// Add the contextual links.
views_add_contextual_links($block['content'], $block_type, $view, $display_id);
}
}