Loading layout_builder_block_delete.services.yml +1 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ services: - '@messenger' - '@event_dispatcher' - '@logger.factory' - '@entity_type.bundle.info' layout_builder_block_delete.batch: class: Drupal\layout_builder_block_delete\LayoutBuilderBlockDeleteBatch arguments: Loading src/Form/DeleteForm.php +9 −1 Original line number Diff line number Diff line Loading @@ -62,6 +62,13 @@ class DeleteForm extends FormBase implements ContainerInjectionInterface { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['block_type'] = [ '#type' => 'select', '#title' => $this->t('Block type'), '#options' => $this->layoutBuilderBlockDeleteManager->getBlockTypesAsOptions(), '#empty_option' => $this->t('- All -'), '#description' => $this->t('You can optionally select a block type. If none is selected, all blocks will be deleted.'), ]; $form['content_types'] = [ '#type' => 'checkboxes', '#title' => $this->t('Content types'), Loading @@ -83,7 +90,8 @@ class DeleteForm extends FormBase implements ContainerInjectionInterface { */ public function submitForm(array &$form, FormStateInterface $form_state) { $content_types = $form_state->getValue('content_types'); $this->layoutBuilderBlockDeleteBatch->batchStart($content_types); $block_type = $form_state->getValue('block_type'); $this->layoutBuilderBlockDeleteBatch->batchStart($content_types, $block_type); } } src/LayoutBuilderBlockDeleteBatch.php +7 −5 Original line number Diff line number Diff line Loading @@ -39,7 +39,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { /** * Kick off batch process to delete blocks on all nodes for content types. */ public function batchStart(array $content_types) { public function batchStart(array $content_types, string $block_type = NULL) { $nodes = []; foreach ($content_types as $content_type) { Loading @@ -59,7 +59,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { 'batchDeleteAllBlocks', ], [ array_keys($nodes), ['nids' => array_keys($nodes), 'block_type' => $block_type], ], ], ], Loading @@ -74,7 +74,9 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { /** * Load nodes in batch process progressively to delete all blocks. */ public static function batchDeleteAllBlocks($nids, &$context) { public static function batchDeleteAllBlocks(array $data, &$context) { $nids = $data['nids']; if (empty($context['sandbox'])) { // Flush caches to avoid false positives looking for block UUID. drupal_flush_all_caches(); Loading @@ -97,7 +99,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { $layout_builder_block_delete_manager = \Drupal::service('layout_builder_block_delete.manager'); if (!empty($nids[$row])) { $layout_builder_block_delete_manager->deleteBlocks($nids[$row]); $layout_builder_block_delete_manager->deleteBlocks($nids[$row], $data['block_type']); } $operation_details = t('Deleting blocks for nodes :current to :limit', [ Loading src/LayoutBuilderBlockDeleteManager.php +43 −6 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\layout_builder_block_delete; use Drupal\block_content\BlockContentUuidLookup; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Utility\Error; use Drupal\layout_builder\LayoutTempstoreRepository; Loading Loading @@ -73,6 +74,13 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { */ protected $logger; /** * The entity type bundle info. * * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface */ protected $entityTypeBundleInfo; /** * Constructs a new LayoutBuilderBlockSanitizerManager object. * Loading @@ -80,8 +88,10 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { * The event dispatcher. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory * The logger for this channel. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * The entity type bundle info. */ public function __construct(BlockContentUuidLookup $block_content_uuid_lookup, SectionStorageManager $plugin_manager_layout_builder_section_storage, LayoutTempstoreRepository $layout_builder_tempstore_repository, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, LoggerChannelFactoryInterface $logger_factory) { public function __construct(BlockContentUuidLookup $block_content_uuid_lookup, SectionStorageManager $plugin_manager_layout_builder_section_storage, LayoutTempstoreRepository $layout_builder_tempstore_repository, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, LoggerChannelFactoryInterface $logger_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info) { $this->blockContentUuidLookup = $block_content_uuid_lookup; $this->pluginManagerLayoutBuilderSectionStorage = $plugin_manager_layout_builder_section_storage; $this->layoutBuilderTempstoreRepository = $layout_builder_tempstore_repository; Loading @@ -89,6 +99,7 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { $this->messenger = $messenger; $this->eventDispatcher = $event_dispatcher; $this->logger = $logger_factory->get('layout_builder_block_delete'); $this->entityTypeBundleInfo = $entity_type_bundle_info; } /** Loading @@ -102,10 +113,28 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { $container->get('entity_type.manager'), $container->get('messenger'), $container->get('event_dispatcher'), $container->get('logger.factory') $container->get('logger.factory'), $container->get('entity_type.bundle.info') ); } /** * Returns a list of all the available block types. * * @return array * An array of block types. */ public function getBlockTypesAsOptions(): array { $block_types = []; $bundles = $this->entityTypeBundleInfo->getBundleInfo('block_content'); foreach ($bundles as $machine_name => $bundle) { $block_types[$machine_name] = $bundle['label']; } return $block_types; } /** * Returns a list of all the content types currently installed. * Loading Loading @@ -161,19 +190,27 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { /** * Delete all blocks for a node. */ public function deleteBlocks(int $nid) { public function deleteBlocks(int $nid, string $block_type = NULL) { try { $entity = $this->entityTypeManager->getStorage('node')->load($nid); $section_storage = $this->getSectionStorageForEntity($entity); $sections = $section_storage->getSections(); foreach ($sections as &$section) { $components = $section->getComponents(); if (!empty($components)) { foreach ($components as $section_component_uuid => $section_component) { if (!empty($block_type)) { if ($section_component->getPluginId() === 'inline_block:' . $block_type) { $section->removeComponent($section_component_uuid); } } else { $section->removeComponent($section_component_uuid); } } } } $section_storage->save(); // $this->messenger->addStatus($this->t("Deleted blocks for node :nid", [':nid' => $nid])); } catch (\Exception $e) { $this->messenger->addWarning($this->t("An exception was encountered: :e", [':e' => $e->getMessage()])); Loading Loading
layout_builder_block_delete.services.yml +1 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ services: - '@messenger' - '@event_dispatcher' - '@logger.factory' - '@entity_type.bundle.info' layout_builder_block_delete.batch: class: Drupal\layout_builder_block_delete\LayoutBuilderBlockDeleteBatch arguments: Loading
src/Form/DeleteForm.php +9 −1 Original line number Diff line number Diff line Loading @@ -62,6 +62,13 @@ class DeleteForm extends FormBase implements ContainerInjectionInterface { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['block_type'] = [ '#type' => 'select', '#title' => $this->t('Block type'), '#options' => $this->layoutBuilderBlockDeleteManager->getBlockTypesAsOptions(), '#empty_option' => $this->t('- All -'), '#description' => $this->t('You can optionally select a block type. If none is selected, all blocks will be deleted.'), ]; $form['content_types'] = [ '#type' => 'checkboxes', '#title' => $this->t('Content types'), Loading @@ -83,7 +90,8 @@ class DeleteForm extends FormBase implements ContainerInjectionInterface { */ public function submitForm(array &$form, FormStateInterface $form_state) { $content_types = $form_state->getValue('content_types'); $this->layoutBuilderBlockDeleteBatch->batchStart($content_types); $block_type = $form_state->getValue('block_type'); $this->layoutBuilderBlockDeleteBatch->batchStart($content_types, $block_type); } }
src/LayoutBuilderBlockDeleteBatch.php +7 −5 Original line number Diff line number Diff line Loading @@ -39,7 +39,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { /** * Kick off batch process to delete blocks on all nodes for content types. */ public function batchStart(array $content_types) { public function batchStart(array $content_types, string $block_type = NULL) { $nodes = []; foreach ($content_types as $content_type) { Loading @@ -59,7 +59,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { 'batchDeleteAllBlocks', ], [ array_keys($nodes), ['nids' => array_keys($nodes), 'block_type' => $block_type], ], ], ], Loading @@ -74,7 +74,9 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { /** * Load nodes in batch process progressively to delete all blocks. */ public static function batchDeleteAllBlocks($nids, &$context) { public static function batchDeleteAllBlocks(array $data, &$context) { $nids = $data['nids']; if (empty($context['sandbox'])) { // Flush caches to avoid false positives looking for block UUID. drupal_flush_all_caches(); Loading @@ -97,7 +99,7 @@ class LayoutBuilderBlockDeleteBatch implements ContainerInjectionInterface { $layout_builder_block_delete_manager = \Drupal::service('layout_builder_block_delete.manager'); if (!empty($nids[$row])) { $layout_builder_block_delete_manager->deleteBlocks($nids[$row]); $layout_builder_block_delete_manager->deleteBlocks($nids[$row], $data['block_type']); } $operation_details = t('Deleting blocks for nodes :current to :limit', [ Loading
src/LayoutBuilderBlockDeleteManager.php +43 −6 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\layout_builder_block_delete; use Drupal\block_content\BlockContentUuidLookup; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Utility\Error; use Drupal\layout_builder\LayoutTempstoreRepository; Loading Loading @@ -73,6 +74,13 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { */ protected $logger; /** * The entity type bundle info. * * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface */ protected $entityTypeBundleInfo; /** * Constructs a new LayoutBuilderBlockSanitizerManager object. * Loading @@ -80,8 +88,10 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { * The event dispatcher. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory * The logger for this channel. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * The entity type bundle info. */ public function __construct(BlockContentUuidLookup $block_content_uuid_lookup, SectionStorageManager $plugin_manager_layout_builder_section_storage, LayoutTempstoreRepository $layout_builder_tempstore_repository, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, LoggerChannelFactoryInterface $logger_factory) { public function __construct(BlockContentUuidLookup $block_content_uuid_lookup, SectionStorageManager $plugin_manager_layout_builder_section_storage, LayoutTempstoreRepository $layout_builder_tempstore_repository, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, LoggerChannelFactoryInterface $logger_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info) { $this->blockContentUuidLookup = $block_content_uuid_lookup; $this->pluginManagerLayoutBuilderSectionStorage = $plugin_manager_layout_builder_section_storage; $this->layoutBuilderTempstoreRepository = $layout_builder_tempstore_repository; Loading @@ -89,6 +99,7 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { $this->messenger = $messenger; $this->eventDispatcher = $event_dispatcher; $this->logger = $logger_factory->get('layout_builder_block_delete'); $this->entityTypeBundleInfo = $entity_type_bundle_info; } /** Loading @@ -102,10 +113,28 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { $container->get('entity_type.manager'), $container->get('messenger'), $container->get('event_dispatcher'), $container->get('logger.factory') $container->get('logger.factory'), $container->get('entity_type.bundle.info') ); } /** * Returns a list of all the available block types. * * @return array * An array of block types. */ public function getBlockTypesAsOptions(): array { $block_types = []; $bundles = $this->entityTypeBundleInfo->getBundleInfo('block_content'); foreach ($bundles as $machine_name => $bundle) { $block_types[$machine_name] = $bundle['label']; } return $block_types; } /** * Returns a list of all the content types currently installed. * Loading Loading @@ -161,19 +190,27 @@ class LayoutBuilderBlockDeleteManager implements ContainerInjectionInterface { /** * Delete all blocks for a node. */ public function deleteBlocks(int $nid) { public function deleteBlocks(int $nid, string $block_type = NULL) { try { $entity = $this->entityTypeManager->getStorage('node')->load($nid); $section_storage = $this->getSectionStorageForEntity($entity); $sections = $section_storage->getSections(); foreach ($sections as &$section) { $components = $section->getComponents(); if (!empty($components)) { foreach ($components as $section_component_uuid => $section_component) { if (!empty($block_type)) { if ($section_component->getPluginId() === 'inline_block:' . $block_type) { $section->removeComponent($section_component_uuid); } } else { $section->removeComponent($section_component_uuid); } } } } $section_storage->save(); // $this->messenger->addStatus($this->t("Deleted blocks for node :nid", [':nid' => $nid])); } catch (\Exception $e) { $this->messenger->addWarning($this->t("An exception was encountered: :e", [':e' => $e->getMessage()])); Loading