Skip to content
Snippets Groups Projects
Commit bc5151be authored by Francesco Pesenti's avatar Francesco Pesenti
Browse files

Fix copy/paste, check access on paste, fix drop alter

parent dab32dda
No related branches found
Tags 7.98
1 merge request!5Fix copy/paste, check access on paste, fix drop alter
<?php
define('LB_PLUS_TEMPLATE_PLACEHOLDER', 'Template:');
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use Drupal\lb_plus\Entity\LbPlusBlockContentSettings;
......@@ -196,13 +194,25 @@ function lb_plus_form_block_content_type_form_submit($form, $form_state) {
* Implements hook_preprocess_HOOK().
*/
function lb_plus_preprocess_layout(&$variables) {
foreach (Element::children($variables['content']) as $index) {
$element = &$variables['content'][$index];
if (!empty($element['layout_builder_add_block']['link'])) {
$element['layout_builder_add_block']['link'] = [
'#type' => 'item',
'#markup' => t('Drop a block here to add it'),
];
// @todo: make this more safe.
$routes = [
'layout_builder.overrides.node.view',
'layout_builder.configure_section',
'lb_plus.paste_block',
'lb_plus.add_block',
'lb_plus.add_default_block',
];
$route_name = \Drupal::routeMatch()->getRouteName();
if (in_array($route_name, $routes)) {
foreach (Element::children($variables['content']) as $index) {
$element = &$variables['content'][$index];
if (!empty($element['layout_builder_add_block']['link'])) {
$element['layout_builder_add_block']['link'] = [
'#type' => 'item',
'#markup' => t('Drop a block here to add it'),
];
}
}
}
}
......@@ -398,15 +398,18 @@ class LbPlusBlockController extends ChooseBlockController implements ContainerI
public function pasteBlock(SectionStorageInterface $section_storage = NULL, $region = NULL, $delta = NULL, $plugin_id = NULL) {
$block = BlockContent::load($plugin_id);
if ($block && $region && $delta) {
$block_type_permissions = $this->config->get('block_type_permissions');
$layout = $section_storage->getSection($delta)->getLayout();
$layout_id = $layout->getPluginId();
$access = !empty($block_type_permissions[$layout_id][$block->bundle()]);
if ($block && $region && $delta !== FALSE && $access) {
$cloned_entity = $block->createDuplicate();
$label_key = \Drupal::entityTypeManager()
->getDefinition('block_content')
->getKey('label');
$new_label = str_replace(LB_PLUS_TEMPLATE_PLACEHOLDER, '', $block->label());
$cloned_entity->set($label_key, trim($new_label));
$cloned_entity->set($label_key, trim($block->label()));
$cloned_entity->setReusable();
if ($cloned_entity->save()) {
......@@ -414,10 +417,21 @@ class LbPlusBlockController extends ChooseBlockController implements ContainerI
$component = new SectionComponent($this->uuidGenerator->generate(), $region, ['id' => 'block_content:' . $cloned_entity->uuid()]);
$section->appendComponent($component);
$this->layoutTempstoreRepository->set($section_storage);
return $this->rebuildLayout($section_storage);
}
}
return $this->rebuildLayout($section_storage);
$response = $this->rebuildLayout($section_storage);
$block_type = BlockContentType::load($block->bundle());
$message = $this->t('The "@block_type" block type is not permitted for the "@section_type" layout section.', [
'@block_type' => $block_type->label(),
'@section_type' => $layout->getPluginDefinition()->getLabel(),
]);
$selector = 'form.layout-builder-form';
$response->addCommand(new MessageCommand($message, $selector, ['type' => 'error'], TRUE));
$response->addCommand(new ScrollTopCommand($selector));
return $response;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment