You are here

class ForwardAccessChecker in Forward 8.3

Same name and namespace in other branches
  1. 8 src/ForwardAccessChecker.php \Drupal\forward\ForwardAccessChecker
  2. 8.2 src/ForwardAccessChecker.php \Drupal\forward\ForwardAccessChecker

Defines a class for checking whether a Forward link or form can be diplayed within a given context.

Hierarchy

Expanded class hierarchy of ForwardAccessChecker

1 string reference to 'ForwardAccessChecker'
forward.services.yml in ./forward.services.yml
forward.services.yml
1 service uses ForwardAccessChecker
forward.access_checker in ./forward.services.yml
Drupal\forward\ForwardAccessChecker

File

src/ForwardAccessChecker.php, line 11

Namespace

Drupal\forward
View source
class ForwardAccessChecker implements ForwardAccessCheckerInterface {

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Constructs a ForwardAccessChecker object.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface
   *   The current user service.
   */
  public function __construct(AccountProxyInterface $current_user) {
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_user'));
  }

  /**
   * {@inheritdoc}
   */
  public function isAllowed(array $settings, EntityInterface $entity = NULL, $view_mode = NULL, $entity_type = NULL, $bundle = NULL) {

    // Full and default are synonymous.
    if ($view_mode == 'default') {
      $view_mode = 'full';
    }

    // Check permission.
    $show = $this->currentUser
      ->hasPermission('access forward');

    // Check view mode.
    if ($show && $view_mode) {
      $show = !empty($settings['forward_view_' . $view_mode]);
    }

    // Check entity type.
    if ($show) {
      if ($entity) {
        $entity_type = $entity
          ->getEntityTypeId();
      }
      $show = $entity_type ? !empty($settings['forward_entity_' . $entity_type]) : FALSE;
    }

    // Check entity bundle.
    if ($show) {
      if ($entity) {
        $bundle = $entity
          ->bundle();
      }
      $show = $entity_type && $bundle ? !empty($settings['forward_' . $entity_type . '_' . $bundle]) : FALSE;
    }
    return $show;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ForwardAccessChecker::$currentUser protected property The current user service.
ForwardAccessChecker::create public static function
ForwardAccessChecker::isAllowed public function Checks whether a Forward link or form can be displayed on a given entity and view mode. Overrides ForwardAccessCheckerInterface::isAllowed
ForwardAccessChecker::__construct public function Constructs a ForwardAccessChecker object.