3 namespace Drupal\quickedit\Access;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\Access\AccessInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Entity\EntityInterface;
11 * Access check for in-place editing entity fields.
13 class QuickEditEntityFieldAccessCheck implements AccessInterface, QuickEditEntityFieldAccessCheckInterface {
16 * Checks Quick Edit access to the field.
18 * @param \Drupal\Core\Entity\EntityInterface $entity
19 * The entity containing the field.
20 * @param string $field_name
22 * @param string $langcode
24 * @param \Drupal\Core\Session\AccountInterface $account
25 * The currently logged in account.
27 * @return \Drupal\Core\Access\AccessResultInterface
30 * @todo Use the $account argument: https://www.drupal.org/node/2266809.
32 public function access(EntityInterface $entity, $field_name, $langcode, AccountInterface $account) {
33 if (!$this->validateRequestAttributes($entity, $field_name, $langcode)) {
34 return AccessResult::forbidden();
37 return $this->accessEditEntityField($entity, $field_name);
43 public function accessEditEntityField(EntityInterface $entity, $field_name) {
44 return $entity->access('update', NULL, TRUE)->andIf($entity->get($field_name)->access('edit', NULL, TRUE));
48 * Validates request attributes.
50 protected function validateRequestAttributes(EntityInterface $entity, $field_name, $langcode) {
51 // Validate the field name and language.
52 if (!$field_name || !$entity->hasField($field_name)) {
55 if (!$langcode || !$entity->hasTranslation($langcode)) {