You are here

class PreviewEnabledAccessCheck in Preview Link 8

Same name and namespace in other branches
  1. 2.x src/Access/PreviewEnabledAccessCheck.php \Drupal\preview_link\Access\PreviewEnabledAccessCheck
  2. 2.0.x src/Access/PreviewEnabledAccessCheck.php \Drupal\preview_link\Access\PreviewEnabledAccessCheck

Preview link access check.

Hierarchy

Expanded class hierarchy of PreviewEnabledAccessCheck

1 string reference to 'PreviewEnabledAccessCheck'
preview_link.services.yml in ./preview_link.services.yml
preview_link.services.yml
1 service uses PreviewEnabledAccessCheck
access_check.preview_enabled in ./preview_link.services.yml
Drupal\preview_link\Access\PreviewEnabledAccessCheck

File

src/Access/PreviewEnabledAccessCheck.php, line 15

Namespace

Drupal\preview_link\Access
View 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) {

    // 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) {
    $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

Namesort descending Modifiers Type Description Overrides
PreviewEnabledAccessCheck::$config protected property The module settings.
PreviewEnabledAccessCheck::access public function Checks access to both the generate route and the preview route.
PreviewEnabledAccessCheck::entityTypeAndBundleEnabled protected function Check if the entity type and bundle are enabled.
PreviewEnabledAccessCheck::__construct public function PreviewEnabledAccessCheck constructor.