View source
<?php
namespace Drupal\layout_builder_st\Element;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Element;
use Drupal\layout_builder\Element\LayoutBuilder as CoreLayoutbuilder;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\layout_builder_st\LayoutBuilderTranslatablePluginInterface;
use Drupal\layout_builder_st\TranslationsHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class LayoutBuilder extends CoreLayoutbuilder {
use TranslationsHelperTrait;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $layout_tempstore_repository, $messenger);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('layout_builder.tempstore_repository'), $container
->get('messenger'), $container
->get('entity_type.manager'));
}
protected function buildAddSectionLink(SectionStorageInterface $section_storage, $delta) {
$link = parent::buildAddSectionLink($section_storage, $delta);
$this
->setTranslationAcess($link, $section_storage);
return $link;
}
protected function buildAdministrativeSection(SectionStorageInterface $section_storage, $delta) {
$section_build = parent::buildAdministrativeSection($section_storage, $delta);
$this
->setTranslationAcess($section_build['remove'], $section_storage);
$this
->setTranslationAcess($section_build['configure'], $section_storage);
if (static::isTranslation($section_storage)) {
foreach (Element::children($section_build['layout-builder__section']) as $region) {
$region_build =& $section_build['layout-builder__section'][$region];
$this
->setTranslationAcess($region_build['layout_builder_add_block'], $section_storage);
foreach (Element::children($region_build) as $uuid) {
if (substr_count($uuid, '-') !== 4) {
continue;
}
if (($key = array_search('js-layout-builder-block', $region_build[$uuid]['#attributes']['class'])) !== FALSE) {
unset($region_build[$uuid]['#attributes']['class'][$key]);
}
if ($contextual_link_element = $this
->createContextualLinkElement($section_storage, $delta, $region, $uuid)) {
$region_build[$uuid]['#contextual_links'] = $contextual_link_element;
}
else {
unset($region_build[$uuid]['#contextual_links']);
}
}
}
}
return $section_build;
}
private function setTranslationAcess(array &$build, SectionStorageInterface $section_storage) {
$build['#access'] = !static::isTranslation($section_storage);
}
private function createContextualLinkElement(SectionStorageInterface $section_storage, $delta, $region, $uuid) {
$section = $section_storage
->getSection($delta);
$contextual_link_settings = [
'route_parameters' => [
'section_storage_type' => $section_storage
->getStorageType(),
'section_storage' => $section_storage
->getStorageId(),
'delta' => $delta,
'region' => $region,
'uuid' => $uuid,
],
];
if (static::isTranslation($section_storage)) {
$contextual_group = 'layout_builder_block_translation';
$component = $section
->getComponent($uuid);
$language = $section_storage
->getTranslationLanguage();
$contextual_link_settings['route_parameters']['langcode'] = $language
->getId();
$plugin = $component
->getPlugin();
if ($plugin instanceof DerivativeInspectionInterface && $plugin
->getBaseId() === 'inline_block') {
$configuration = $plugin
->getConfiguration();
$block = $this->entityTypeManager
->getStorage('block_content')
->loadRevision($configuration['block_revision_id']);
if ($block
->isTranslatable()) {
$contextual_group = 'layout_builder_inline_block_translation';
}
}
}
else {
$contextual_group = 'layout_builder_block';
$contextual_link_settings['metadata'] = [
'operations' => 'move:update:remove',
];
}
return [
$contextual_group => $contextual_link_settings,
];
}
private static function hasTranslatableConfiguration(SectionComponent $component) {
$plugin = $component
->getPlugin();
if ($plugin instanceof LayoutBuilderTranslatablePluginInterface) {
return $plugin
->hasTranslatableConfiguration();
}
elseif ($plugin instanceof ConfigurableInterface) {
$configuration = $plugin
->getConfiguration();
return !empty($configuration['label_display']) && !empty($configuration['label']);
}
return FALSE;
}
}