You are here

class EckEntityCreateAccessCheck in Entity Construction Kit (ECK) 8

Determines access for ECK entity add page.

Hierarchy

Expanded class hierarchy of EckEntityCreateAccessCheck

1 string reference to 'EckEntityCreateAccessCheck'
eck.services.yml in ./eck.services.yml
eck.services.yml
1 service uses EckEntityCreateAccessCheck
access_check.eck_entity.add in ./eck.services.yml
Drupal\eck\Access\EckEntityCreateAccessCheck

File

src/Access/EckEntityCreateAccessCheck.php, line 14

Namespace

Drupal\eck\Access
View source
class EckEntityCreateAccessCheck implements AccessInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs an EckEntityCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Checks access to the eck entity add page for the entity bundle type.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param \Drupal\eck\EckEntityTypeInterface $eck_entity_type
   *   The entity type.
   * @param string $eck_entity_bundle
   *   (optional) The entity type bundle.
   *
   * @return bool|AccessResult|\Drupal\Core\Access\AccessResultInterface
   *   A \Drupal\Core\Access\AccessInterface constant value.
   */
  public function access(AccountInterface $account, EckEntityTypeInterface $eck_entity_type, $eck_entity_bundle = NULL) {
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler($eck_entity_type
      ->id());
    if (!empty($eck_entity_bundle)) {
      return $access_control_handler
        ->createAccess($eck_entity_bundle, $account, [], TRUE);
    }

    // Get the entity type bundles.
    $bundles = $this->entityTypeManager
      ->getStorage($eck_entity_type
      ->id() . '_type')
      ->loadMultiple();

    // If checking whether an entity of any type may be created.
    foreach ($bundles as $eck_entity_bundle) {
      if (($access = $access_control_handler
        ->createAccess($eck_entity_bundle
        ->id(), $account, [], TRUE)) && $access
        ->isAllowed()) {
        return $access;
      }
    }

    // No opinion.
    return AccessResult::neutral();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EckEntityCreateAccessCheck::$entityTypeManager protected property The entity type manager.
EckEntityCreateAccessCheck::access public function Checks access to the eck entity add page for the entity bundle type.
EckEntityCreateAccessCheck::__construct public function Constructs an EckEntityCreateAccessCheck object.