Sourabh Bhutani
Sourabh Bhutani

Reputation: 663

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

Upvotes: 4

Views: 5803

Answers (1)

Sourabh Bhutani
Sourabh Bhutani

Reputation: 663

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

}

Upvotes: 3

Related Questions