Ability to check entity access

Created on 6 April 2023, about 1 year ago
Updated 27 May 2023, about 1 year ago

Problem/Motivation

I can't find a way to do an access check inside a template. For example, embedding addition admin information in a node template. The problem is that the access method is not allowed by the twig sandbox.

Steps to reproduce

in a node template use

node.access('edit')

Proposed resolution

Maybe something like this?


  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new TwigFunction('has_access', [$this, 'hasAccess']),
    ];
  }

  /**
   * Check if current user has access to a specific operation.
   *
   * @param \Drupal\Core\Access\AccessibleInterface $accessible
   *   Object implementing the accessible interface.
   * @param $operation
   *   The operation being checked.
   *
   * @return bool
   */
  public function hasAccess(AccessibleInterface $accessible, $operation): bool {
    return $accessible->access($operation);
  }

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Closed: won't fix

Version

3.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States neclimdul Houston, TX

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024