1

The problem is all in the title. So here is my code (i cut some part to make it more visible) :

Constructor :

public function __construct() {
        $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
        $this->sub_forms['user'] = \Drupal::service('entity.form_builder')->getForm($user);
    }

buildForm :

public function buildForm(array $form, FormStateInterface $form_state) {
        $sub_forms_keys = array_keys($this->sub_forms);
        // INC ALL SUBFORM INTO THE MAIN FORM
        foreach ($this->sub_forms as $key => $sub_form) {
            // REMOVE THE PROCESS CALLBACK
            if (!empty($sub_form['#process'])) {
                unset($sub_form['#process']);
            }

            // ALTER THE SUBFORM
            if ($key == $sub_forms_keys[0]) { // user Alter
                foreach ($sub_form as $field_key => $sub_form_field) { // Pour tous les champs                
                    if (substr($field_key, 0, 10) != 'field_user') { // Si ce n'est pas un champ personnalisé friterie
                        unset($sub_form[$field_key]); // Cacher le champ
                    } else if (array_key_exists('widget', $sub_form[$field_key])) { // Si c'est un champ friterie et qu'il dispose d'un widget
                        $sub_form_field_widget = $sub_form[$field_key]['widget'][0];
                        $sub_form_field_widget['#states'] = array(
                            'visible' => array(':input[#edit-proprietaire-0]' => array('checked' => true))
                        );
                        dsm($sub_form_field_widget);
                    }
                }
            }

            // ADD THE SUBFORM INTO THE MAIN FORM
            $form[$key] = [
                '#type' => 'container',
                'form' => $sub_form
            ];
        }
        return $form;
    }

Here is a pastern link of a dsm() of a field to show you the structure and that the #states is include : http://pastebin.com/1a8AqVzm

Then here is the HTML code of the entity i want to focus (it's a radio button) :

<input class="clear-panel-field panel-field-inline container-inline form-radio" data-drupal-selector="edit-proprietaire-0" type="radio" id="edit-proprietaire-0" name="proprietaire" value="0">
1
  '#states' => array(
  'visible' => array(
    ':input[name="your checkbox"]' => array('checked' => TRUE),
  ),
)

add this to the element you want to hide.this will be visible only when the checkbox is checked.

0
'#states' => array(
   'visible' => array(
      ':input[name="check box name"]' => array('value' => 'value'),
    ),
   ),

This will hide dependency field based on input checked, even if it true.

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.