public function access(AccountInterface $account, FlagInterface $flag, $entity_id) {
    if (in_array($flag->id(), $this->socialContentReport->getReportFlagTypes())) {
      // Make sure user is allowed to use the flag.
      if (!$account->hasPermission('flag ' . $flag->id())) {
        return AccessResult::forbidden();
      }

      $entity = $this->entityTypeManager->getStorage($flag->getFlaggableEntityTypeId())
        ->load($entity_id);

      $flagged = $flag->isFlagged($entity, $account);

      // If the user already flagged the content they aren't allowed to do it
      // again.
      return $flagged ? AccessResult::forbidden() : AccessResult::allowed();
    }

    return AccessResult::neutral();
  }
public function access(AccountInterface $account, FlagInterface $flag, $entity_id) {
    if (in_array($flag->id(), $this->socialContentReport->getReportFlagTypes())) {
      // Make sure user is allowed to use the flag.
      if (!$account->hasPermission('flag ' . $flag->id())) {
        return AccessResult::forbidden();
      }

      $entity = $this->entityTypeManager->getStorage($flag->getFlaggableEntityTypeId())
        ->load($entity_id);

      $flagged = $flag->isFlagged($entity, $account);

      // If the user already flagged the content they aren't allowed to do it
      // again.
      return $flagged ? AccessResult::forbidden() : AccessResult::allowed();
    }

    return AccessResult::neutral();
  }
public function access ( AccountInterface $ account , FlagInterface $ flag , $ entity_id ) { if ( in_array ( $ flag -> id ( ) , $ this -> socialContentReport -> getReportFlagTypes ( ) ) ) { // Make sure user is allowed to use the flag. if ( ! $ account -> hasPermission ( 'flag ' . $ flag -> id ( ) ) ) { return AccessResult :: forbidden ( ) ; } $ entity = $ this -> entityTypeManager -> getStorage ( $ flag -> getFlaggableEntityTypeId ( ) ) -> load ( $ entity_id ) ; $ flagged = $ flag -> isFlagged ( $ entity , $ account ) ; // If the user already flagged the content they aren't allowed to do it // again. return $ flagged ? AccessResult :: forbidden ( ) : AccessResult :: allowed ( ) ; } return AccessResult :: neutral ( ) ; }
Checks if user is allowed to flag.

@param \Drupal\Core\Session\AccountInterface $account
Run access checks for this account.
@param \Drupal\flag\FlagInterface $flag
The flag type.
@param int $entity_id
The entity ID which is being reported.

@return \Drupal\Core\Access\AccessResult
Allowed if user may use the flag and hasn't reported it yet.
Checks if user is allowed to flag .