OverridesSectionStorage.php in Layout Builder Symmetric Translations 8
File
src/Plugin/SectionStorage/OverridesSectionStorage.php
View source
<?php
namespace Drupal\layout_builder_st\Plugin\SectionStorage;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage as CoreOverridesSectionStorage;
use Drupal\layout_builder_st\TranslatableSectionStorageInterface;
final class OverridesSectionStorage extends CoreOverridesSectionStorage implements TranslatableSectionStorageInterface {
const TRANSLATED_CONFIGURATION_FIELD_NAME = 'layout_builder__translation';
protected function handleTranslationAccess(AccessResult $result, $operation, AccountInterface $account) {
$entity = $this
->getEntity();
$field_config = $entity
->getFieldDefinition(static::FIELD_NAME)
->getConfig($entity
->bundle());
return $result
->andIf(AccessResult::allowedIf($this
->isDefaultTranslation() || $entity instanceof TranslatableInterface && $this
->isOverridden() && !$field_config
->isTranslatable()))
->addCacheableDependency($entity)
->addCacheableDependency($field_config);
}
protected function isTranslatable() {
$entity = $this
->getEntity();
if ($entity instanceof TranslatableInterface) {
return $entity
->isTranslatable();
}
return FALSE;
}
public function isDefaultTranslation() {
if ($this
->isTranslatable()) {
$entity = $this
->getEntity();
return $entity
->isDefaultTranslation();
}
return TRUE;
}
public function setTranslatedComponentConfiguration($uuid, array $configuration) {
if (!$this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->isEmpty()) {
$translation_settings = $this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->getValue()[0];
}
$translation_settings['value']['components'][$uuid] = $configuration;
$this
->getEntity()
->set(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME, [
$translation_settings,
]);
}
public function getTranslatedComponentConfiguration($uuid) {
if ($this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->isEmpty()) {
return [];
}
$translation_settings = $this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->getValue()[0];
return isset($translation_settings['value']['components'][$uuid]) ? $translation_settings['value']['components'][$uuid] : [];
}
public function getTranslatedConfiguration() {
if ($this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->isEmpty()) {
return [];
}
return $this
->getEntity()
->get(OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME)
->getValue()[0];
}
public function getTranslationLanguage() {
if (!$this
->isDefaultTranslation()) {
return $this
->getEntity()
->language();
}
return NULL;
}
public function getSourceLanguage() {
if (!$this
->isDefaultTranslation()) {
$entity = $this
->getEntity();
return $entity
->getUntranslated()
->language();
}
return NULL;
}
}