class CurrentPathContext

Makes the current path available as a context variable.

Hierarchy

Expanded class hierarchy of CurrentPathContext

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

File

src/ContextProvider/CurrentPathContext.php, line 16

Namespace

Drupal\rules\ContextProvider
View source
class CurrentPathContext implements ContextProviderInterface {
    use StringTranslationTrait;
    
    /**
     * The current path service.
     *
     * @var \Drupal\Core\Path\CurrentPathStack
     */
    protected $currentPathStack;
    
    /**
     * Constructs a new CurrentPathContext.
     *
     * @param \Drupal\Core\Path\CurrentPathStack $current_path_stack
     *   The current path stack service.
     */
    public function __construct(CurrentPathStack $current_path_stack) {
        $this->currentPathStack = $current_path_stack;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRuntimeContexts(array $unqualified_context_ids) {
        $values = [
            'path' => $this->currentPathStack
                ->getPath(),
            'url' => Url::fromRoute('<current>', [], [
                'absolute' => TRUE,
            ])->toString(),
        ];
        $context_definition = new ContextDefinition('current_path', $this->t('Current path'));
        $context = new Context($context_definition, $values);
        $cacheability = new CacheableMetadata();
        $cacheability->setCacheContexts([
            'url.path',
        ]);
        $context->addCacheableDependency($cacheability);
        $result = [
            'current_path' => $context,
        ];
        return $result;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAvailableContexts() {
        return $this->getRuntimeContexts([]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CurrentPathContext::$currentPathStack protected property The current path service.
CurrentPathContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
CurrentPathContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
CurrentPathContext::__construct public function Constructs a new CurrentPathContext.
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.