3 namespace Drupal\ctools\Plugin\DisplayVariant;
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Block\BlockManager;
7 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
8 use Drupal\Core\Condition\ConditionManager;
9 use Drupal\Core\Display\VariantBase;
10 use Drupal\Core\Display\ContextAwareVariantInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\Core\Plugin\Context\ContextHandlerInterface;
13 use Drupal\Core\Render\Element;
14 use Drupal\Core\Session\AccountInterface;
15 use Drupal\Core\Utility\Token;
16 use Drupal\ctools\Form\AjaxFormTrait;
17 use Drupal\ctools\Plugin\BlockVariantInterface;
18 use Drupal\ctools\Plugin\BlockVariantTrait;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
22 * Provides a base class for a display variant that simply contains blocks.
24 abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVariantInterface, ContainerFactoryPluginInterface, BlockVariantInterface, RefinableCacheableDependencyInterface {
27 use BlockVariantTrait;
30 * The context handler.
32 * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
34 protected $contextHandler;
39 * @var \Drupal\Component\Uuid\UuidInterface
41 protected $uuidGenerator;
46 * @var \Drupal\Core\Session\AccountInterface
53 * @var \Drupal\Core\Utility\Token
58 * An array of collected contexts.
60 * This is only used on runtime, and is not stored.
62 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
64 protected $contexts = [];
67 * Constructs a new BlockDisplayVariant.
69 * @param array $configuration
70 * A configuration array containing information about the plugin instance.
71 * @param string $plugin_id
72 * The plugin ID for the plugin instance.
73 * @param mixed $plugin_definition
74 * The plugin implementation definition.
75 * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
76 * The context handler.
77 * @param \Drupal\Core\Session\AccountInterface $account
79 * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
81 * @param \Drupal\Core\Utility\Token $token
83 * @param \Drupal\Core\Block\BlockManager $block_manager
85 * @param \Drupal\Core\Condition\ConditionManager $condition_manager
86 * The condition manager.
88 public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, BlockManager $block_manager, ConditionManager $condition_manager) {
89 // Inject dependencies as early as possible, so they can be used in
91 $this->contextHandler = $context_handler;
92 $this->account = $account;
93 $this->uuidGenerator = $uuid_generator;
94 $this->token = $token;
95 $this->blockManager = $block_manager;
96 $this->conditionManager = $condition_manager;
98 parent::__construct($configuration, $plugin_id, $plugin_definition);
104 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
109 $container->get('context.handler'),
110 $container->get('current_user'),
111 $container->get('uuid'),
112 $container->get('token'),
113 $container->get('plugin.manager.block'),
114 $container->get('plugin.manager.condition')
121 public function defaultConfiguration() {
122 return parent::defaultConfiguration() + [
130 public function calculateDependencies() {
131 foreach ($this->getBlockCollection() as $instance) {
132 $this->calculatePluginDependencies($instance);
134 return $this->dependencies;
140 public function getConfiguration() {
142 'blocks' => $this->getBlockCollection()->getConfiguration(),
143 ] + parent::getConfiguration();
149 public function setConfiguration(array $configuration) {
150 // preserve the uuid.
151 if ($this->configuration && !empty($this->configuration['uuid'])) {
152 $configuration['uuid'] = $this->configuration['uuid'];
154 parent::setConfiguration($configuration);
155 $this->getBlockCollection()->setConfiguration($this->configuration['blocks']);
162 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
163 * An array of set contexts, keyed by context name.
165 public function getContexts() {
166 return $this->contexts;
172 * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
173 * An array of contexts, keyed by context name.
177 public function setContexts(array $contexts) {
178 $this->contexts = $contexts;
185 protected function contextHandler() {
186 return $this->contextHandler;
192 protected function getBlockConfig() {
193 return $this->configuration['blocks'];
199 protected function uuidGenerator() {
200 return $this->uuidGenerator;
206 public function __sleep() {
207 $vars = parent::__sleep();
209 // Gathered contexts objects should not be serialized.
210 if (($key = array_search('contexts', $vars)) !== FALSE) {
214 // The block plugin collection should also not be serialized, ensure that
215 // configuration is synced back.
216 if (($key = array_search('blockPluginCollection', $vars)) !== FALSE) {
217 if ($this->blockPluginCollection) {
218 $this->configuration['blocks'] = $this->blockPluginCollection->getConfiguration();