You are here

class LayoutBuilderBlockSanitizerManager in Layout Builder Block Sanitizer 8

Class LayoutBuilderBlockSanitizerManager.

Hierarchy

Expanded class hierarchy of LayoutBuilderBlockSanitizerManager

1 file declares its use of LayoutBuilderBlockSanitizerManager
SanitizerForm.php in src/Form/SanitizerForm.php
1 string reference to 'LayoutBuilderBlockSanitizerManager'
layout_builder_block_sanitizer.services.yml in ./layout_builder_block_sanitizer.services.yml
layout_builder_block_sanitizer.services.yml
1 service uses LayoutBuilderBlockSanitizerManager
layout_builder_block_sanitizer.manager in ./layout_builder_block_sanitizer.services.yml
Drupal\layout_builder_block_sanitizer\LayoutBuilderBlockSanitizerManager

File

src/LayoutBuilderBlockSanitizerManager.php, line 22

Namespace

Drupal\layout_builder_block_sanitizer
View source
class LayoutBuilderBlockSanitizerManager implements ContainerInjectionInterface {
  use LayoutEntityHelperTrait;
  use StringTranslationTrait;

  /**
   * Drupal\block_content\BlockContentUuidLookup definition.
   *
   * @var \Drupal\block_content\BlockContentUuidLookup
   */
  protected $blockContentUuidLookup;

  /**
   * Drupal\layout_builder\SectionStorage\SectionStorageManager definition.
   *
   * @var \Drupal\layout_builder\SectionStorage\SectionStorageManager
   */
  protected $pluginManagerLayoutBuilderSectionStorage;

  /**
   * Drupal\layout_builder\LayoutTempstoreRepository definition.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepository
   */
  protected $layoutBuilderTempstoreRepository;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new LayoutBuilderBlockSanitizerManager object.
   */
  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) {
    $this->blockContentUuidLookup = $block_content_uuid_lookup;
    $this->pluginManagerLayoutBuilderSectionStorage = $plugin_manager_layout_builder_section_storage;
    $this->layoutBuilderTempstoreRepository = $layout_builder_tempstore_repository;
    $this->entityTypeManager = $entity_type_manager;
    $this->messenger = $messenger;
  }

  /**
   * Create method.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('block_content.uuid_lookup'), $container
      ->get('plugin.manager.layout_builder.section_storage'), $container
      ->get('layout_builder.tempstore_repository'), $container
      ->get('entity_type.manager'), $container
      ->get('messenger'));
  }

  /**
   * Helper callback to get list of nodes.
   *
   * @todo: Get only LB enabled node types?
   */
  public function getNodes() {
    return $this->entityTypeManager
      ->getStorage('node')
      ->loadMultiple();
  }

  /**
   * Sanitize a node by ID.
   */
  public function sanitizeNode($nid_to_sanitize) {
    try {

      // Load node objet to sanitize.
      $entity = $this->entityTypeManager
        ->getStorage('node')
        ->load($nid_to_sanitize);
      $type = 'overrides';
      $contexts['entity'] = EntityContext::fromEntity($entity);
      $view_mode = 'full';
      $view_mode = LayoutBuilderEntityViewDisplay::collectRenderDisplay($entity, $view_mode)
        ->getMode();
      $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode);
      $section_storage = $this->pluginManagerLayoutBuilderSectionStorage
        ->load($type, $contexts);
      $section_storage = $this->layoutBuilderTempstoreRepository
        ->get($section_storage);
      $id = $section_storage
        ->getStorageId();
      $sections = $section_storage
        ->getSections();

      // Check through each section's components to confirm blocks are valid.
      foreach ($sections as &$section) {
        $components = $section
          ->getComponents();
        foreach ($components as $section_component_uuid => $section_component) {
          $configuration = $section_component
            ->get('configuration');
          $provider = $configuration['provider'] ?? '';
          if ($provider == 'block_content') {
            $raw_id = $configuration['id'];
            $id = str_replace('block_content:', '', $raw_id);

            // Attempt to find a block w/ this UUID.
            $block = $this->blockContentUuidLookup
              ->get($id);
            if ($block == NULL) {
              $section
                ->removeComponent($section_component_uuid);
              $this->messenger
                ->addStatus($this
                ->t("Sanitized :block", [
                ':block' => $section_component_uuid,
              ]));
            }
          }
        }
      }

      // Sanitize default display.
      $section_storage = $this
        ->getSectionStorageForEntity($entity);
      $sections = $section_storage
        ->getSections();
      foreach ($sections as &$section) {
        $components = $section
          ->getComponents();
        foreach ($components as $section_component_uuid => $section_component) {
          $configuration = $section_component
            ->get('configuration');
          $provider = $configuration['provider'] ?? '';
          if ($provider == 'block_content') {
            $raw_id = $configuration['id'];
            $id = str_replace('block_content:', '', $raw_id);

            // Attempt to find a block w/ this UUID.
            $block = $this->blockContentUuidLookup
              ->get($id);
            if ($block == NULL) {
              $section
                ->removeComponent($section_component_uuid);
              $this->messenger
                ->addStatus($this
                ->t("Sanitized :block", [
                ':block' => $section_component_uuid,
              ]));
            }
          }
        }
      }
      $section_storage
        ->save();
    } catch (\TypeError $type_error) {

      // @todo Figure out why type error is thrown, take appropriate action.
    } catch (\Exception $e) {
      $this->messenger
        ->addWarning($this
        ->t("An exception was encountered: :e", [
        ':e' => $e
          ->getMessage(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutBuilderBlockSanitizerManager::$blockContentUuidLookup protected property Drupal\block_content\BlockContentUuidLookup definition.
LayoutBuilderBlockSanitizerManager::$entityTypeManager protected property The entity type manager.
LayoutBuilderBlockSanitizerManager::$layoutBuilderTempstoreRepository protected property Drupal\layout_builder\LayoutTempstoreRepository definition.
LayoutBuilderBlockSanitizerManager::$messenger protected property The Messenger service.
LayoutBuilderBlockSanitizerManager::$pluginManagerLayoutBuilderSectionStorage protected property Drupal\layout_builder\SectionStorage\SectionStorageManager definition.
LayoutBuilderBlockSanitizerManager::create public static function Create method. Overrides ContainerInjectionInterface::create
LayoutBuilderBlockSanitizerManager::getNodes public function Helper callback to get list of nodes.
LayoutBuilderBlockSanitizerManager::sanitizeNode public function Sanitize a node by ID.
LayoutBuilderBlockSanitizerManager::__construct public function Constructs a new LayoutBuilderBlockSanitizerManager object.
LayoutEntityHelperTrait::$sectionStorageManager protected property The section storage manager. 1
LayoutEntityHelperTrait::getEntitySections protected function Gets the sections for an entity if any.
LayoutEntityHelperTrait::getInlineBlockComponents protected function Gets components that have Inline Block plugins.
LayoutEntityHelperTrait::getInlineBlockRevisionIdsInSections protected function Gets revision IDs for layout sections.
LayoutEntityHelperTrait::getSectionStorageForEntity protected function Gets the section storage for an entity.
LayoutEntityHelperTrait::isEntityUsingFieldOverride Deprecated protected function Determines if an entity is using a field for the layout override.
LayoutEntityHelperTrait::isLayoutCompatibleEntity protected function Determines if an entity can have a layout.
LayoutEntityHelperTrait::originalEntityUsesDefaultStorage protected function Determines if the original entity used the default section storage.
LayoutEntityHelperTrait::sectionStorageManager private function Gets the section storage manager. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.