You are here

class PageEventVariables in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/preprocess_event_dispatcher/src/Variables/PageEventVariables.php \Drupal\preprocess_event_dispatcher\Variables\PageEventVariables

Class PageEventVariables.

Hierarchy

Expanded class hierarchy of PageEventVariables

3 files declare their use of PageEventVariables
OtherEventVariablesTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventVariablesTest.php
PagePreprocessEventFactory.php in modules/preprocess_event_dispatcher/src/Factory/PagePreprocessEventFactory.php
PageTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/PageTest.php

File

modules/preprocess_event_dispatcher/src/Variables/PageEventVariables.php, line 10

Namespace

Drupal\preprocess_event_dispatcher\Variables
View source
class PageEventVariables extends AbstractEventVariables {

  /**
   * Is the current page a rendering a node?.
   *
   * @return bool
   *   Is it?
   */
  public function isNodePage() : bool {
    return isset($this->variables['node']) && $this->variables['node'] instanceof NodeInterface;
  }

  /**
   * Get the node of the page.
   *
   * @return \Drupal\node\NodeInterface|null
   *   Drupal node.
   */
  public function getNode() : ?NodeInterface {
    if (!$this
      ->isNodePage()) {
      return NULL;
    }
    return $this->variables['node'];
  }

  /**
   * Get the template var.
   *
   * @param string $name
   *   Name.
   * @param mixed $default
   *   Default.
   *
   * @return mixed
   *   Value
   */
  public function get(string $name, $default = NULL) {
    if (!isset($this->variables['page'][$name])) {
      return $default;
    }
    return $this->variables['page'][$name];
  }

  /**
   * Set a given page variable.
   *
   * @param string $name
   *   Name.
   * @param mixed $value
   *   Value.
   */
  public function set(string $name, $value = NULL) : void {
    $this->variables['page'][$name] = $value;
  }

  /**
   * Remove a given page variable.
   *
   * @param string $name
   *   Name.
   */
  public function remove(string $name) : void {
    unset($this->variables['page'][$name]);
  }

  /**
   * Get a variable with a given name by reference.
   *
   * @param string $name
   *   Variable name.
   *
   * @return mixed
   *   Reference for the variable.
   */
  public function &getByReference(string $name) {
    return $this->variables['page'][$name];
  }

  /**
   * Get the complete $variables of the page template.
   *
   * @return array
   *   Reference to all variables of the page template.
   */
  public function &getRootVariablesByReference() : array {
    return $this->variables;
  }

  /**
   * Add a cache context to the page template.
   *
   * @param string $context
   *   A cache context such as 'url.path'.
   */
  public function addCacheContext(string $context) : void {
    $this->variables['#cache']['contexts'][] = $context;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractEventVariables::$variables protected property Variables.
AbstractEventVariables::__construct public function Event Variables constructor.
PageEventVariables::addCacheContext public function Add a cache context to the page template.
PageEventVariables::get public function Get the template var. Overrides AbstractEventVariables::get
PageEventVariables::getByReference public function Get a variable with a given name by reference. Overrides AbstractEventVariables::getByReference
PageEventVariables::getNode public function Get the node of the page.
PageEventVariables::getRootVariablesByReference public function Get the complete $variables of the page template.
PageEventVariables::isNodePage public function Is the current page a rendering a node?.
PageEventVariables::remove public function Remove a given page variable. Overrides AbstractEventVariables::remove
PageEventVariables::set public function Set a given page variable. Overrides AbstractEventVariables::set