View source
<?php
namespace Drupal\panelizer\Plugin\PanelsStorage;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\ctools\Context\AutomaticContext;
use Drupal\panelizer\Exception\PanelizerException;
use Drupal\panelizer\PanelizerInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\panels\Storage\PanelsStorageBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelizerDefaultPanelsStorage extends PanelsStorageBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $panelizer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PanelizerInterface $panelizer) {
$this->entityTypeManager = $entity_type_manager;
$this->panelizer = $panelizer;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
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('panelizer'));
}
protected function parseId($id) {
list($entity_type_id, $part_two, $view_mode, $name) = explode(':', $id);
if (strpos($entity_type_id, '*') === 0) {
$entity_type_id = substr($entity_type_id, 1);
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if ($entity = $storage
->load($part_two)) {
$bundle = $entity
->bundle();
}
else {
throw new PanelizerException("Unable to load {$entity_type_id} with id {$part_two}");
}
}
else {
$entity = NULL;
$bundle = $part_two;
}
return [
$entity_type_id,
$bundle,
$view_mode,
$name,
$entity,
];
}
protected function getEntityContext($entity_type_id, EntityInterface $entity = NULL) {
$contexts = [];
$contexts['@panelizer.entity_context:entity'] = new AutomaticContext(new EntityContextDefinition('entity:' . $entity_type_id, NULL, TRUE), $entity);
return $contexts;
}
public function load($id) {
try {
list($entity_type_id, $bundle, $view_mode, $name, $entity) = $this
->parseId($id);
if ($panels_display = $this->panelizer
->getDefaultPanelsDisplay($name, $entity_type_id, $bundle, $view_mode)) {
$contexts = $this
->getEntityContext($entity_type_id, $entity);
$contexts = $contexts + $this->panelizer
->getDisplayStaticContexts($name, $entity_type_id, $bundle, $view_mode);
$panels_display
->setContexts($contexts);
return $panels_display;
}
} catch (PanelizerException $e) {
}
}
public function save(PanelsDisplayVariant $panels_display) {
$id = $panels_display
->getStorageId();
try {
list($entity_type_id, $bundle, $view_mode, $name) = $this
->parseId($id);
$this->panelizer
->setDefaultPanelsDisplay($name, $entity_type_id, $bundle, $view_mode, $panels_display);
} catch (PanelizerException $e) {
throw new \Exception("Couldn't find Panelizer default to store Panels display");
}
}
public function access($id, $op, AccountInterface $account) {
try {
list($entity_type_id, $bundle, $view_mode, $name) = $this
->parseId($id);
} catch (PanelizerException $e) {
return AccessResult::forbidden();
}
if ($panels_display = $this->panelizer
->getDefaultPanelsDisplay($name, $entity_type_id, $bundle, $view_mode)) {
if ($op == 'change layout') {
if ($this->panelizer
->hasDefaultPermission('change layout', $entity_type_id, $bundle, $view_mode, $name, $account)) {
return AccessResult::allowed();
}
}
else {
if ($op == 'read' || $this->panelizer
->hasDefaultPermission('change content', $entity_type_id, $bundle, $view_mode, $name, $account)) {
return AccessResult::allowed();
}
}
}
return AccessResult::forbidden();
}
}