32c6e86fd0f548b332a5d1afdb3caecd8fdda3ee
[yaffs-website] / web / modules / contrib / ctools / src / Plugin / DisplayVariant / BlockDisplayVariant.php
1 <?php
2
3 namespace Drupal\ctools\Plugin\DisplayVariant;
4
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;
20
21 /**
22  * Provides a base class for a display variant that simply contains blocks.
23  */
24 abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVariantInterface, ContainerFactoryPluginInterface, BlockVariantInterface, RefinableCacheableDependencyInterface {
25
26   use AjaxFormTrait;
27   use BlockVariantTrait;
28
29   /**
30    * The context handler.
31    *
32    * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
33    */
34   protected $contextHandler;
35
36   /**
37    * The UUID generator.
38    *
39    * @var \Drupal\Component\Uuid\UuidInterface
40    */
41   protected $uuidGenerator;
42
43   /**
44    * The current user.
45    *
46    * @var \Drupal\Core\Session\AccountInterface
47    */
48   protected $account;
49
50   /**
51    * The token service.
52    *
53    * @var \Drupal\Core\Utility\Token
54    */
55   protected $token;
56
57   /**
58    * An array of collected contexts.
59    *
60    * This is only used on runtime, and is not stored.
61    *
62    * @var \Drupal\Component\Plugin\Context\ContextInterface[]
63    */
64   protected $contexts = [];
65
66   /**
67    * Constructs a new BlockDisplayVariant.
68    *
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
78    *   The current user.
79    * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
80    *   The UUID generator.
81    * @param \Drupal\Core\Utility\Token $token
82    *   The token service.
83    * @param \Drupal\Core\Block\BlockManager $block_manager
84    *   The block manager.
85    * @param \Drupal\Core\Condition\ConditionManager $condition_manager
86    *   The condition manager.
87    */
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
90     // configuration.
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;
97
98     parent::__construct($configuration, $plugin_id, $plugin_definition);
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
105     return new static(
106       $configuration,
107       $plugin_id,
108       $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')
115     );
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function defaultConfiguration() {
122     return parent::defaultConfiguration() + [
123       'blocks' => []
124     ];
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function calculateDependencies() {
131     foreach ($this->getBlockCollection() as $instance) {
132       $this->calculatePluginDependencies($instance);
133     }
134     return $this->dependencies;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function getConfiguration() {
141     return [
142       'blocks' => $this->getBlockCollection()->getConfiguration(),
143     ] + parent::getConfiguration();
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function setConfiguration(array $configuration) {
150     // preserve the uuid.
151     if ($this->configuration && !empty($this->configuration['uuid'])) {
152       $configuration['uuid'] = $this->configuration['uuid'];
153     }
154     parent::setConfiguration($configuration);
155     $this->getBlockCollection()->setConfiguration($this->configuration['blocks']);
156     return $this;
157   }
158
159   /**
160    * Gets the contexts.
161    *
162    * @return \Drupal\Component\Plugin\Context\ContextInterface[]
163    *   An array of set contexts, keyed by context name.
164    */
165   public function getContexts() {
166     return $this->contexts;
167   }
168
169   /**
170    * Sets the contexts.
171    *
172    * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
173    *   An array of contexts, keyed by context name.
174    *
175    * @return $this
176    */
177   public function setContexts(array $contexts) {
178     $this->contexts = $contexts;
179     return $this;
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   protected function contextHandler() {
186     return $this->contextHandler;
187   }
188
189   /**
190    * {@inheritdoc}
191    */
192   protected function getBlockConfig() {
193     return $this->configuration['blocks'];
194   }
195
196   /**
197    * {@inheritdoc}
198    */
199   protected function uuidGenerator() {
200     return $this->uuidGenerator;
201   }
202
203   /**
204    * {@inheritdoc}
205    */
206   public function __sleep() {
207     $vars = parent::__sleep();
208
209     // Gathered contexts objects should not be serialized.
210     if (($key = array_search('contexts', $vars)) !== FALSE) {
211       unset($vars[$key]);
212     }
213
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();
219       }
220       unset($vars[$key]);
221     }
222
223     return $vars;
224   }
225
226 }