View source
<?php
namespace Drupal\dashboards\Plugin\SectionStorage;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\dashboards\Entity\Dashboard;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_builder\Entity\SampleEntityGeneratorInterface;
use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase;
class DashboardSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, ThirdPartySettingsInterface {
protected $entityTypeManager;
protected $entityBundleInfo;
protected $sampleEntityGenerator;
protected $account;
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_type.bundle.info'), $container
->get('layout_builder.sample_entity_generator'), $container
->get('current_user'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_bundle_info, SampleEntityGeneratorInterface $sample_entity_generator, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityBundleInfo = $entity_bundle_info;
$this->sampleEntityGenerator = $sample_entity_generator;
$this->account = $current_user;
}
protected function getDashboard() {
return $this
->getContextValue(Dashboard::CONTEXT_TYPE);
}
protected function getSectionList() {
return $this
->getDashboard();
}
public function getStorageId() {
return $this
->getDashboard()
->id();
}
public function getSectionListFromId($id) {
@trigger_error('\\Drupal\\layout_builder\\SectionStorageInterface::getSectionListFromId() is deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. The section list should be derived from context. See https://www.drupal.org/node/3016262', E_USER_DEPRECATED);
return $this->entityTypeManager
->getStorage('dashboard')
->load($id);
}
public function buildRoutes(RouteCollection $collection) {
$requirements = [];
$this
->buildLayoutRoutes($collection, $this
->getPluginDefinition(), 'dashboards/{dashboard}/layout', [
'parameters' => [
'dashboard' => [
'type' => 'entity:dashboard',
],
],
], $requirements, [
'_admin_route' => FALSE,
], '', 'dashboard');
}
public function getRedirectUrl() {
return Url::fromRoute('entity.dashboard.canonical', [
'dashboard' => $this
->getDashboard()
->id(),
]);
}
public function getLayoutBuilderUrl($rel = 'view') {
return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$rel}", [
'dashboard' => $this
->getDashboard()
->id(),
]);
}
public function extractIdFromRoute($value, $definition, $name, array $defaults) {
throw new \Exception(new TranslatableMarkup('This method is deprecated in 8.7.0'));
}
public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {
$contexts = [];
$id = !empty($value) ? $value : (!empty($defaults['dashboard']) ? $defaults['dashboard'] : NULL);
if ($id && ($entity = $this->entityTypeManager
->getStorage('dashboard')
->load($id))) {
$contexts[Dashboard::CONTEXT_TYPE] = EntityContext::fromEntity($entity);
}
return $contexts;
}
public function label() {
return $this
->getDashboard()
->label();
}
public function save() {
return $this
->getDashboard()
->save();
}
public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
$entity = $this
->getContextValue(Dashboard::CONTEXT_TYPE);
return !$entity
->isOverriden();
}
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
if (!$account) {
$account = $this->account;
}
$result = AccessResult::allowedIfHasPermission($account, 'administer dashboards');
if ($return_as_object) {
return $result;
}
return $result
->isAllowed();
}
public function setThirdPartySetting($module, $key, $value) {
$this
->getDashboard()
->setThirdPartySetting($module, $key, $value);
return $this;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
return $this
->getDashboard()
->getThirdPartySetting($module, $key, $default);
}
public function getThirdPartySettings($module) {
return $this
->getDashboard()
->getThirdPartySettings($module);
}
public function unsetThirdPartySetting($module, $key) {
$this
->getDashboard()
->unsetThirdPartySetting($module, $key);
return $this;
}
public function getThirdPartyProviders() {
return $this
->getDashboard()
->getThirdPartyProviders();
}
}