4

I have some action links defined in module.links.actions.yml which uses a custom "create entity" route. For entity creation I set up some restrictions and at some point the access is denied. The problem is that whilst the access to the page is denied, the action link is still visible until I clear all caches.

Is there a way to tell Drupal not to cache the links or to programmatically clear the links cache? Or some options I can add in the yml file to prevent caching?

Thanks.

1
  • You need to provide appropriate cache context or cache tags to for your route.
    – ya.teck
    Commented Nov 12, 2016 at 21:24

1 Answer 1

0

The following example implements an action link in Drupal 8 with the requirement of user-specific caching. This should allow you to hide/display the action link for the different users on your page. Providing different titles for different users should also work.

mymodule.links.action.yml:

entity.mymodule.add_form:
  class: Drupal\mymodule\Plugin\Menu\LocalAction\UserAction
  route_name: entity.mymodule.add_form
  title: 'Link to Add Form'
  appears_on:
    - entity.mymodule.collection

mymodule/src/Plugin/Menu/LocalAction/UserAction.php:

<?php

namespace Drupal\randomintro\Plugin\Menu\LocalAction;

use Drupal\Core\Menu\LocalActionDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Defines a test local action plugin class.
 */
class UserAction extends LocalActionDefault {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;


  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, AccountInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider);

    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('router.route_provider'),
      $container->get('current_user')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return parent::getCacheContexts() + ['user'];
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(RouteMatchInterface $route_match) {
    // If the user is not Anonymous.
    if (!$this->currentUser->isAnonymous()) {
      // Getting the uid.
      $uid = $this->currentUser->id();
      // Adding the link.
      return ['user' => $uid];
    }
    return [];
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.