9dec2b2b1381181fd5836e11bab771d6bb5d8085
[yaffs-website] / web / themes / contrib / bootstrap / docs / plugins / Form.md
1 <!-- @file Documentation for the @BootstrapForm annotated discovery plugin. -->
2 <!-- @defgroup -->
3 <!-- @ingroup -->
4 # @BootstrapForm
5
6 - [Create a plugin](#create)
7 - [Rebuild the cache](#rebuild)
8
9 ---
10
11 ## Create a plugin {#create}
12
13 We'll use `SearchBlockForm` implemented by this base theme as an example of
14 how to remove `#input_group_button` from `search_block_form`.
15
16 Replace all following instances of `THEMENAME` with the actual machine name of
17 your sub-theme.
18
19 Create a file at `./THEMENAME/src/Plugin/Form/SearchBlockForm.php` with the
20 following contents:
21
22 ```php
23 <?php
24
25 namespace Drupal\THEMENAME\Plugin\Form;
26
27 use Drupal\bootstrap\Plugin\Form\SearchBlockForm as BootstrapSearchBlockForm;
28 use Drupal\bootstrap\Utility\Element;
29 use Drupal\Core\Form\FormStateInterface;
30
31 /**
32  * Implements hook_form_FORM_ID_alter().
33  *
34  * @ingroup plugins_form
35  *
36  * @BootstrapForm("search_block_form")
37  */
38 class SearchBlockForm extends BootstrapSearchBlockForm {
39
40   /**
41    * {@inheritdoc}
42    */
43   public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
44     // Call the parent method from the base theme, if applicable (which it is
45     // in this case because Bootstrap actually implements this alter).
46     parent::alterForm($form, $form_state, $form_id);
47
48     // Disable #input_group_button the normal way:
49     $form['keys']['#input_group_button'] = FALSE;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
56     // This method is the same as above, except the the $form argument passed is
57     // an instance of \Drupal\bootstrap\Utility\Element for easier manipulation.
58     // Using this method is preferable and considered "Best Practice".
59     //
60     // Disable #input_group_button using the $form Element object:
61     // $form->keys->setProperty('input_group_button', FALSE);.
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function submitForm(array &$form, FormStateInterface $form_state) {
68     // This method is automatically called when the form is submitted.
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public static function submitFormElement(Element $form, FormStateInterface $form_state) {
75     // This method is the same as above, except the the $form argument passed is
76     // an instance of \Drupal\bootstrap\Utility\Element for easier manipulation.
77     // Using this method is preferable and considered "Best Practice".
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public static function validateForm(array &$form, FormStateInterface $form_state) {
84     // This method is automatically called when the form is validated.
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function validateFormElement(Element $form, FormStateInterface $form_state) {
91     // This method is the same as above, except the the $form argument passed is
92     // an instance of \Drupal\bootstrap\Utility\Element for easier manipulation.
93     // Using this method is preferable and considered "Best Practice".
94   }
95
96 }
97 ?>
98 ```
99
100 ## Rebuild the cache {#rebuild}
101
102 Once you have saved, you must rebuild your cache for this new plugin to be
103 discovered. This must happen anytime you make a change to the actual file name
104 or the information inside the `@BootstrapForm` annotation.
105
106 To rebuild your cache, navigate to `admin/config/development/performance` and
107 click the `Clear all caches` button. Or if you prefer, run `drush cr` from the
108 command line.
109
110 VoilĂ ! After this, you should have a fully functional `@BootstrapForm` plugin!