sites_group_overrides-1.x-dev/src/SitesGroupOverridesInlineBlockEntityOperations.php
src/SitesGroupOverridesInlineBlockEntityOperations.php
<?php
declare(strict_types=1);
namespace Drupal\sites_group_overrides;
use Drupal\Core\Entity\EntityInterface;
use Drupal\layout_builder\SectionComponent;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\layout_builder\InlineBlockUsageInterface;
use Drupal\layout_builder\InlineBlockEntityOperations;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
/**
* Extend the LB InlineBlockEntityOperations class for site overrides.
*/
class SitesGroupOverridesInlineBlockEntityOperations extends InlineBlockEntityOperations {
/**
* {@inheritdoc}
*
* @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $sitesGroupOverridesService
* The sites group overrides service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, InlineBlockUsageInterface $usage, SectionStorageManagerInterface $section_storage_manager, protected SitesGroupOverridesServiceInterface $sitesGroupOverridesService) {
parent::__construct($entity_type_manager, $usage, $section_storage_manager);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('inline_block.usage'),
$container->get('plugin.manager.layout_builder.section_storage'),
$container->get('sites_group_overrides.service'),
);
}
/**
* {@inheritdoc}
*/
protected function saveInlineBlockComponent(EntityInterface $entity, SectionComponent $component, $new_revision, $duplicate_blocks) {
// Duplicate blocks for new site overrides.
if ($entity instanceof GroupRelationshipInterface && $field_name = $this->sitesGroupOverridesService->getTargetFieldName($entity, OverridesSectionStorage::FIELD_NAME)) {
if ($entity->get($field_name)->isEmpty()) {
$duplicate_blocks = TRUE;
}
}
parent::saveInlineBlockComponent($entity, $component, $new_revision, $duplicate_blocks);
}
}