View source
<?php
namespace Drupal\panels\Controller;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\ctools\Form\AjaxFormTrait;
use Drupal\panels\CachedValuesGetterTrait;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class Panels extends ControllerBase {
use AjaxFormTrait;
use CachedValuesGetterTrait;
protected $blockManager;
protected $conditionManager;
protected $variantManager;
protected $contextHandler;
protected $tempstore;
public function __construct(BlockManagerInterface $block_manager, PluginManagerInterface $condition_manager, PluginManagerInterface $variant_manager, ContextHandlerInterface $context_handler, SharedTempStoreFactory $tempstore) {
$this->blockManager = $block_manager;
$this->conditionManager = $condition_manager;
$this->variantManager = $variant_manager;
$this->contextHandler = $context_handler;
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block'), $container
->get('plugin.manager.condition'), $container
->get('plugin.manager.display_variant'), $container
->get('context.handler'), $container
->get('user.shared_tempstore'), $container
->get('plugin.manager.panels.pattern'));
}
public function selectBlock(Request $request, $machine_name, $tempstore_id) {
$cached_values = $this
->getCachedValues($this->tempstore, $tempstore_id, $machine_name);
$variant_plugin = $cached_values['plugin'];
$pattern_plugin = $variant_plugin
->getPattern();
$contexts = $pattern_plugin
->getDefaultContexts($this->tempstore, $tempstore_id, $machine_name);
$variant_plugin
->setContexts($contexts);
$build = [
'#type' => 'container',
'#attached' => [
'library' => [
'core/drupal.ajax',
],
],
];
$available_plugins = $this->blockManager
->getDefinitionsForContexts($variant_plugin
->getContexts());
$available_plugins = $this->blockManager
->getSortedDefinitions($available_plugins);
foreach ($available_plugins as $plugin_id => $plugin_definition) {
$category = $plugin_definition['category'];
$category_key = 'category-' . $category;
if (!isset($build[$category_key])) {
$build[$category_key] = [
'#type' => 'fieldgroup',
'#title' => $category,
'content' => [
'#theme' => 'links',
],
];
}
$build[$category_key]['content']['#links'][$plugin_id] = [
'title' => $plugin_definition['admin_label'],
'url' => $pattern_plugin
->getBlockAddUrl($tempstore_id, $machine_name, $plugin_id, $request->query
->get('region'), $request->query
->get('destination')),
'attributes' => $this
->getAjaxAttributes(),
];
}
return $build;
}
}