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

wombatbuddy’s picture

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node form.
 */
function YOUR_MODULE_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $node = $form_state->getFormObject()->getEntity();
  if ($node->getType() != 'YOUR_CONTENT_TYPE') {
    return;
  }
  
  $checked_values = $form_state->getValue('field_kindvriendelijkheid');
  
  if ($checked_values && count($checked_values) > 3) {
    $form['field_kindvriendelijk']['widget']['value']['#value'] = 1;
  }
  elseif ($checked_values) {
    $form['field_kindvriendelijk']['widget']['value']['#value'] = 0;
  }
  
  $form['field_kindvriendelijk']['#prefix'] = '<div id="toevoegen-kindvriendelijk">';
  $form['field_kindvriendelijk']['#suffix'] = '</div>';
  $form['field_kindvriendelijkheid']['widget']['#ajax'] = [
    'callback' => 'my_ajax_callback',
    'wrapper' => 'toevoegen-kindvriendelijk',
  ];
}

/**
 * Ajax callback.
 */
function my_ajax_callback(&$form, FormStateInterface $form_state) {
  return $form['field_kindvriendelijk'];
}
Belba’s picture

Hi

Thanks for thinking with me. Unfortunaly, this code does not does his work

wombatbuddy’s picture

The code is working, but you may share your code so we can try find bugs.

Belba’s picture

Please see the code in use below. 

Field B is now no longer checked, as it used to be.

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node form.
 */
function zaal_condities_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface  $form_state, $form_id) {
  $node = $form_state->getFormObject()->getEntity();
  if ($node->getType() != 'bedrijf') {
    return;
  }
  $checked_values = $form_state->getValue('field_kindvriendelijkheid');
  
       if ($checked_values && count($checked_values) > 3) {
         $form['field_kindvriendelijk']['widget']['value']['#value'] = 1;
       }
       elseif ($checked_values) {
         $form['field_kindvriendelijk']['widget']['value']['#value'] = 0;
       }
       
       $form['field_kindvriendelijk']['#prefix'] = '<div id="toevoegen-kindvriendelijk">';
       $form['field_kindvriendelijk']['#suffix'] = '</div>';
       $form['field_kindvriendelijkheid']['widget']['#ajax'] = [
         'callback' => 'my_ajax_callback',
         'wrapper' => 'toevoegen-kindvriendelijk',
       ]; 
}

/**
 * Ajax callback.
 */
 function zaal_condities_my_ajax_callback(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
       return $form['field_kindvriendelijk'];
}
wombatbuddy’s picture

Replace this string 

'callback' => 'my_ajax_callback',

with 

'callback' => 'zaal_condities_my_ajax_callback',
Belba’s picture

Thanks for correct modification and your patience, your code indeed works perfectly.

The working code in action

Jaypan’s picture

Nice support Wombatbuddy