1 <!-- @file Documentation for the @BootstrapAlter annotated discovery plugin. -->
6 - [Pre-requisite](#prerequisite)
7 - [Supported alter hooks](#supported)
8 - [Form alter hooks](#form)
9 - [Create a plugin](#create)
10 - [Rebuild the cache](#rebuild)
14 ## Pre-requisite {#prerequisite}
16 Due to the nature of how Drupal alter hooks work, there is no "catch all" alter
17 hook (like for forms with hook_form_alter). That means for you to use this
18 plugin, it must be invoked from inside each and every alter hook that lives in
21 Luckily you don't have to worry about invoking the plugin directly. Instead,
22 all you have to do is call the `Bootstrap::alter` helper method and pass the
23 alter function name and parameters as arguments:
26 use Drupal\bootstrap\Bootstrap;
29 * Implements hook_some_hook_alter().
31 function THEMENAME_some_hook_alter(&$data, &$context1 = NULL, &$context2 = NULL) {
32 Bootstrap::alter(__FUNCTION__, $data, $context1, $context2);
37 ## Supported alter hooks {#supported}
39 This base theme implements several of the most commonly used alter hooks in
40 themes and are automatically supported out-of-the-box.
42 Once a base theme has implemented an alter hook, like mentioned above, all
43 subsequent sub-themes will have the ability to implement a plugin for that
44 alter hook directly. All you have to do is simply create the plugin file in
45 `./THEMENAME/src/Plugin/Alter`. No need to implement any code in
48 - `hook_bootstrap_colorize_text_alter`
49 - `hook_bootstrap_iconize_text_alter`
50 - `hook_element_info_alter`
51 - `hook_js_settings_alter`
52 - `hook_library_info_alter`
53 - `hook_page_attachments_alter`
54 - `hook_theme_registry_alter`
55 - `hook_theme_suggestions_alter`
57 {.alert.alert-info}**Note:** if you do not see an alter hook here that you think
58 _should_ be here, please
59 [create an issue](https://www.drupal.org/node/add/project-issue/bootstrap)
61 ## Form alter hooks {#form}
63 You were probably thinking: "Hey, where's `hook_form_alter`? Didn't you _just_
66 As we all know, forms can be a tad more involved than just a simple "alter" and
67 we figured that we'd give you a little more power behind what you can actually
68 do with them. So if you're interested in those, please go see:
70 @link plugins_form @BootstrapForm @endlink
72 While, yes technically, `hook_form_system_theme_settings_alter` could also
73 fall under the form plugin, we decided to take those a step further as well, see:
76 @link plugins_setting @BootstrapSetting @endlink
78 ## Create a plugin {#create}
80 We'll use `PageAttachments` implemented by this base theme as an example of
81 how to add a library from your sub-theme to every page request.
83 Replace all following instances of `THEMENAME` with the actual machine name of
86 Create a file at `./THEMENAME/src/Plugin/Alter/PageAttachments.php` with the
92 * Contains \Drupal\THEMENAME\Plugin\Alter\PageAttachments.
95 namespace Drupal\THEMENAME\Plugin\Alter;
97 use Drupal\bootstrap\Annotation\BootstrapAlter;
100 * Implements hook_page_attachments_alter().
102 * @ingroup plugins_alter
104 * @BootstrapAlter("page_attachments")
106 class PageAttachments extends \Drupal\bootstrap\Plugin\Alter\PageAttachments {
111 public function alter(&$attachments, &$context1 = NULL, &$context2 = NULL) {
112 // Call the parent method from the base theme, if applicable (which it is
113 // in this case because Bootstrap actually implements this alter).
114 parent::alter($attachments, $context1, $context2);
116 // Add your custom library.
117 $attachments['#attached']['library'][] = 'THEMENAME/my_library';
123 ## Rebuild the cache {#rebuild}
125 Once you have saved, you must rebuild your cache for this new plugin to be
126 discovered. This must happen anytime you make a change to the actual file name
127 or the information inside the `@BootstrapAlter` annotation.
129 To rebuild your cache, navigate to `admin/config/development/performance` and
130 click the `Clear all caches` button. Or if you prefer, run `drush cr` from the
133 VoilĂ ! After this, you should have a fully functional `@BootstrapAlter` plugin!