Commit b261004a authored by webchick's avatar webchick

Issue #1879256 by tim.plunkett, dawehner: Refactor viewSpecialBlocks...

Issue #1879256 by tim.plunkett, dawehner: Refactor viewSpecialBlocks architecture once blocks are plugins.
parent 8fa982af
......@@ -78,9 +78,35 @@ public function build() {
$this->entity->set('label', filter_xss_admin($this->view->getTitle()));
// Before returning the block output, convert it to a renderable array
// with contextual links.
views_add_block_contextual_links($output, $this->view, $this->displayID);
$this->addContextualLinks($output);
$this->view->destroy();
return $output;
}
/**
* Converts Views block content to a renderable array with contextual links.
*
* @param string|array $output
* An string|array representing the block. This will be modified to be a
* renderable array, containing the optional '#contextual_links' property (if
* there are any contextual links associated with the block).
* @param string $block_type
* The type of the block. If it's 'block' it's a regular views display,
* but 'exposed_filter' exist as well.
*/
protected function addContextualLinks(&$output, $block_type = 'block') {
// Do not add contextual links to an empty block.
if (!empty($output)) {
// 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($output)) {
$output = array('#markup' => $output);
}
// Add the contextual links.
views_add_contextual_links($output, $block_type, $this->view, $this->displayID);
}
}
}
......@@ -26,11 +26,11 @@ class ViewsExposedFilterBlock extends ViewsBlock {
* Implements \Drupal\block\BlockBase::build().
*/
public function build() {
$type = 'exp';
$output = $this->view->display_handler->viewSpecialBlocks($type);
$output = $this->view->display_handler->viewExposedFormBlocks();
// Before returning the block output, convert it to a renderable array with
// contextual links.
views_add_block_contextual_links($output, $this->view, $this->display_id, 'special_block_' . $type);
$this->addContextualLinks($output, 'exposed_filter');
$this->view->destroy();
return $output;
}
......
......@@ -2646,20 +2646,21 @@ public function getSpecialBlocks() {
}
/**
* Render any special blocks provided for this display.
* Render the exposed form as block.
*
* @return string|NULL
* The rendered exposed form as string or NULL otherwise.
*/
public function viewSpecialBlocks($type) {
if ($type == 'exp') {
// avoid interfering with the admin forms.
if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'views') {
return;
}
$this->view->initHandlers();
public function viewExposedFormBlocks() {
// avoid interfering with the admin forms.
if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'views') {
return;
}
$this->view->initHandlers();
if ($this->usesExposed() && $this->getOption('exposed_block')) {
$exposed_form = $this->getPlugin('exposed_form');
return $exposed_form->render_exposed_form(TRUE);
}
if ($this->usesExposed() && $this->getOption('exposed_block')) {
$exposed_form = $this->getPlugin('exposed_form');
return $exposed_form->render_exposed_form(TRUE);
}
}
......
......@@ -503,38 +503,6 @@ function views_contextual_links_view_alter(&$element, $items) {
}
}
/**
* Converts Views block content to a renderable array with contextual links.
*
* @param $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).
* @param $view
* The view that was used to generate the block content.
* @param $display_id
* The ID of the display within the view that was used to generate the block
* content.
* @param $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)) {
// 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)) {
$block = array('#markup' => $block);
}
// Add the contextual links.
views_add_contextual_links($block, $block_type, $view, $display_id);
}
}
/**
* Adds contextual links associated with a view display to a renderable array.
*
......@@ -620,7 +588,7 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable
}
// On exposed_forms blocks contextual links should always be visible.
$plugin['contextual_links_locations'][] = 'special_block_-exp';
$plugin['contextual_links_locations'][] = 'exposed_filter';
$has_links = !empty($plugin['contextual links']) && !empty($plugin['contextual_links_locations']);
if ($has_links && in_array($location, $plugin['contextual_links_locations'])) {
foreach ($plugin['contextual links'] as $module => $link) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment