You are here

class Configs in Bamboo Twig 8

Provides a 'Configs' Twig Extensions.

Hierarchy

  • class \Drupal\bamboo_twig_config\TwigExtension\Configs extends \Drupal\bamboo_twig_config\TwigExtension\Twig_Extension

Expanded class hierarchy of Configs

1 string reference to 'Configs'
bamboo_twig_config.services.yml in bamboo_twig_config/bamboo_twig_config.services.yml
bamboo_twig_config/bamboo_twig_config.services.yml
1 service uses Configs
bamboo_twig_config.twig.configs in bamboo_twig_config/bamboo_twig_config.services.yml
Drupal\bamboo_twig_config\TwigExtension\Configs

File

bamboo_twig_config/src/TwigExtension/Configs.php, line 12

Namespace

Drupal\bamboo_twig_config\TwigExtension
View source
class Configs extends \Twig_Extension {

  /**
   * Config API for storing variables that travel between instances.
   *
   * @var Drupal\Core\Config\ConfigFactory
   */
  private $config;

  /**
   * State API for storing variables that shouldn't travel between instances.
   *
   * @var Drupal\Core\State\StateInterface
   */
  private $state;

  /**
   * TwigExtension constructor.
   */
  public function __construct(ConfigFactory $config, StateInterface $state) {
    $this->config = $config;
    $this->state = $state;
  }

  /**
   * List of all Twig functions.
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('get_config', [
        $this,
        'getConfig',
      ]),
      new \Twig_SimpleFunction('get_state', [
        $this,
        'getState',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig.twig.configs';
  }

  /**
   * Load given Config API configuration.
   *
   * @param string $key
   *   The key of the data to retrieve.
   * @param string $name
   *   The name of config to retrieve.
   *
   * @return mixed|null
   *   Returns the stored value for a given key, or NULL if no value exists.
   */
  public function getConfig($key, $name) {
    return $this->config
      ->get($key)
      ->get($name);
  }

  /**
   * Load given State API configuration.
   *
   * @param string $key
   *   The key of the data to retrieve.
   *
   * @return mixed|null
   *   Returns the stored value for a given key, or NULL if no value exists.
   */
  public function getState($key) {
    return $this->state
      ->get($key);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Configs::$config private property Config API for storing variables that travel between instances.
Configs::$state private property State API for storing variables that shouldn't travel between instances.
Configs::getConfig public function Load given Config API configuration.
Configs::getFunctions public function List of all Twig functions.
Configs::getName public function Unique identifier for this Twig extension.
Configs::getState public function Load given State API configuration.
Configs::__construct public function TwigExtension constructor.