View source
<?php
namespace Drupal\ctools\Plugin\DisplayVariant;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Display\VariantBase;
use Drupal\Core\Display\ContextAwareVariantInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Token;
use Drupal\ctools\Form\AjaxFormTrait;
use Drupal\ctools\Plugin\BlockVariantInterface;
use Drupal\ctools\Plugin\BlockVariantTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVariantInterface, ContainerFactoryPluginInterface, BlockVariantInterface {
use AjaxFormTrait;
use BlockVariantTrait;
protected $contextHandler;
protected $uuidGenerator;
protected $account;
protected $token;
protected $contexts = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, BlockManagerInterface $block_manager, ConditionManager $condition_manager) {
$this->contextHandler = $context_handler;
$this->account = $account;
$this->uuidGenerator = $uuid_generator;
$this->token = $token;
$this->blockManager = $block_manager;
$this->conditionManager = $condition_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.handler'), $container
->get('current_user'), $container
->get('uuid'), $container
->get('token'), $container
->get('plugin.manager.block'), $container
->get('plugin.manager.condition'));
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'blocks' => [],
];
}
public function calculateDependencies() {
foreach ($this
->getBlockCollection() as $instance) {
$this
->calculatePluginDependencies($instance);
}
return $this->dependencies;
}
public function getConfiguration() {
return [
'blocks' => $this
->getBlockCollection()
->getConfiguration(),
] + parent::getConfiguration();
}
public function setConfiguration(array $configuration) {
if ($this->configuration && !empty($this->configuration['uuid'])) {
$configuration['uuid'] = $this->configuration['uuid'];
}
parent::setConfiguration($configuration);
$this
->getBlockCollection()
->setConfiguration($this->configuration['blocks']);
return $this;
}
public function getContexts() {
return $this->contexts;
}
public function setContexts(array $contexts) {
$this->contexts = $contexts;
return $this;
}
protected function contextHandler() {
return $this->contextHandler;
}
protected function getBlockConfig() {
return $this->configuration['blocks'];
}
protected function uuidGenerator() {
return $this->uuidGenerator;
}
public function __sleep() {
$vars = parent::__sleep();
if (($key = array_search('contexts', $vars)) !== FALSE) {
unset($vars[$key]);
}
if (($key = array_search('blockPluginCollection', $vars)) !== FALSE) {
if ($this->blockPluginCollection) {
$this->configuration['blocks'] = $this->blockPluginCollection
->getConfiguration();
}
unset($vars[$key]);
}
return $vars;
}
}