og-8.x-1.x-dev/src/Cache/Context/OgGroupContextCacheContext.php

src/Cache/Context/OgGroupContextCacheContext.php
<?php

declare(strict_types=1);

namespace Drupal\og\Cache\Context;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\OgContextInterface;

/**
 * Defines a cache context service for the currently active group.
 *
 * This uses OgContext to determine the active group. Potential use cases for
 * this cache context are elements on the page that vary by the active group,
 * for example a group header, or a block showing recent group content.
 *
 * Cache context ID: 'og_group_context'
 */
class OgGroupContextCacheContext implements CacheContextInterface {

  /**
   * The string to return when no context is found.
   */
  const NO_CONTEXT = 'none';

  public function __construct(protected readonly OgContextInterface $ogContext) {}

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return new TranslatableMarkup('OG active group');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    // Do not provide a cache context if there is no group in the current
    // context.
    $group = $this->ogContext->getGroup();
    if (empty($group)) {
      return self::NO_CONTEXT;
    }

    // Compose a cache context string that consists of the entity type ID and
    // the entity ID of the active group.
    return implode(':', [$group->getEntityTypeId(), $group->id()]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc