0

I have a webform (contact_info) which is attached to a paragraph's field (field_contact_info). The paragraph has other fields as well.

I implemented hook_form_alter() to change some attributes inside the webform's form.

Is there any way to access the values of other paragraph fields value in hook_form_alter()?

I can pass them inside the webform object, but those aren't available in hook_form_alter() or the form array.

Any help on this?

5
  • Does this answer your question? How do I get $entity in hook_form_alter()?
    – Hudri
    Aug 20 at 12:20
  • Sorry that is completely different issue. If I get the entity inside hook_form_alter(), it will return webform object not the paragraph. Aug 20 at 12:28
  • This is quite the same issue. Once you have the host entity for the form, you can query whatever entity and whatever field you want. Remember Webform has a getSourceEntity()
    – Hudri
    Aug 20 at 12:51
  • Ah! you are absolutely correct!! You made my day. Thank you so much!! Aug 20 at 13:06
  • Hi @Hudri, Would you submit the answer? Thanks again for your answer. Aug 20 at 14:00
0

This question is answered by @hudri

inside the hook_form_alter() get the entity first:

if ($form_state->getFormObject() instanceof \Drupal\Core\Entity\EntityFormInterface) {
  $entity = $form_state->getFormObject()->getEntity();
}

Then get the parent paragraph using this

$entity->getSourceEntity()

This will return the paragraph instance with all the fields.

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.