trait BlockVariantTrait
Same name in other branches
- 8.x-3.x src/Plugin/BlockVariantTrait.php \Drupal\ctools\Plugin\BlockVariantTrait
Provides methods for \Drupal\ctools\Plugin\BlockVariantInterface.
Hierarchy
- trait \Drupal\ctools\Plugin\BlockVariantTrait
2 files declare their use of BlockVariantTrait
- BlockDisplayVariant.php in src/
Plugin/ DisplayVariant/ BlockDisplayVariant.php - BlockVariantTraitTest.php in tests/
src/ Unit/ BlockVariantTraitTest.php
File
-
src/
Plugin/ BlockVariantTrait.php, line 11
Namespace
Drupal\ctools\PluginView source
trait BlockVariantTrait {
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManager
*/
protected $blockManager;
/**
* The plugin collection that holds the block plugins.
*
* @var \Drupal\ctools\Plugin\BlockPluginCollection
*/
protected $blockPluginCollection;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionNames()
*/
public abstract function getRegionNames();
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::getBlock()
*/
public function getBlock($block_id) {
return $this->getBlockCollection()
->get($block_id);
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::addBlock()
*/
public function addBlock(array $configuration) {
$configuration['uuid'] = $this->uuidGenerator()
->generate();
$this->getBlockCollection()
->addInstanceId($configuration['uuid'], $configuration);
$block = $this->getBlock($configuration['uuid']);
// Allow modules to react to the change.
$event = new BlockVariantEvent($block, $this);
$this->eventDispatcher()
->dispatch($event, BlockVariantEvents::ADD_BLOCK);
return $configuration['uuid'];
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::removeBlock()
*/
public function removeBlock($block_id) {
$block = $this->getBlock($block_id);
$this->getBlockCollection()
->removeInstanceId($block_id);
// Allow modules to react to the change.
$event = new BlockVariantEvent($block, $this);
$this->eventDispatcher()
->dispatch($event, BlockVariantEvents::DELETE_BLOCK);
return $this;
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::updateBlock()
*/
public function updateBlock($block_id, array $configuration) {
$block = $this->getBlock($block_id);
$existing_configuration = $block->getConfiguration();
$this->getBlockCollection()
->setInstanceConfiguration($block_id, $configuration + $existing_configuration);
// Allow modules to react to the change.
$event = new BlockVariantEvent($block, $this);
$this->eventDispatcher()
->dispatch($event, BlockVariantEvents::UPDATE_BLOCK);
return $this;
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignment()
*/
public function getRegionAssignment($block_id) {
$configuration = $this->getBlock($block_id)
->getConfiguration();
return $configuration['region'] ?? NULL;
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignments()
*/
public function getRegionAssignments() {
// Build an array of the region names in the right order.
$empty = array_fill_keys(array_keys($this->getRegionNames()), []);
$full = $this->getBlockCollection()
->getAllByRegion();
// Merge it with the actual values to maintain the ordering.
return array_intersect_key(array_merge($empty, $full), $empty);
}
/**
* @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionName()
*/
public function getRegionName($region) {
$regions = $this->getRegionNames();
return $regions[$region] ?? '';
}
/**
* Gets the block plugin manager.
*
* @return \Drupal\Core\Block\BlockManager
* The block plugin manager.
*/
protected function getBlockManager() {
if (!$this->blockManager) {
$this->blockManager = \Drupal::service('plugin.manager.block');
}
return $this->blockManager;
}
/**
* Returns the block plugins used for this display variant.
*
* @return \Drupal\Core\Block\BlockPluginInterface[]|\Drupal\ctools\Plugin\BlockPluginCollection
* An array or collection of configured block plugins.
*/
protected function getBlockCollection() {
if (!$this->blockPluginCollection) {
$this->blockPluginCollection = new BlockPluginCollection($this->getBlockManager(), $this->getBlockConfig());
}
return $this->blockPluginCollection;
}
/**
* Gets the event dispatcher.
*
* @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected function eventDispatcher() {
if (!$this->eventDispatcher) {
$this->eventDispatcher = \Drupal::service('event_dispatcher');
}
return $this->eventDispatcher;
}
/**
* Returns the UUID generator.
*
* @return \Drupal\Component\Uuid\UuidInterface
*/
protected abstract function uuidGenerator();
/**
* Returns the configuration for stored blocks.
*
* @return array
* An array of block configuration, keyed by the unique block ID.
*/
protected abstract function getBlockConfig();
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
BlockVariantTrait::$blockManager | protected | property | The block manager. | |
BlockVariantTrait::$blockPluginCollection | protected | property | The plugin collection that holds the block plugins. | |
BlockVariantTrait::$eventDispatcher | protected | property | The event dispatcher. | |
BlockVariantTrait::addBlock | public | function | ||
BlockVariantTrait::eventDispatcher | protected | function | Gets the event dispatcher. | |
BlockVariantTrait::getBlock | public | function | ||
BlockVariantTrait::getBlockCollection | protected | function | Returns the block plugins used for this display variant. | |
BlockVariantTrait::getBlockConfig | abstract protected | function | Returns the configuration for stored blocks. | 2 |
BlockVariantTrait::getBlockManager | protected | function | Gets the block plugin manager. | |
BlockVariantTrait::getRegionAssignment | public | function | ||
BlockVariantTrait::getRegionAssignments | public | function | ||
BlockVariantTrait::getRegionName | public | function | ||
BlockVariantTrait::getRegionNames | abstract public | function | 2 | |
BlockVariantTrait::removeBlock | public | function | ||
BlockVariantTrait::updateBlock | public | function | ||
BlockVariantTrait::uuidGenerator | abstract protected | function | Returns the UUID generator. | 2 |