sites_group_overrides-1.x-dev/src/Plugin/SectionStorage/GroupRelationshipOverridesSectionStorage.php
src/Plugin/SectionStorage/GroupRelationshipOverridesSectionStorage.php
<?php
declare(strict_types=1);
namespace Drupal\sites_group_overrides\Plugin\SectionStorage;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
use Drupal\sites_group_overrides\SitesGroupOverridesServiceInterface;
/**
* Defines the 'group_relationship_overrides' section storage type.
*
* @SectionStorage(
* id = "group_relationship_overrides",
* weight = -30,
* handles_permission_check = TRUE,
* context_definitions = {
* "entity" = @ContextDefinition("entity", constraints = {
* "EntityHasField" = \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::FIELD_NAME,
* }),
* "view_mode" = @ContextDefinition("string", default_value = "default"),
* }
* )
*
* @internal
* Plugin classes are internal.
*/
class GroupRelationshipOverridesSectionStorage extends OverridesSectionStorage {
/**
* The group_relationship for the current entity.
*
* @var GroupRelationshipInterface|null
* The group_relationship.
*/
protected ?GroupRelationshipInterface $relationship = NULL;
/**
* {@inheritdoc}
*
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The layout tempstore repository.
* @param \Drupal\sites_group_overrides\SitesGroupOverridesServiceInterface $sitesGroupOverridesService
* The sites group override service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, SectionStorageManagerInterface $section_storage_manager, EntityRepositoryInterface $entity_repository, AccountInterface $current_user, protected LayoutTempstoreRepositoryInterface $layoutTempstoreRepository, protected SitesGroupOverridesServiceInterface $sitesGroupOverridesService) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $section_storage_manager, $entity_repository, $current_user);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('plugin.manager.layout_builder.section_storage'),
$container->get('entity.repository'),
$container->get('current_user'),
$container->get('layout_builder.tempstore_repository'),
$container->get('sites_group_overrides.service'),
);
}
/**
* {@inheritdoc}
*/
protected function getSectionList() {
// Return the section list from the relationship. Otherwise one can not add sections.
if (($relationship = $this->getRelationship()) && $field_name = $this->sitesGroupOverridesService->getTargetFieldName($relationship, OverridesSectionStorage::FIELD_NAME)) {
return $relationship->get($field_name);
}
return parent::getSectionList();
}
/**
* {@inheritdoc}
*/
public function getTempstoreKey() {
$key = parent::getTempstoreKey();
$entity = $this->getEntity();
if ($relationship = $this->getRelationship()) {
$group = $relationship->getGroup();
$key .= '.sites_group:' . $group->id();
}
return $key;
}
/**
* {@inheritdoc}
*/
public function save() {
// Save chnages on the relationship if we have a site override.
if (($relationship = $this->getRelationship()) && $this->sitesGroupOverridesService->getTargetFieldName($relationship, OverridesSectionStorage::FIELD_NAME)) {
$this->layoutTempstoreRepository->delete($this);
return $relationship->save();
}
parent::save();
}
/**
* Gets the group_relationship for the current entity.
*
* We want to cache this, to make sure we are always working on the same
* group_relationship when updating layouts.
*
* @return GroupRelationshipInterface|null
* The group_relationship.
*/
protected function getRelationship(): ?GroupRelationshipInterface {
if (!$this->relationship) {
$entity = $this->getEntity();
if ($relationship = $this->sitesGroupOverridesService->getRelationship($entity)) {
$this->relationship = $relationship;
}
}
return $this->relationship;
}
}
