0

I want to disable a single checkbox in a checkboxes FAPI element.

<?php

namespace Drupal\fapi_example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class SimpleForm extends FormBase {

  /**
   * Build the simple form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['checboxes'] = [
      '#type' => 'checkboxes',
      '#options' => [
        'A' => $this->t('A'), 
        'B' => $this->t('B'),
        //I want C allway check and disable.
        'C' => $this->t('C'),
        'D' => $this->t('D'),
      ],
      '#default_value' => array('C'),
      //Follow topic https://www.drupal.org/node/284917. But don't work
      //'#disabled_values' => array('C'),
      '#title' => $this->t('Title Checkboxes'),
    ];

    return $form;
  }

  /**
   * Getter method for Form ID.
   */
  public function getFormId() {
    return 'form_checkboxes';
  }

  /**
   * Implements a form submit handler.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }

}

I try to follow this topic but its not working (8.1.x), and tried to search in google but it just solve in Drupal 7. So How can I disable a single checkbox in a 'checkboxes' FAPI element?

  • Please provide the code you've come up with that isn't working. That will help others answer your question. – hampercm Aug 22 '16 at 12:32
1

I find a way. Add option process in define form type.

<?php

namespace Drupal\fapi_example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class SimpleForm extends FormBase {

  /**
   * Build the simple form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['checboxes'] = [
      '#type' => 'checkboxes',
      '#options' => [
        'A' => $this->t('A'), 
        'B' => $this->t('B'),
        //I want C allways check and disable.
        'C' => $this->t('C'),
        'D' => $this->t('D'),
      ],
      //Add options process
      '#process' => [
        array($this, 'processCheckboxes'),
      ],
      '#default_value' => array('C'),
      //Follow topic https://www.drupal.org/node/284917. But don't work
      //'#disabled_values' => array('C'),
      '#title' => $this->t('Title Checkboxes'),
    ];

    return $form;
  }

  /**
   * Process checkboxes
   */
  public static function processCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) {
    $value = is_array($element['#value']) ? $element['#value'] : array();
    $element['#tree'] = TRUE;
    if (count($element['#options']) > 0) {
      if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
        $element['#default_value'] = array();
      }
      $weight = 0;
      foreach ($element['#options'] as $key => $choice) {
        if ($key === 0) {
          $key = '0';
        }
        $weight += 0.001;

        $element += array($key => array());
        $element[$key] += array(
          '#type' => 'checkbox',
          '#title' => $choice,
          '#return_value' => $key,
          '#default_value' => isset($value[$key]) ? $key : NULL,
          '#attributes' => $element['#attributes'],
          '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
          '#error_no_message' => TRUE,
          '#weight' => $weight,
        );

        if ($key == 'C') {
          $element[$key]['#disabled'] = TRUE;
          $element[$key]['#default_value'] = $key;
        }
      }
    }
    return $element;
  }

  /**
   * Getter method for Form ID.
   */
  public function getFormId() {
    return 'form_checkboxes';
  }

  /**
   * Implements a form submit handler.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }

}

It's working but i think FAPI allow options disable some value is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.