Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 8 how to alter paragraph form?

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

like image 718
Sourabh Bhutani Avatar asked Aug 31 '25 05:08

Sourabh Bhutani


1 Answers

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]
      ),
    );
  }

}
like image 187
Sourabh Bhutani Avatar answered Sep 05 '25 05:09

Sourabh Bhutani