public function LayoutBuilderBlockSanitizerManager::sanitizeNode in Layout Builder Block Sanitizer 8
Sanitize a node by ID.
File
- src/
LayoutBuilderBlockSanitizerManager.php, line 98
Class
- LayoutBuilderBlockSanitizerManager
- Class LayoutBuilderBlockSanitizerManager.
Namespace
Drupal\layout_builder_block_sanitizerCode
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(),
]));
}
}