diff --git a/modules/ctools_inline_block/ctools_inline_block.module b/modules/ctools_inline_block/ctools_inline_block.module index 8002ed8..d0c10ed 100644 --- a/modules/ctools_inline_block/ctools_inline_block.module +++ b/modules/ctools_inline_block/ctools_inline_block.module @@ -3,6 +3,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\block\BlockInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\ctools_inline_block\Plugin\Block\InlineBlock; /** @@ -86,7 +87,7 @@ function ctools_inline_block_block_insert(BlockInterface $block) { ->insert('inline_block') ->fields([ 'uuid' => $plugin->getEntity()->uuid(), - 'loader' => 'ctools.block_loader', + 'loader' => 'ctools_inline_block.block_loader', 'data' => $block->id(), ]) ->execute(); diff --git a/modules/ctools_inline_block/ctools_inline_block.services.yml b/modules/ctools_inline_block/ctools_inline_block.services.yml index 3eed444..c32644f 100644 --- a/modules/ctools_inline_block/ctools_inline_block.services.yml +++ b/modules/ctools_inline_block/ctools_inline_block.services.yml @@ -1,5 +1,16 @@ services: - ctools.block_loader: + ctools_inline_block.block_loader: class: '\Drupal\ctools_inline_block\BlockLoader' arguments: - '@entity_type.manager' + ctools_inline_block.panels_block_loader: + class: '\Drupal\ctools_inline_block\PanelizerBlockLoader' + arguments: + - '@entity_type.manager' + - '@panelizer' + ctools_inline_block.event.inline_block: + class: '\Drupal\ctools_inline_block\EventSubscriber\InlineBlock' + arguments: + - '@database' + tags: + - { name: event_subscriber } diff --git a/modules/ctools_inline_block/src/EventSubscriber/InlineBlock.php b/modules/ctools_inline_block/src/EventSubscriber/InlineBlock.php new file mode 100644 index 0000000..70b73aa --- /dev/null +++ b/modules/ctools_inline_block/src/EventSubscriber/InlineBlock.php @@ -0,0 +1,60 @@ +connection = $connection; + } + + public static function getSubscribedEvents() { + $events[BlockVariantInterface::ADD_BLOCK][] = ['onVariantBlockAdd']; + $events[BlockVariantInterface::DELETE_BLOCK][] = ['onVariantBlockDelete']; + return $events; + } + + public function onVariantBlockAdd(BlockVariantEvent $event) { + $block = $event->getBlock(); + $block_plugin_id = $block->getPluginId(); + if ($block_plugin_id && explode(':', $block_plugin_id)[0] == 'inline_content') { + /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display_variant */ + $panels_display_variant = $event->getVariant(); + // Panelizer Default Display + $loader = 'ctools_inline_block.panels_block_loader'; + $data = "{$panels_display_variant->getStorageType()}:{$panels_display_variant->getStorageId()}"; + $this->connection + ->insert('inline_block') + ->fields([ + 'uuid' => $block->getConfiguration()['uuid'], + 'loader' => $loader, + 'data' => $data, + ]) + ->execute(); + } + } + + public function onVariantBlockDelete(BlockVariantEvent $event) { + $block = $event->getBlock(); + $block_plugin_id = $block->getPluginId(); + if ($block_plugin_id && explode(':', $block_plugin_id)[0] == 'inline_content') { + $this->connection + ->delete('inline_block') + ->condition('uuid', $block->getConfiguration()['uuid']) + ->execute(); + } + } + +} diff --git a/modules/ctools_inline_block/src/PanelsBlockLoader.php b/modules/ctools_inline_block/src/PanelsBlockLoader.php new file mode 100644 index 0000000..4ce7d54 --- /dev/null +++ b/modules/ctools_inline_block/src/PanelsBlockLoader.php @@ -0,0 +1,68 @@ +entityTypeManager = $entity_type_manager; + $this->panelizer = $panelizer; + } + + /** + * {@inheritdoc} + */ + public function load($uuid, $data = NULL) { + // $data is expected to be storage type and storage id. + if ($data) { + list($storage_type, $storage_id) = explode(':', $data, 2); + if ($storage_type == 'panelizer_default') { + list($entity_type, $bundle, $view_mode, $default) = explode(':', $storage_id); + $display = $this->panelizer->getDefaultPanelsDisplay($default, $entity_type, $bundle, $view_mode); + return $display->getBlock($uuid); + } + if ($storage_type == 'panelizer_field') { + list($entity_type, $entity_id, $view_mode, $revision_id) = explode(':', $storage_id); + /** @var FieldableEntityInterface $entity */ + if ($revision_id) { + $entity = $this->entityTypeManager->getStorage($entity_type)->loadRevision($revision_id); + } + else { + $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); + } + $display = $this->panelizer->getPanelsDisplay($entity, $view_mode); + return $display->getBlock($uuid); + } + } + else { + throw new \InvalidArgumentException('No entity ID was specified'); + } + } + +} diff --git a/modules/ctools_inline_block/src/Plugin/Block/InlineBlock.php b/modules/ctools_inline_block/src/Plugin/Block/InlineBlock.php index 3729eef..be3b6ba 100644 --- a/modules/ctools_inline_block/src/Plugin/Block/InlineBlock.php +++ b/modules/ctools_inline_block/src/Plugin/Block/InlineBlock.php @@ -64,7 +64,7 @@ class InlineBlock extends BlockContentBlock { public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['view_mode'] = $form_state->getValue('view_mode'); /** @var \Drupal\block_content\BlockContentInterface $entity */ - $entity = $form['settings']['entity']['#entity'] ?: $form['entity']['#entity']; + $entity = !empty($form['settings']['entity']['#entity']) ? $form['settings']['entity']['#entity'] : $form['entity']['#entity']; $this->configuration['label'] = $entity->label(); $this->configuration['langcode'] = $form_state->getValue('langcode'); diff --git a/src/Event/BlockVariantEvent.php b/src/Event/BlockVariantEvent.php new file mode 100644 index 0000000..9aa7f05 --- /dev/null +++ b/src/Event/BlockVariantEvent.php @@ -0,0 +1,58 @@ +block = $block; + $this->variant = $variant; + } + + /** + * Gets the block plugin. + * + * @return BlockPluginInterface + */ + public function getBlock() { + return $this->block; + } + + /** + * Gets the variant plugin. + * + * @return BlockVariantInterface + */ + public function getVariant() { + return $this->variant; + } + +} diff --git a/src/Plugin/BlockVariantInterface.php b/src/Plugin/BlockVariantInterface.php index aa2709a..4aec7cd 100644 --- a/src/Plugin/BlockVariantInterface.php +++ b/src/Plugin/BlockVariantInterface.php @@ -10,6 +10,21 @@ use Drupal\Core\Display\VariantInterface; interface BlockVariantInterface extends VariantInterface { /** + * Denotes that a block is being added to the variant. + */ + CONST ADD_BLOCK = 'add.block'; + + /** + * Denotes that a block is being updated in the variant. + */ + CONST UPDATE_BLOCK = 'update.block'; + + /** + * Denotes that a block is being deleted from the variant. + */ + CONST DELETE_BLOCK = 'delete.block'; + + /** * Returns the human-readable list of regions keyed by machine name. * * @return array diff --git a/src/Plugin/BlockVariantTrait.php b/src/Plugin/BlockVariantTrait.php index fa01870..856dc0e 100644 --- a/src/Plugin/BlockVariantTrait.php +++ b/src/Plugin/BlockVariantTrait.php @@ -2,6 +2,8 @@ namespace Drupal\ctools\Plugin; +use Drupal\ctools\Event\BlockVariantEvent; + /** * Provides methods for \Drupal\ctools\Plugin\BlockVariantInterface. */ @@ -39,6 +41,11 @@ trait BlockVariantTrait { public function addBlock(array $configuration) { $configuration['uuid'] = $this->uuidGenerator()->generate(); $this->getBlockCollection()->addInstanceId($configuration['uuid'], $configuration); + + $block = $this->getBlock($configuration['uuid']); + $event = new BlockVariantEvent($block, $this); + $this->getDispatcher()->dispatch(BlockVariantInterface::ADD_BLOCK, $event); + return $configuration['uuid']; } @@ -46,6 +53,9 @@ trait BlockVariantTrait { * @see \Drupal\ctools\Plugin\BlockVariantInterface::removeBlock() */ public function removeBlock($block_id) { + $block = $this->getBlock($block_id); + $event = new BlockVariantEvent($block, $this); + $this->getDispatcher()->dispatch(BlockVariantInterface::DELETE_BLOCK, $event); $this->getBlockCollection()->removeInstanceId($block_id); return $this; } @@ -54,8 +64,11 @@ trait BlockVariantTrait { * @see \Drupal\ctools\Plugin\BlockVariantInterface::updateBlock() */ public function updateBlock($block_id, array $configuration) { - $existing_configuration = $this->getBlock($block_id)->getConfiguration(); + $block = $this->getBlock($block_id); + $existing_configuration = $block->getConfiguration(); $this->getBlockCollection()->setInstanceConfiguration($block_id, $configuration + $existing_configuration); + $event = new BlockVariantEvent($block, $this); + $this->getDispatcher()->dispatch(BlockVariantInterface::ADD_BLOCK, $event); return $this; } @@ -113,6 +126,18 @@ trait BlockVariantTrait { } /** + * Gets the event dispatcher. + * + * @return \Symfony\Component\EventDispatcher\EventDispatcher + */ + protected function getDispatcher() { + if (!$this->dispatcher) { + $this->dispatcher = \Drupal::service('event_dispatcher'); + } + return $this->dispatcher; + } + + /** * Returns the UUID generator. * * @return \Drupal\Component\Uuid\UuidInterface