View source
<?php
namespace Drupal\page_manager\Entity;
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\page_manager\Context\ContextDefinitionFactory;
use Drupal\page_manager\Event\PageManagerContextEvent;
use Drupal\page_manager\Event\PageManagerEvents;
use Drupal\page_manager\PageInterface;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\page_manager\PageVariantInterface;
class Page extends ConfigEntityBase implements PageInterface {
protected $id;
protected $label;
protected $description;
protected $path;
protected $variants;
protected $contexts = [];
protected $access_conditions = [];
protected $access_logic = 'and';
protected $accessConditionCollection;
protected $use_admin_theme;
protected $parameters = [];
public function getDescription() {
return $this->description;
}
public function getPath() {
return $this->path;
}
public function usesAdminTheme() {
return isset($this->use_admin_theme) ? $this->use_admin_theme : strpos($this
->getPath(), '/admin/') === 0;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
static::routeBuilder()
->setRebuildNeeded();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
static::routeBuilder()
->setRebuildNeeded();
}
protected static function routeBuilder() {
return \Drupal::service('router.builder');
}
protected function variantStorage() {
return \Drupal::service('entity_type.manager')
->getStorage('page_variant');
}
public function getPluginCollections() {
return [
'access_conditions' => $this
->getAccessConditions(),
];
}
public function getAccessConditions() {
if (!$this->accessConditionCollection) {
$this->accessConditionCollection = new ConditionPluginCollection(\Drupal::service('plugin.manager.condition'), $this
->get('access_conditions'));
}
return $this->accessConditionCollection;
}
public function addAccessCondition(array $configuration) {
$configuration['uuid'] = $this
->uuidGenerator()
->generate();
$this
->getAccessConditions()
->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
public function getAccessCondition($condition_id) {
return $this
->getAccessConditions()
->get($condition_id);
}
public function removeAccessCondition($condition_id) {
$this
->getAccessConditions()
->removeInstanceId($condition_id);
return $this;
}
public function getAccessLogic() {
return $this->access_logic;
}
public function getParameters() {
$names = $this
->getParameterNames();
if ($names) {
return array_intersect_key($this->parameters, array_flip($names));
}
return [];
}
public function getParameter($name) {
if ($this
->hasParameter($name)) {
return $this->parameters[$name];
}
return NULL;
}
public function hasParameter($name) {
return isset($this->parameters[$name]);
}
public function setParameter($name, $type, $label = '') {
$this->parameters[$name] = [
'machine_name' => $name,
'type' => $type,
'label' => $label,
];
$this->contexts = [];
foreach ($this
->getVariants() as $page_variant) {
$page_variant
->resetCollectedContexts();
}
return $this;
}
public function removeParameter($name) {
unset($this->parameters[$name]);
$this->contexts = [];
foreach ($this
->getVariants() as $page_variant) {
$page_variant
->resetCollectedContexts();
}
return $this;
}
public function getParameterNames() {
if (preg_match_all('|\\{(\\w+)\\}|', $this
->getPath(), $matches)) {
return $matches[1];
}
return [];
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$this
->filterParameters();
}
protected function filterParameters() {
$names = $this
->getParameterNames();
foreach ($this
->get('parameters') as $name => $parameter) {
if (empty($parameter['type']) || !in_array($name, $names)) {
$this
->removeParameter($name);
}
}
return $this;
}
public function addContext($name, ContextInterface $value) {
$this->contexts[$name] = $value;
return $this;
}
public function getContexts() {
$global_contexts = [
'current_user',
];
if (!$this->contexts) {
$this
->eventDispatcher()
->dispatch(PageManagerEvents::PAGE_CONTEXT, new PageManagerContextEvent($this));
foreach ($this
->getParameters() as $machine_name => $configuration) {
if (!isset($global_contexts[$machine_name])) {
if (!isset($this->contexts[$machine_name])) {
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
$context_definition = ContextDefinitionFactory::create($configuration['type'])
->setLabel($configuration['label']);
$context = new Context($context_definition);
$context
->addCacheableDependency($cacheability);
$this->contexts[$machine_name] = $context;
}
else {
$this->contexts[$machine_name]
->getContextDefinition()
->setDataType($configuration['type']);
if (!empty($configuration['label'])) {
$this->contexts[$machine_name]
->getContextDefinition()
->setLabel($configuration['label']);
}
}
}
}
}
return $this->contexts;
}
public function addVariant(PageVariantInterface $variant) {
if ($this->variants === NULL) {
$this
->getVariants();
}
$this->variants[$variant
->id()] = $variant;
$this
->sortVariants();
return $this;
}
public function getVariant($variant_id) {
$variants = $this
->getVariants();
if (!isset($variants[$variant_id])) {
throw new \UnexpectedValueException('The requested variant does not exist or is not associated with this page');
}
return $variants[$variant_id];
}
public function removeVariant($variant_id) {
$this
->getVariant($variant_id)
->delete();
unset($this->variants[$variant_id]);
return $this;
}
public function getVariants() {
if (!isset($this->variants)) {
$this->variants = [];
foreach ($this
->variantStorage()
->loadByProperties([
'page' => $this
->id(),
]) as $variant) {
$this->variants[$variant
->id()] = $variant;
}
$this
->sortVariants();
}
return $this->variants;
}
protected function sortVariants() {
if (isset($this->variants)) {
@uasort($this->variants, [
$this,
'variantSortHelper',
]);
}
}
public function variantSortHelper($a, $b) {
$a_weight = $a
->getWeight();
$b_weight = $b
->getWeight();
if ($a_weight == $b_weight) {
return 0;
}
return $a_weight < $b_weight ? -1 : 1;
}
protected function eventDispatcher() {
return \Drupal::service('event_dispatcher');
}
public function __sleep() {
$vars = parent::__sleep();
if (($key = array_search('contexts', $vars)) !== FALSE) {
unset($vars[$key]);
}
return $vars;
}
protected function urlRouteParameters($rel) {
if ($rel == 'edit-form') {
$uri_route_parameters = [];
$uri_route_parameters['machine_name'] = $this
->id();
$uri_route_parameters['step'] = 'general';
return $uri_route_parameters;
}
return parent::urlRouteParameters($rel);
}
}