- πΈπ°Slovakia lubwn
I actually stubled upon this issue when writing my own module and adding some fields programativally to fieldset created by form_group module. I am unable to set weight of field programatically on a field which is wrapped in fieldset by admin UI.
I can actually solve this by simple JS on client-side but I wanted to solve this in backend. What is the proposed resolution in this case? Can my form alter hook run after the field_group module form alter hook?
- π¬π§United Kingdom james.williams
@lubwn - two suggestions: either use
hook_module_implements_alter()
, to get your hook_form_alter to act after field_group. Or add a #pre_render or #after_build callback to the form in your hook_form_alter. Those can then change things in the form array, after the form_alter stage. - πΊπΈUnited States mrweiner
either use hook_module_implements_alter(), to get your hook_form_alter to act after field_group.
This actually is not possible without modifying module weights, because field_group_module_implements_alter is run after custom implementations. So even if you do implement it, field group's will run later, causing field_group_form_alter to run last.
- π©π°Denmark ramlev
@mrweiner you should add a #pre_render to your form_alter and add a class implementing Drupal\Core\Security\TrustedCallbackInterface
user_module.module
<?php use Drupal\user_module\YourCustomFormAlterBuilder; function user_module_form_FORM_ID_alter(&$form, $form_state) { $form['#pre_render'][] = [YourCustomFormAlterBuilder::class, 'preRender']; }
Add something like this in your modules src/ folder.
YourCustomFormAlterBuilder.php
<?php namespace Drupal\user_module; use Drupal\Core\Security\TrustedCallbackInterface; class YourCustomFormAlterBuilder implements TrustedCallbackInterface { public static function trustedCallbacks() { return ['preRender']; } public static function preRender($form) { // Alter the form here. return $form; }
- π¨π¦Canada No Sssweat
Thanks #8 did the trick, but needed a tweak for the .module code
<?php use Drupal\user_module\YourCustomFormAlterBuilder; function user_module_form_FORM_ID_alter(&$form, $form_state) { $form['#pre_render'][] = [new YourCustomFormAlterBuilder, 'preRender']; }
- πͺπΈSpain psf_ Huelva
Great solution in #8. Maybe it should be added to the documentation?