namespace Drupal\layout_builder\Plugin\SectionStorage;
+use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
-use Drupal\Core\Plugin\Context\Context;
-use Drupal\Core\Plugin\Context\ContextDefinition;
-use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\Core\Plugin\Context\EntityContext;
+use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\OverridesSectionStorageInterface;
* experimental modules and development releases of contributed modules.
* See https://www.drupal.org/core/experimental for more information.
*/
-class OverridesSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, OverridesSectionStorageInterface {
+class OverridesSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, OverridesSectionStorageInterface, SectionStorageLocalTaskProviderInterface {
+
+ /**
+ * The field name used by this storage.
+ *
+ * @var string
+ */
+ const FIELD_NAME = 'layout_builder__layout';
/**
* The entity type manager.
if (strpos($id, '.') !== FALSE) {
list($entity_type_id, $entity_id) = explode('.', $id, 2);
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);
- if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) {
- return $entity->get('layout_builder__layout');
+ if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) {
+ return $entity->get(static::FIELD_NAME);
}
}
throw new \InvalidArgumentException(sprintf('The "%s" ID for the "%s" section storage type is invalid', $id, $this->getStorageType()));
}
}
+ /**
+ * {@inheritdoc}
+ */
+ public function buildLocalTasks($base_plugin_definition) {
+ $local_tasks = [];
+ foreach ($this->getEntityTypes() as $entity_type_id => $entity_type) {
+ $local_tasks["layout_builder.overrides.$entity_type_id.view"] = $base_plugin_definition + [
+ 'route_name' => "layout_builder.overrides.$entity_type_id.view",
+ 'weight' => 15,
+ 'title' => $this->t('Layout'),
+ 'base_route' => "entity.$entity_type_id.canonical",
+ 'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
+ ];
+ $local_tasks["layout_builder.overrides.$entity_type_id.save"] = $base_plugin_definition + [
+ 'route_name' => "layout_builder.overrides.$entity_type_id.save",
+ 'title' => $this->t('Save Layout'),
+ 'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
+ 'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
+ ];
+ $local_tasks["layout_builder.overrides.$entity_type_id.cancel"] = $base_plugin_definition + [
+ 'route_name' => "layout_builder.overrides.$entity_type_id.cancel",
+ 'title' => $this->t('Cancel Layout'),
+ 'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
+ 'weight' => 5,
+ 'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
+ ];
+ // @todo This link should be conditionally displayed, see
+ // https://www.drupal.org/node/2917777.
+ $local_tasks["layout_builder.overrides.$entity_type_id.revert"] = $base_plugin_definition + [
+ 'route_name' => "layout_builder.overrides.$entity_type_id.revert",
+ 'title' => $this->t('Revert to defaults'),
+ 'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
+ 'weight' => 10,
+ 'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
+ ];
+ }
+ return $local_tasks;
+ }
+
/**
* Determines if this entity type's ID is stored as an integer.
*
* {@inheritdoc}
*/
public function getDefaultSectionStorage() {
- return LayoutBuilderEntityViewDisplay::collectRenderDisplay($this->getEntity(), 'default');
+ // @todo Expand to work for all view modes in
+ // https://www.drupal.org/node/2907413.
+ return LayoutBuilderEntityViewDisplay::collectRenderDisplay($this->getEntity(), 'full');
}
/**
/**
* {@inheritdoc}
*/
- public function getLayoutBuilderUrl() {
+ public function getLayoutBuilderUrl($rel = 'view') {
$entity = $this->getEntity();
$route_parameters[$entity->getEntityTypeId()] = $entity->id();
- return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getEntity()->getEntityTypeId()}.view", $route_parameters);
+ return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getEntity()->getEntityTypeId()}.$rel", $route_parameters);
}
/**
*/
public function getContexts() {
$entity = $this->getEntity();
- // @todo Use EntityContextDefinition after resolving
- // https://www.drupal.org/node/2932462.
- $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity);
+ $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
return $contexts;
}
return $this->getEntity()->save();
}
+ /**
+ * {@inheritdoc}
+ */
+ public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
+ $default_section_storage = $this->getDefaultSectionStorage();
+ $result = AccessResult::allowedIf($default_section_storage->isLayoutBuilderEnabled())->addCacheableDependency($default_section_storage);
+ return $return_as_object ? $result : $result->isAllowed();
+ }
+
}