Updated the Bootstrap theme.
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / Menu.php
diff --git a/web/themes/contrib/bootstrap/src/Plugin/Preprocess/Menu.php b/web/themes/contrib/bootstrap/src/Plugin/Preprocess/Menu.php
new file mode 100644 (file)
index 0000000..9af5dc6
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace Drupal\bootstrap\Plugin\Preprocess;
+
+use Drupal\bootstrap\Utility\Attributes;
+use Drupal\bootstrap\Utility\Variables;
+use Drupal\Core\Template\Attribute;
+use Drupal\Core\Url;
+
+/**
+ * Pre-processes variables for the "menu" theme hook.
+ *
+ * @ingroup plugins_preprocess
+ *
+ * @BootstrapPreprocess("menu")
+ */
+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());
+    }
+  }
+
+}