ParagraphBlocksPanelsController.php in Paragraph blocks 8.2
File
src/Controller/ParagraphBlocksPanelsController.php
View source
<?php
namespace Drupal\paragraph_blocks\Controller;
use Drupal\panels\Controller\Panels;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Render\Element;
class ParagraphBlocksPanelsController extends Panels {
public function selectBlock(Request $request, $machine_name, $tempstore_id) {
$build = parent::selectBlock($request, $machine_name, $tempstore_id);
$available_plugins = $this->blockManager
->getDefinitions();
$blocks = [];
foreach ($build as $category => $item) {
foreach (array_keys($item['content']['#links']) as $plugin_id) {
$plugin_definition = $available_plugins[$plugin_id];
$blocks[] = [
'plugin_id' => $plugin_id,
'category' => $plugin_definition['category'],
'id' => $plugin_definition['id'],
'provider' => $plugin_definition['provider'],
'label' => $plugin_definition['admin_label'],
];
}
}
$original_blocks = $blocks;
\Drupal::moduleHandler()
->alter('panels_ipe_blocks', $blocks);
$keyed_blocks = [];
foreach ($blocks as $block) {
$plugin_id = $block['plugin_id'];
$keyed_blocks[$plugin_id] = $block;
}
foreach (Element::children($build) as $category) {
$item =& $build[$category];
foreach (array_keys($item['content']['#links']) as $plugin_id) {
if (isset($keyed_blocks[$plugin_id])) {
$item['content']['#links'][$plugin_id]['title'] = $keyed_blocks[$plugin_id]['label'];
}
else {
unset($item['content']['#links'][$plugin_id]);
}
}
if (empty($item['content']['#links'])) {
unset($build[$category]);
}
}
return $build;
}
}