class PreviewEnabledAccessCheck in Preview Link 2.x
Same name and namespace in other branches
- 8 src/Access/PreviewEnabledAccessCheck.php \Drupal\preview_link\Access\PreviewEnabledAccessCheck
- 2.0.x src/Access/PreviewEnabledAccessCheck.php \Drupal\preview_link\Access\PreviewEnabledAccessCheck
Preview link access check.
Hierarchy
- class \Drupal\preview_link\Access\PreviewEnabledAccessCheck implements AccessInterface
Expanded class hierarchy of PreviewEnabledAccessCheck
1 string reference to 'PreviewEnabledAccessCheck'
1 service uses PreviewEnabledAccessCheck
File
- src/
Access/ PreviewEnabledAccessCheck.php, line 18
Namespace
Drupal\preview_link\AccessView source
class PreviewEnabledAccessCheck implements AccessInterface {
/**
* The module settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* PreviewEnabledAccessCheck constructor.
*/
public function __construct(ConfigFactoryInterface $configFactory) {
$this->config = $configFactory
->get('preview_link.settings');
}
/**
* Checks access to both the generate route and the preview route.
*/
public function access(Route $route, RouteMatchInterface $route_match) : AccessResultInterface {
// Get the entity for both the preview route and the generate preview link
// route.
if ($entity_type_id = $route
->getOption('preview_link.entity_type_id')) {
$entity = $route_match
->getParameter($route
->getOption('preview_link.entity_type_id'));
}
else {
$entity = $route_match
->getParameter('entity');
}
return AccessResult::allowedIf($this
->entityTypeAndBundleEnabled($entity))
->addCacheableDependency($entity)
->addCacheContexts([
'route',
])
->addCacheableDependency($this->config);
}
/**
* Check if the entity type and bundle are enabled.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return bool
* TRUE if enabled, FALSE otherwise.
*/
protected function entityTypeAndBundleEnabled(EntityInterface $entity) : bool {
$enabled_entity_types = $this->config
->get('enabled_entity_types');
// If no entity types are specified, fallback to allowing all.
if (empty($enabled_entity_types)) {
return TRUE;
}
// If the entity type exists in the configuration object.
if (isset($enabled_entity_types[$entity
->getEntityTypeId()])) {
$enabled_bundles = $enabled_entity_types[$entity
->getEntityTypeId()];
// If no bundles were specified, assume all bundles are enabled.
if (empty($enabled_bundles)) {
return TRUE;
}
// Otherwise fallback to requiring the specific bundle.
if (in_array($entity
->bundle(), $enabled_bundles)) {
return TRUE;
}
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PreviewEnabledAccessCheck:: |
protected | property | The module settings. | |
PreviewEnabledAccessCheck:: |
public | function | Checks access to both the generate route and the preview route. | |
PreviewEnabledAccessCheck:: |
protected | function | Check if the entity type and bundle are enabled. | |
PreviewEnabledAccessCheck:: |
public | function | PreviewEnabledAccessCheck constructor. |