ViewmodepagePattern.php in View Mode Page 8.3
File
src/Entity/ViewmodepagePattern.php
View source
<?php
namespace Drupal\view_mode_page\Entity;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\view_mode_page\ViewmodepagePatternInterface;
class ViewmodepagePattern extends ConfigEntityBase implements ViewmodepagePatternInterface {
protected $id;
protected $label;
protected $type;
protected $aliasTypeCollection;
protected $pattern;
protected $view_mode;
protected $selection_criteria = [];
protected $selection_logic = 'and';
protected $weight = 0;
protected $relationships = [];
protected $selectionConditionCollection;
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$criteria = [];
foreach ($this
->getSelectionConditions() as $id => $condition) {
$criteria[$id] = $condition
->getConfiguration();
}
$this->selection_criteria = $criteria;
\Drupal::service('cache.data')
->deleteAll();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
\Drupal::service('cache.data')
->deleteAll();
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->calculatePluginDependencies($this
->getAliasType());
foreach ($this
->getSelectionConditions() as $instance) {
$this
->calculatePluginDependencies($instance);
}
return $this
->getDependencies();
}
public function getPattern() {
return $this->pattern;
}
public function getPatternRegex() {
$pattern = $this
->getPattern();
$pattern = preg_replace('!/+!', '/', $pattern);
$pattern = trim($pattern, '/');
$patternArray = explode('/', $pattern);
$patternRegex = '!^';
foreach ($patternArray as $patternPart) {
if ($patternPart == '%') {
$patternRegex .= '/(.*)';
}
else {
$patternRegex .= '/' . preg_quote($patternPart, '!');
}
}
$patternRegex .= '$!';
return $patternRegex;
}
public function setPattern($pattern) {
$this->pattern = $pattern;
return $this;
}
public function getViewMode() {
return $this->view_mode;
}
public function getViewModeLabel() {
$view_mode = $this
->getViewMode();
if ($entity_type_id = $this
->getAliasType()
->getDerivativeId()) {
$entity_display_repository = \Drupal::service('entity_display.repository');
$view_modes = $entity_display_repository
->getViewModeOptions($entity_type_id);
if (!empty($view_modes[$view_mode])) {
return $view_modes[$view_mode];
}
}
return $view_mode;
}
public function getType() {
return $this->type;
}
public function getAliasType() {
if (!$this->aliasTypeCollection) {
$this->aliasTypeCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('view_mode_page.manager.alias_type'), $this
->getType(), [
'default' => $this
->getPattern(),
]);
}
return $this->aliasTypeCollection
->get($this
->getType());
}
public function getWeight() {
return $this->weight;
}
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
public function getSelectionConditions() {
if (!$this->selectionConditionCollection) {
$this->selectionConditionCollection = new ConditionPluginCollection(\Drupal::service('plugin.manager.condition'), $this
->get('selection_criteria'));
}
return $this->selectionConditionCollection;
}
public function addSelectionCondition(array $configuration) {
$configuration['uuid'] = $this
->uuidGenerator()
->generate();
$this
->getSelectionConditions()
->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
public function getSelectionCondition($condition_id) {
return $this
->getSelectionConditions()
->get($condition_id);
}
public function removeSelectionCondition($condition_id) {
$this
->getSelectionConditions()
->removeInstanceId($condition_id);
return $this;
}
public function getSelectionLogic() {
return $this->selection_logic;
}
public function getContexts() {
$contexts = $this
->getAliasType()
->getContexts();
foreach ($this
->getRelationships() as $token => $definition) {
$resolver = \Drupal::service('ctools.typed_data.resolver');
$context = $resolver
->convertTokenToContext($token, $contexts);
$context_definition = $context
->getContextDefinition();
if (!empty($definition['label'])) {
$context_definition
->setLabel($definition['label']);
}
$contexts[$token] = $context;
}
return $contexts;
}
public function hasRelationship($token) {
return isset($this->relationships[$token]);
}
public function addRelationship($token, $label = NULL) {
if (!$this
->hasRelationship($token)) {
$this->relationships[$token] = [
'label' => $label,
];
}
return $this;
}
public function replaceRelationship($token, $label) {
if ($this
->hasRelationship($token)) {
$this->relationships[$token] = [
'label' => $label,
];
}
return $this;
}
public function removeRelationship($token) {
unset($this->relationships[$token]);
return $this;
}
public function getRelationships() {
return $this->relationships;
}
public function applies(EntityInterface $entity) {
if ($this
->getAliasType()
->applies($entity)) {
$definitions = $this
->getAliasType()
->getContextDefinitions();
if (count($definitions) > 1) {
throw new \Exception("Alias types do not support more than one context.");
}
$keys = array_keys($definitions);
$this
->getAliasType()
->setContextValue($keys[0], $entity);
$contexts = $this
->getContexts();
$context_handler = \Drupal::service('context.handler');
$conditions = $this
->getSelectionConditions();
foreach ($conditions as $condition) {
if ($condition instanceof ContextAwarePluginInterface) {
try {
$context_handler
->applyContextMapping($condition, $contexts);
} catch (ContextException $e) {
watchdog_exception('view_mode_page', $e);
return FALSE;
}
}
$result = $condition
->execute();
if ($this
->getSelectionLogic() == 'and' && !$result) {
return FALSE;
}
elseif ($this
->getSelectionLogic() == 'or' && $result) {
return TRUE;
}
}
return TRUE;
}
return FALSE;
}
}