LayoutBuilderLock.php in Layout Builder Lock 8
File
src/LayoutBuilderLock.php
View source
<?php
namespace Drupal\layout_builder_lock;
use Drupal\Core\Render\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
class LayoutBuilderLock implements TrustedCallbackInterface {
const NO_LOCK = [];
const LOCKED_BLOCK_UPDATE = 1;
const LOCKED_BLOCK_DELETE = 2;
const LOCKED_BLOCK_MOVE = 3;
const LOCKED_BLOCK_ADD = 4;
const LOCKED_SECTION_CONFIGURE = 5;
const LOCKED_SECTION_BEFORE = 6;
const LOCKED_SECTION_AFTER = 7;
const LOCKED_SECTION_BLOCK_MOVE = 8;
protected static function hasManageLockDefaultPermission() {
return \Drupal::currentUser()
->hasPermission('manage lock settings on sections');
}
protected static function hasManageLockOverridesPermission() {
return \Drupal::currentUser()
->hasPermission('manage lock settings on overrides');
}
protected static function hasBypassLockSettingsPermission() {
return \Drupal::currentUser()
->hasPermission('bypass lock settings on layout overrides');
}
public static function preRender(array $element) {
$element['#attached']['library'][] = 'layout_builder_lock/edit';
$overridden = FALSE;
$section_storage = $element['#section_storage'];
if ($section_storage instanceof OverridesSectionStorageInterface && $section_storage
->isOverridden()) {
$overridden = TRUE;
}
if ($overridden && self::hasBypassLockSettingsPermission()) {
return $element;
}
if (!$overridden && self::hasManageLockDefaultPermission()) {
return $element;
}
$section_number = 0;
for ($i = 0; $i < $section_storage
->count(); $i++) {
$settings = [];
try {
$settings = array_filter($section_storage
->getSection($i)
->getThirdPartySetting('layout_builder_lock', 'lock', self::NO_LOCK));
} catch (\OutOfBoundsException $ignored) {
}
if (empty($settings)) {
continue;
}
$default_components = [];
try {
if ($section_storage instanceof OverridesSectionStorageInterface) {
$default_components = $section_storage
->getDefaultSectionStorage()
->getSection($i)
->getComponents();
}
else {
$default_components = $section_storage
->getSection($i)
->getComponents();
}
} catch (\OutOfBoundsException $ignored) {
}
$section_number = $i * 2 + 1;
if (!isset($element['layout_builder'][$section_number])) {
continue;
}
foreach ($element['layout_builder'][$section_number] as $name => $item) {
if (isset($settings[self::LOCKED_SECTION_BEFORE])) {
unset($element['layout_builder'][$section_number - 1]);
}
if (isset($settings[self::LOCKED_SECTION_AFTER])) {
unset($element['layout_builder'][$section_number + 1]);
}
if (isset($item['#url'])) {
if ($name == 'remove') {
unset($element['layout_builder'][$section_number][$name]);
}
if ($name == 'configure' && isset($settings[self::LOCKED_SECTION_CONFIGURE]) && !self::hasManageLockOverridesPermission()) {
unset($element['layout_builder'][$section_number][$name]);
}
}
elseif (isset($item['#layout'])) {
foreach (Element::children($item) as $region_key) {
$has_custom_block = FALSE;
foreach (Element::children($item[$region_key]) as $item_key) {
if ($item_key == 'layout_builder_add_block') {
if (isset($settings[self::LOCKED_BLOCK_ADD])) {
unset($element['layout_builder'][$section_number][$name][$region_key][$item_key]);
}
}
elseif (isset($item[$region_key][$item_key]['#contextual_links'])) {
if ($overridden && !isset($default_components[$item_key])) {
$has_custom_block = TRUE;
continue;
}
$allow_moving = TRUE;
$layout_builder_remove_block_operations = [];
$block_operations = [
'move',
'update',
'remove',
];
if (isset($settings[self::LOCKED_BLOCK_MOVE])) {
$allow_moving = FALSE;
unset($block_operations[array_search('move', $block_operations)]);
$layout_builder_remove_block_operations[] = 'layout_builder_block_move';
}
if (isset($settings[self::LOCKED_BLOCK_UPDATE])) {
unset($block_operations[array_search('update', $block_operations)]);
$layout_builder_remove_block_operations[] = 'layout_builder_block_update';
}
if (isset($settings[self::LOCKED_BLOCK_DELETE])) {
unset($block_operations[array_search('remove', $block_operations)]);
$layout_builder_remove_block_operations[] = 'layout_builder_block_remove';
}
if (count($block_operations) != 3) {
$element['layout_builder'][$section_number][$name][$region_key][$item_key]['#contextual_links']['layout_builder_block']['metadata']['operations'] = implode(':', $block_operations);
}
if (!empty($layout_builder_remove_block_operations)) {
$element['layout_builder'][$section_number][$name][$region_key][$item_key]['#contextual_links']['layout_builder_block']['metadata']['layout_builder_lock'] = implode(':', $layout_builder_remove_block_operations);
}
if (!$allow_moving) {
foreach ([
'layout-builder-block',
'js-layout-builder-block',
] as $class) {
if (isset($element['layout_builder'][$section_number][$name][$region_key][$item_key]['#attributes']['class']) && is_array($element['layout_builder'][$section_number][$name][$region_key][$item_key]['#attributes']['class'])) {
$key = array_search($class, $element['layout_builder'][$section_number][$name][$region_key][$item_key]['#attributes']['class']);
unset($element['layout_builder'][$section_number][$name][$region_key][$item_key]['#attributes']['class'][$key]);
}
}
$element['layout_builder'][$section_number][$name][$region_key][$item_key]['#attributes']['class'][] = 'layout-builder-block-locked';
}
}
}
if (isset($settings[self::LOCKED_SECTION_BLOCK_MOVE]) && !$has_custom_block) {
if (isset($element['layout_builder'][$section_number][$name][$region_key]['#attributes']['class']) && is_array($element['layout_builder'][$section_number][$name][$region_key]['#attributes']['class'])) {
$key = array_search('js-layout-builder-region', $element['layout_builder'][$section_number][$name][$region_key]['#attributes']['class']);
unset($element['layout_builder'][$section_number][$name][$region_key]['#attributes']['class'][$key]);
}
}
}
}
}
}
return $element;
}
public static function trustedCallbacks() {
return [
'preRender',
];
}
}