ParagraphAccessControlHandler.php in Paragraphs 8
File
src/ParagraphAccessControlHandler.php
View source
<?php
namespace Drupal\paragraphs;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $configFactory;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type);
$this->configFactory = $config_factory;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'));
}
protected function checkAccess(EntityInterface $paragraph, $operation, AccountInterface $account) {
$config = $this->configFactory
->get('paragraphs.settings');
if ($operation === 'view') {
$access_result = AccessResult::allowedIf($paragraph
->isPublished() || $account
->hasPermission('view unpublished paragraphs') && $config
->get('show_unpublished'))
->addCacheableDependency($config);
}
else {
$access_result = AccessResult::allowed();
}
if ($paragraph
->getParentEntity() != NULL) {
$operation = $operation == 'delete' ? 'update' : $operation;
if ($paragraph
->getParentEntity()
->getEntityTypeId() != 'paragraphs_library_item') {
$parent_access = $paragraph
->getParentEntity()
->access($operation, $account, TRUE);
$access_result = $access_result
->andIf($parent_access);
}
}
return $access_result;
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
if (\Drupal::requestStack()
->getCurrentRequest()
->getRequestFormat() === 'html') {
return AccessResult::allowed()
->addCacheContexts([
'request_format',
]);
}
return AccessResult::neutral()
->addCacheContexts([
'request_format',
]);
}
}