4

In drupal 8, I'm using paragraph in node. It's working fine but I got stuck to alter paragraph form. I want to hide one field on base of other field value.

Please help if somebody worked it on before

Sourabh Bhutani
  • 623
  • 11
  • 21

1 Answers1

3

I found below code helpful and fixed my problem. I hope it will be useful for others also.

function hook_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
 
// $entity_form['#bundle']  paragraph machine name
  if($entity_form['#entity_type'] == 'paragraph' && $entity_form['#bundle'] == 'location'){
    $parents = $entity_form['#parents'];
    $identifier = $parents[0].'['.implode('][', array_slice($parents, 1)).']';
    $entity_form['field_dropoff_time']['#states'] = array(
      'visible' => array(
        'select[name="'. $identifier .'[field_camp_location_type]"]' => ['value' => 1]
      ),
    );
    $entity_form['field_pickup_time']['#states'] = array(
      'visible' => array(
        'select[name="'. $identifier .'[field_camp_location_type]"]' => ['value' => 1]
      ),
    );
  }

}
Sourabh Bhutani
  • 623
  • 11
  • 21