Skip to content
Snippets Groups Projects

Issue #3365669: Support Layout Builder Restrictions

1 file
+ 85
1
Compare changes
  • Side-by-side
  • Inline
@@ -5,6 +5,7 @@ namespace Drupal\layout_library\Plugin\SectionStorage;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
@@ -15,7 +16,9 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\field_ui\FieldUI;
use Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase;
use Drupal\layout_library\Entity\Layout;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
@@ -36,7 +39,7 @@ use Symfony\Component\Routing\RouteCollection;
* },
* )
*/
class Library extends SectionStorageBase implements ContainerFactoryPluginInterface {
class Library extends SectionStorageBase implements ContainerFactoryPluginInterface, ThirdPartySettingsInterface {
/**
* Entity type manager.
@@ -303,4 +306,85 @@ class Library extends SectionStorageBase implements ContainerFactoryPluginInterf
return $this->getContextValue('layout');
}
/**
* {@inheritdoc}
*/
public function setThirdPartySetting($module, $key, $value) {
$layout = $this->getLayout();
if ($layout instanceof ThirdPartySettingsInterface) {
$this->getEntityDisplayFromLayout($layout)->setThirdPartySetting($module, $key, $value);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getThirdPartySetting($module, $key, $default = NULL): mixed {
$layout = $this->getLayout();
if ($layout instanceof ThirdPartySettingsInterface) {
return $this->getEntityDisplayFromLayout($layout)->getThirdPartySetting($module, $key, $default);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getThirdPartySettings($module): array {
$layout = $this->getLayout();
if ($layout instanceof ThirdPartySettingsInterface) {
// Inherit 3rd part settings from the LB entity view mode.
// This implicitly adds support for LB Restrictions.
// See EntityViewModeRestriction::alterBlockDefinitions.
return $this->getEntityDisplayFromLayout($layout)->getThirdPartySettings($module);
}
return [];
}
/**
* {@inheritdoc}
*/
public function unsetThirdPartySetting($module, $key) {
$layout = $this->getLayout();
if ($layout instanceof ThirdPartySettingsInterface) {
$this->getEntityDisplayFromLayout($layout)->unsetThirdPartySetting($module, $key);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getThirdPartyProviders(): array {
$layout = $this->getLayout();
if ($layout instanceof ThirdPartySettingsInterface) {
return $this->getEntityDisplayFromLayout($layout)->getThirdPartyProviders();
}
return [];
}
/**
* Returns the LB entity view display.
*
* @param \Drupal\layout_library\Entity\Layout $layout
* The layout.
*
* @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
* The LB entity view display.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getEntityDisplayFromLayout(Layout $layout): LayoutEntityDisplayInterface {
// Reconsider the view mode when
// https://www.drupal.org/project/drupal/issues/2907413 gets resolved.
$id = sprintf('%s.%s.default', $layout->getTargetEntityType(), $layout->getTargetBundle());
$display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load($id);
assert($display instanceof LayoutEntityDisplayInterface);
return $display;
}
}
Loading