You are here

class IsGroupMemberCacheContext in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Cache/Context/IsGroupMemberCacheContext.php \Drupal\group\Cache\Context\IsGroupMemberCacheContext

Defines a cache context for "is a group member or not" caching.

Do NOT use this on an element that shows up a lot, but with a different group ID depending on the situation. E.g.: The group operations block. The reason is that core will combine all of these possible outcomes into a really long cache key and both CacheContextsManager and RenderCache will have to run code for each and every one of the possible outcomes.

You MAY, however, use this for objects that almost always ask for the same group ID. A good example would be a call-to-action on some pages that only shows up if you're not a member of a specific group yet. In that scenario, you'll definitely want to use this cache context and it will not kill your site's performance with fire.

Calculated cache context ID: 'user.is_group_member:%group_id'.

@todo With the new VariationCache's CacheRedirect system, this could actually be used on the GroupOperationsBlock as it no longer expands into a really long cache ID but simply adds another CacheRedirect.

Hierarchy

Expanded class hierarchy of IsGroupMemberCacheContext

1 file declares its use of IsGroupMemberCacheContext
IsGroupMemberCacheContextTest.php in tests/src/Unit/IsGroupMemberCacheContextTest.php
1 string reference to 'IsGroupMemberCacheContext'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses IsGroupMemberCacheContext
cache_context.user.is_group_member in ./group.services.yml
Drupal\group\Cache\Context\IsGroupMemberCacheContext

File

src/Cache/Context/IsGroupMemberCacheContext.php, line 32

Namespace

Drupal\group\Cache\Context
View source
class IsGroupMemberCacheContext implements CalculatedCacheContextInterface {

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

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The membership loader service.
   *
   * @var \Drupal\group\GroupMembershipLoaderInterface
   */
  protected $membershipLoader;

  /**
   * Constructs a new GroupMembershipPermissionsCacheContext class.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\group\GroupMembershipLoaderInterface $membership_loader
   *   The group membership loader service.
   */
  public function __construct(AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager, GroupMembershipLoaderInterface $membership_loader) {
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->membershipLoader = $membership_loader;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t("Is group member");
  }

  /**
   * {@inheritdoc}
   */
  public function getContext($group_id = NULL) {
    if (!$group_id) {
      throw new \LogicException('No group ID provided for user.is_group_member cache context.');
    }
    $group = $this->entityTypeManager
      ->getStorage('group')
      ->load($group_id);
    if (!$group) {
      throw new \LogicException('Incorrect group ID provided for user.is_group_member cache context.');
    }
    return $this->membershipLoader
      ->load($group, $this->currentUser) ? '1' : '0';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($group_id = NULL) {
    if (!$group_id) {
      throw new \LogicException('No group ID provided for user.is_group_member cache context.');
    }

    // The value of this context is affected when the user joins or leaves the
    // group. Both of which trigger a user save, so we can simply add the user's
    // cacheable metadata here.
    return CacheableMetadata::createFromObject($this->currentUser
      ->getAccount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IsGroupMemberCacheContext::$currentUser protected property The current user.
IsGroupMemberCacheContext::$entityTypeManager protected property The entity type manager.
IsGroupMemberCacheContext::$membershipLoader protected property The membership loader service.
IsGroupMemberCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface::getCacheableMetadata
IsGroupMemberCacheContext::getContext public function Returns the string representation of the cache context. Overrides CalculatedCacheContextInterface::getContext
IsGroupMemberCacheContext::getLabel public static function Returns the label of the cache context. Overrides CalculatedCacheContextInterface::getLabel
IsGroupMemberCacheContext::__construct public function Constructs a new GroupMembershipPermissionsCacheContext class.