1 <!-- @file Documentation for the @BootstrapPreprocess annotated plugin. -->
6 - [Create a plugin](#create)
7 - [Rebuild the cache](#rebuild)
11 ## Create a plugin {#create}
13 We'll use `Page` implemented by this base theme as an example of how to add
14 custom classes for the `page.html.twig` template that should only be added
15 under certain conditions.
17 Replace all following instances of `THEMENAME` with the actual machine name of
20 Create a file at `./THEMENAME/src/Plugin/Preprocess/Page.php` with the
26 namespace Drupal\THEMENAME\Plugin\Preprocess;
28 use Drupal\bootstrap\Plugin\Preprocess\Page as BootstrapPage;
29 use Drupal\bootstrap\Utility\Element;
30 use Drupal\bootstrap\Utility\Variables;
33 * Pre-processes variables for the "page" theme hook.
35 * @ingroup plugins_preprocess
37 * @BootstrapPreprocess("page")
39 class Page extends BootstrapPage {
41 * It should be noted that you do not need all three methods here.
42 * This is to just show you the different examples of how this plugin
43 * works and how they can be tailored to your needs.
49 public function preprocess(array &$variables, $hook, array $info) {
50 $value = isset($variables['element']['child']['#value']) ? $variables['element']['child']['#value'] : FALSE;
51 if (_some_module_condition($value)) {
52 $variables['attributes']['class'][] = 'my-theme-class';
53 $variables['attributes']['class'][] = 'another-theme-class';
54 $key = array_search('page', $variables['attributes']['class']);
56 unset($variables['attributes']['class'][$key]);
60 // If you are extending and overriding a preprocess method from the base
61 // theme, it is imperative that you also call the parent (base theme) method
62 // at some point in the process, typically after you have finished with your
64 parent::preprocess($variables, $hook, $info);
70 public function preprocessVariables(Variables $variables) {
71 // This method is almost identical to the one above, but it introduces the
72 // Variables utility class in the base theme. This class has a plethora of
73 // helpful methods to quickly modify common tasks when you're in a
74 // preprocess function. It also acts like the normal $variables array when
75 // you need it to in instances of accessing nested content or in loop
76 // structures like foreach.
77 $value = isset($variables['element']['child']['#value']) ? $variables['element']['child']['#value'] : FALSE;
78 if (_some_module_condition($value)) {
79 $variables->addClass(['my-theme-class', 'another-theme-class'])->removeClass('page');
81 parent::preprocessVariables($variables);
87 protected function preprocessElement(Element $element, Variables $variables) {
88 // This method is only ever invoked if either $variables['element'] or
89 // $variables['elements'] exists. These keys are usually only found in forms
90 // or render arrays when there is a #type being used. This introduces the
91 // Element utility class in the base theme. It too has a bucket-load of
92 // features, specific to the unique characteristics of render arrays with
93 // their "properties" (keys starting with #). This will quickly allow you to
94 // access some of the nested element data and reduce the overhead required
95 // for commonly used logic.
96 $value = $element->child->getProperty('value', FALSE);
97 if (_some_module_condition($value)) {
98 $variables->addClass(['my-theme-class', 'another-theme-class'])->removeClass('page');
100 parent::preprocessElement($element, $variables);
107 ## Rebuild the cache {#rebuild}
109 Once you have saved, you must rebuild your cache for this new plugin to be
110 discovered. This must happen anytime you make a change to the actual file name
111 or the information inside the `@BootstrapPreprocess` annotation.
113 To rebuild your cache, navigate to `admin/config/development/performance` and
114 click the `Clear all caches` button. Or if you prefer, run `drush cr` from the
117 VoilĂ ! After this, you should have a fully functional `@BootstrapPreprocess`