Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Belba on
A field with multiple select list of taxonomy terms (field A )
A second field is a boolean (field B)
If in field A, 4 or more checkboxes are checked, then field B is true.
I try to achieve this in an ajax callback.
Hook form alter :
$form['field_kindvriendelijk']['widget']['value']['#prefix'] = '<div id="toevoegen-kindvriendelijk">';
$form['field_kindvriendelijk']['widget']['value']['#suffix'] = '</div>';
$form['field_kindvriendelijkheid']['widget']['#ajax'] = array(
'callback' => 'zaal_condities_KindVriendelijkCallback',
'event' => 'change',
'wrapper' => 'toevoegen-kindvriendelijk',
);
ajaxCallback :
function zaal_condities_KindVriendelijkCallback(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
$kindvriend = $form_state->getValue('field_kindvriendelijkheid');
$counter = 0;
foreach ($kindvriend as $value) {
$counter++;
$value[number];
if($value > 3){
$form['field_kindvriendelijk']['widget']['value']['#checked'] = TRUE;
}
}
return $form['field_kindvriendelijk'];
}
Target field B is already checked, when 1 item is checked in field A. When removing the checkboxex in A. Field B remain checked. Any tips are appreciated. Even a solution without the ajaxcallback.
Comments
use Drupal\Core\Form
Follow your instructions
Hi
Thanks for thinking with me. Unfortunaly, this code does not does his work
The code is working, but you
The code is working, but you may share your code so we can try find bugs.
Please see the code in use
Please see the code in use below.
Field B is now no longer checked, as it used to be.
Replace this string
Replace this string
with
Thanks for correct
Thanks for correct modification and your patience, your code indeed works perfectly.
The working code in action
Nice support Wombatbuddy
Nice support Wombatbuddy