Score:0

How to access paragraph fields from a webform's form?

cn flag

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?

ru flag
Does this answer your question? [How do I get $entity in hook\_form\_alter()?](https://drupal.stackexchange.com/questions/260843/how-do-i-get-entity-in-hook-form-alter)
Md. Shafiq Hossain avatar
cn flag
Sorry that is completely different issue. If I get the entity inside hook_form_alter(), it will return webform object not the paragraph.
ru flag
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()](https://github.com/drupalprojects/webform/blob/e5a82adbdd323b5aa506f5226adf15dd1d7e9b11/src/Plugin/WebformSourceEntityManager.php#L41)
Md. Shafiq Hossain avatar
cn flag
Ah! you are absolutely correct!! You made my day. Thank you so much!!
Md. Shafiq Hossain avatar
cn flag
Hi @Hudri, Would you submit the answer? Thanks again for your answer.
Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.