class Menu

Pre-processes variables for the "menu" theme hook.

Plugin annotation

@BootstrapPreprocess("menu")

Hierarchy

Expanded class hierarchy of Menu

Related topics

Namespace

Drupal\bootstrap\Plugin\Preprocess
Source
class Menu extends PreprocessBase implements PreprocessInterface {

  /**
   * {@inheritdoc}
   */
  protected function preprocessVariables(Variables $variables) {
    foreach ($variables->items as &$item) {
      $wrapperAttributes = new Attributes();
      $linkAttributes = new Attributes();
      if ($item['attributes'] instanceof Attribute || $item['attributes'] instanceof Attributes) {
        $wrapperAttributes->setAttributes($item['attributes']->getIterator()->getArrayCopy());
      }
      if ($item['url'] instanceof Url) {
        $wrapperAttributes->setAttributes($item['url']->getOption('wrapper_attributes') ? : []);
        $wrapperAttributes->setAttributes($item['url']->getOption('container_attributes') ? : []);
        $linkAttributes->setAttributes($item['url']->getOption('attributes') ? : []);
      }

      // Unfortunately, in newer core/Twig versions, only certain classes are
      // allowed to be invoked due to stricter sandboxing policies. To get
      // around this, just rewrap attributes in core's native Attribute class.
      $item['attributes'] = new Attribute($wrapperAttributes->getArrayCopy());
      $item['link_attributes'] = new Attribute($linkAttributes->getArrayCopy());
    }
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Description
Menu::preprocessVariables protected function Preprocess the variables array. Overrides PreprocessBase::preprocessVariables
PluginBase::$theme protected property The currently set theme object.
PluginBase::__construct public function
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocess public function Preprocess theme hook variables. Overrides PreprocessInterface::preprocess
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessElement protected function Preprocess the variables array if an element is present.