class SiteContext

Sets the current node as a context on node routes.

Modules may add properties to this global context by implementing hook_data_type_info_alter(&$data_types) to modify the $data_types['site'] element.

@todo Need a way to alter the global context contents to set a value for any added site properties.

Hierarchy

Expanded class hierarchy of SiteContext

1 string reference to 'SiteContext'
rules.services.yml in ./rules.services.yml
rules.services.yml
1 service uses SiteContext
rules.site_context in ./rules.services.yml
Drupal\rules\ContextProvider\SiteContext

File

src/ContextProvider/SiteContext.php, line 23

Namespace

Drupal\rules\ContextProvider
View source
class SiteContext implements ContextProviderInterface {
  use StringTranslationTrait;
  
  /**
   * The system.site configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $systemSiteConfig;
  
  /**
   * Constructs a new SiteContext.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config.factory service.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->systemSiteConfig = $config_factory->get('system.site');
  }
  
  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $site = [
      'url' => Url::fromRoute('<front>', [], [
        'absolute' => TRUE,
      ])->toString(),
      'login-url' => Url::fromRoute('user.page', [], [
        'absolute' => TRUE,
      ])->toString(),
      'name' => $this->systemSiteConfig
        ->get('name'),
      'slogan' => $this->systemSiteConfig
        ->get('slogan'),
      'mail' => $this->systemSiteConfig
        ->get('mail'),
    ];
    $context_definition = new ContextDefinition('site', $this->t('Site information'));
    $context = new Context($context_definition, $site);
    $cacheability = new CacheableMetadata();
    $cacheability->setCacheContexts([
      'site',
    ]);
    $context->addCacheableDependency($cacheability);
    $result = [
      'site' => $context,
    ];
    return $result;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
SiteContext::$systemSiteConfig protected property The system.site configuration.
SiteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
SiteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
SiteContext::__construct public function Constructs a new SiteContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.