bee_hotel-1.x-dev/src/Access/BeeHotelBookThisUnitAccessCheck.php
src/Access/BeeHotelBookThisUnitAccessCheck.php
<?php
namespace Drupal\bee_hotel\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Validate access to the unti entity.
*
* @package Drupal\bee_hotel
*/
class BeeHotelBookThisUnitAccessCheck implements AccessInterface {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Drupal configuration service container.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Constructs a BeeHotelBookThisUnitAccessCheck object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory.
*/
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactory $config_factory) {
$this->entityTypeManager = $entity_manager;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('config.factory')
);
}
/**
* Check access to the unit entity.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
* @param \Drupal\node\Entity\Node $node
* A unit node.
*
* @return string
* A \Drupal\Core\Access\AccessInterface constant value.
*/
public function access(AccountInterface $account, Node $node = NULL) {
if (isset($node)) {
$bee_hotel_settings = $this->configFactory->getEditable('node.type.' . $node->bundle())->get('bee');
if (isset($bee_hotel_settings['bookable']) && $bee_hotel_settings['bookable']) {
// Everybody is welcome to book my units :)
return AccessResult::allowed();
}
}
return AccessResult::forbidden();
}
}
