SiteContext.php in Rules 8.3
File
src/ContextProvider/SiteContext.php
View source
<?php
namespace Drupal\rules\ContextProvider;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
class SiteContext implements ContextProviderInterface {
use StringTranslationTrait;
protected $systemSiteConfig;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->systemSiteConfig = $config_factory
->get('system.site');
}
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;
}
public function getAvailableContexts() {
return $this
->getRuntimeContexts([]);
}
}
Classes
Name |
Description |
SiteContext |
Sets the current node as a context on node routes. |