Created
May 22, 2017 19:24
-
-
Save slucero/6a9a739b8986420cff496b8312e35376 to your computer and use it in GitHub Desktop.
YAML Content - Block Placement Event Subscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\yaml_content\EventSubscriber; | |
use Drupal\block\Entity\Block; | |
use Drupal\block_content\Entity\BlockContent; | |
use Drupal\Core\Config\ConfigFactoryInterface; | |
use Drupal\Core\Entity\EntityStorageException; | |
use Drupal\yaml_content\Event\EntityPostSaveEvent; | |
use Drupal\yaml_content\Event\YamlContentEvents; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
/** | |
* An event subscriber to handle placement of block content entities in config. | |
* | |
* When a content block is imported and saved through yaml_content, place it | |
* in the block layout so it may be available for selection from entity reference | |
* fields pointing to block configuration entities. | |
*/ | |
class BlockContentPlacementSubscriber implements EventSubscriberInterface { | |
/** | |
* The configuration factory service to be used. | |
* | |
* @var \Drupal\Core\Config\ConfigFactoryInterface | |
*/ | |
protected $configFactory; | |
/** | |
* Constructs a BlockContentPlacementSubscriber object. | |
* | |
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
* The configuration factory service to be used. | |
*/ | |
public function __construct(ConfigFactoryInterface $config_factory) { | |
$this->configFactory = $config_factory; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() { | |
$events[YamlContentEvents::ENTITY_POST_SAVE][] = ['placeContentBlock']; | |
return $events; | |
} | |
/** | |
* Place an imported content block to generate configuration. | |
* | |
* When a content block entity is imported, place it in the hidden region of | |
* the theme to make it available for reference from block reference fields. | |
* | |
* @param \Drupal\yaml_content\Event\EntityPostSaveEvent $event | |
* The triggered event when an entity has been saved. | |
*/ | |
public function placeContentBlock(EntityPostSaveEvent $event) { | |
$entity = $event->getEntity(); | |
$content = $event->getContentData(); | |
if ($entity->getEntityTypeId() == 'block_content') { | |
\Drupal::logger('yaml_content')->debug('Event triggered: @class', ['@class' => get_class($event)]); | |
$config = $this->prepareBlockConfiguration($entity, $content); | |
$block = $this->createBlock($config); | |
// Disable the block by default. | |
$block->disable(); | |
try { | |
$block->save(); | |
} | |
catch (EntityStorageException $exception) { | |
\Drupal::logger('yaml_content') | |
->warning("Failed to place content block: @label\n @exception", [ | |
'@label' => $entity->label(), | |
'@exception' => $exception->getMessage(), | |
]); | |
} | |
} | |
} | |
/** | |
* Prepare configuration for block creation. | |
* | |
* @param \Drupal\block_content\Entity\BlockContent $block | |
* The content block that was created to trigger the event. | |
* @param array $content_data | |
* The content data corresponding to the created content block. | |
* | |
* @return array | |
* An array of settings prepared for block creation. | |
* | |
* @see \Drupal\barista\EventSubscriber\BlockContentPlacementSubscriber::createBlock() | |
*/ | |
public function prepareBlockConfiguration(BlockContent $block, array $content_data) { | |
$machine_name = $content_data['#extra']['machine_name']; | |
$uuid = 'block_content:' . $block->uuid(); | |
$block_config = [ | |
'id' => $machine_name, | |
'label' => $block->label(), | |
'plugin' => $uuid, | |
'provider' => 'block_content', | |
'theme' => $this->configFactory->get('system.theme')->get('default'), | |
'region' => 'content', | |
'visibility' => [], | |
'weight' => 0, | |
]; | |
return $block_config; | |
} | |
/** | |
* Create the block entity with the passed settings. | |
* | |
* @param array $settings | |
* An array of settings values to configure the created block. | |
* | |
* @return \Drupal\block\Entity\Block | |
* The created Block entity. | |
*/ | |
public function createBlock(array $settings) { | |
$values = []; | |
foreach (['region', 'id', 'theme', 'plugin', 'weight', 'visibility'] as $key) { | |
$values[$key] = $settings[$key]; | |
// Remove extra values that do not belong in the settings array. | |
unset($settings[$key]); | |
} | |
$values['settings'] = $settings; | |
$block = Block::create($values); | |
return $block; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment