Form API http://lobsterr.me/ en How to set a custom redirection after form submission Drupal 9? http://lobsterr.me/post/how-set-custom-redirection-after-form-submission-drupal-9 <span class="field field--name-title field--type-string field--label-hidden">How to set a custom redirection after form submission Drupal 9?</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">01/25/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>To redirect a user after form submit to a custom URL. We need to implement custom submit function.</p> <pre> <code class="language-php">use Drupal\Core\Form\FormStateInterface; /** * Implements hook_form_FORM_ID_alter(). */ function mydoule_form_YOUR_FORM_ID_alter(&amp;$form, FormStateInterface $form_state, $form_id) { // add custom function as submit handler. $form['#submit'][] = '_mymodule_custom_submit_handler'; // Sometime forms doesn't into account '#submit' parameter. So, we have to set custom handler to the specific button of the form $form['actions']['submit']['#submit'][] = '_mymodule_custom_submit_handler'; } /** * Custom submit handler for your form form. */ function _mymodule_custom_submit_handler($form, FormStateInterface $form_state) { // Set a custom redirect. $form_state-&gt;setRedirect('custom.route'); }</code></pre> <p>You will need to replace module name and put into install file of your module and also don't forget to replace form id.</p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/form-api" hreflang="en">Form API</a></div> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Mon, 25 Jan 2021 13:15:12 +0000 LOBsTerr 28 at http://lobsterr.me How to alter options for dropdown (select input) in Drupal http://lobsterr.me/post/how-alter-options-dropdown-select-input-drupal <span class="field field--name-title field--type-string field--label-hidden">How to alter options for dropdown (select input) in Drupal</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">09/04/2019</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>Sometimes we need to alter or restrict some options for dropdown of entity reference field. One of the solution to use views and provide views entity reference widget, but in some cases the logic would be too complex and we will have to implement custom plugins for views. The second solution could be just replace options for dropdown widget. For this we need implement "hook_field_widget_form_alter"</p> <pre> <code class="language-php">/** * Implements hook_field_widget_form_alter(). */ function your_module_field_widget_form_alter( if (!empty($field_definition) &amp;&amp; $field_definition-&gt;getName() == 'fut_collection') { $element['#options'] = _your_module_get_input_options(); } } /** * Gets group collection options. */ function _your_module_get_input_options() { $options = []; // here your custom logic to get ids. $items = Term::loadMultiple($ids); if ($items ) { // a default empty value. $options['_none'] = t('- None -'); foreach ($items as $item) { $options[$item-&gt;id()] = $item-&gt;getName(); } } return $options; }</code></pre> <p>Keep in mind that you have to use the entity ids of entity type for your entity reference field. Also, if you want to provide the default value without any value, you should use this code:</p> <pre> <code class="language-php">$options['_none'] = t('- None -');</code></pre> <p>and not like</p> <pre> <code class="language-php">$options[''] = t('- None -');</code></pre> <p>In other case you can face some unexpected bugs</p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/form-api" hreflang="en">Form API</a></div> <div class="field__item btn btn-secondary"><a href="/tags/hook" hreflang="en">Hook</a></div> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Wed, 04 Sep 2019 09:05:50 +0000 LOBsTerr 10 at http://lobsterr.me