2

I have a custom Views access plugin that check access against a value in the current User's Profile (entity) field value.

The plugin works but if I change the profile field value the access does not check again until the cache is cleared. I know this is an issue with setting up the cache tags/contexts but I cannot not figure out what is needed so that the cache is cleared when the user profile is updated.

Right now I'm not using any cache tags/contexts in my View access plug and I've tried various combinations without luck. Help!

Current cache functions:

public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $profile = \Drupal::entityManager()->getStorage('profile')->loadByUser(user_load(\Drupal::currentUser()->id()), 'employee');    
return $profile->getCacheTags();
  }

1 Answer 1

4

A Views access plugin implements CacheableDependencyInterface. So you probably need to set a tag and a context for the current user in these methods:

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return ['user:' . \Drupal::currentUser()->id()];
  }
5
  • Thank you! I was not adding the current user ID to getCacheTags - makes perfect sense now. Commented Oct 14, 2017 at 15:57
  • Actually, I spoke too soon. The caching is still not clearing properly. The cache tags example you provided is using the user object but the field value that I am changing is on the Profile object for the user. Is there a way to set the cache contexts to use the Profile object of the user? Commented Oct 14, 2017 at 17:00
  • I've supposed you mean with user profile the core user entity, because core often refers to the user content form as profile, but if this is from a contrib module, then load the profile entity the same like you did for the field value and return the cache tag from this entity $profile->getCacheTags(). This is only for the tag, the context is still a dependency on the current user.
    – 4uk4
    Commented Oct 14, 2017 at 22:42
  • Thanks again. I still can't get it to work. And, yes, sorry, by profile I was referring to the contrib profile module that creates 'profile' entities for the user objects. I've edited my original question to include my current cache functions. I am loading the profile entity in getCacheTags() and returning it as return $profile->getCacheTags() - the getCacheContexts() is set to return ['user'] Commented Oct 15, 2017 at 1:45
  • How would this work for anon user. For example, test a language variable...
    – Sigma
    Commented Oct 24, 2018 at 5:38

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.