I’ve Drupal Development Company subsequent type which habits is:
-
When you choose an choice from choose management, totally different checkbox are load on checkboxes management.
-
Once you test/uncheck checkbox a message is confirmed on Drupal Development Company botton with data about choose worth and checkbox clicked.
Every little thing works positive, however is not possible test by default a checkbox from checkboxes management after ajax operate.
Any thought?
namespace Drupaldrupal_miseriesForm; use DrupalCoreFormFormBase; use DrupalCoreFormFormStateInterface; use DrupalCoreRenderElementCheckboxes; class CheckboxesAjax extends FormBase { protected $values = [ 'select' => [ '1' => 'Value1', '2' => 'Value2', ], // Choices when choose 1 is chosen 'checkboxes_v1' => [ '1' => 'checkbox1', '2' => 'checkbox2', ], // Choices when choose 2 is chosen 'checkboxes_v2' => [ '3' => 'checkbox3', '4' => 'checkbox4', ], // Default values for checkboxes 'checkboxes_df' => [ // Default value when checkboxes_v1 is showed '1' => ['1' => '1', '2' => '0'], // Default worth when checkboxes_v2 is confirmed '2' => ['3' => '0', '4' => '1'], ], ]; public operate getFormId() { return 'drupal_miseries_checkboxesajax'; } public operate selectAjaxCallback(array &$type, FormStateInterface $form_state) { return $type['container']; } public operate checkboxesAjaxCallback(array &$type, FormStateInterface $form_state) { $checkbox = $form_state->getTriggeringElement(); $choose = $form_state->getValue('choose'); $type['status']['mensaje'] = [ '#markup' => 'Select: '.$select.' '.$checkbox['#title'].' clicked.'.'
', ]; return $type['status']; } public operate buildForm(array $type, FormStateInterface $form_state) { $type['select'] = [ '#type' => 'select', '#title' => $this->t('Select form control'), '#options' => $this->values['select'], '#default_value' => key($this->values['select']), '#ajax' => [ 'callback' => [$this, 'selectAjaxCallback'], 'wrapper' => 'checkboxes_wrapper', ], ]; $type['container'] = [ '#type' => 'container', '#attributes' => ['id' => 'checkboxes_wrapper'], ]; $type['container']['checkboxes'] = [ '#type' => 'checkboxes', '#title' => $this->t('Checkboxes form control from select @select', [ '@select' => empty($form_state->getValues()) ? $this->values['select'][key($this->values['select'])] : $form_state->getValue('choose') ]), '#choices' => empty($form_state->getValues()) ? $this->values['checkboxes_v1'] : $this->values['checkboxes_v'.$form_state->getValue('select')], '#default_value' => empty($form_state->getValues()) ? $this->values['checkboxes_df']['1'] : $this->values['checkboxes_df'][$form_state->getValue('select')], '#ajax' => [ 'callback' => [$this, 'checkboxesAjaxCallback'], 'wrapper' => 'standing', ], ]; $type['status'] = [ '#type' => 'container', '#attributes' => ['id' => 'status'], ]; return $type; } public operate submitForm(array &$type, FormStateInterface $form_state) { } }