0

I'm using Drupal 8.7.8 and Webform 5.4.0. I have multiple section nodes that are displayed on course nodes. These are 2 different content types.

I'm adding webforms to sections, via the twig file conditionally (not all sections) with {{ drupal_entity('webform', 'my_form_id') }} and I need to pass the value of field_code of the section into a hidden field on the webform.

Once the form is rendered on the course page, the field_code value passed belongs to the course node rather than the section. How can I access the field_code value on the section?

I tried adding a hidden field with this token as the default [webform_submission:node:field_code:value], which works when I test the form on the section, but when I test the form on the course (where the user will access it) I'm getting the field_code value of the course.

What's the best way to handle this? Is there a way to set the source entity to the content type that the form is on? Please let me know if I need to add more info.

Thanks!

2
  • I don't think it's possible with drupal_entity because the fallback function of the drupal_entity extension accept 5 argument $entity_type $id $view_mode $langcode and $check_access, so i suggest to develop new extension like drupal_entity with another argument [field_machine_name => value] to set value of field
    – berramou
    Nov 2, 2019 at 13:43
  • Thanks @berramou! I've edited my question to clarify how I'm using the token. I'm not setting the field value with drupal_entity, I'm just rendering the form on the twig with it. But, maybe I should be rendering it some other way.
    – cd_g
    Nov 2, 2019 at 20:07

1 Answer 1

0

I'm answering this with how I solved it in case anyone else has the same question. Maybe my question could be worded better though...

I was able to get this working with the Webform Node module.

I installed webform node. Then, I added a webform field to my content type. I selected the form I wanted as the default field settings. I selected the "Use this field's Content entity as the webform submission's source entity." checkbox.

I added my form to the display of the nodes I wanted it to be on in the content type's twig:

{% if node.field_name.entity.label == "My condition label" %}
  {{ content.field_name_of_webform_field }}
{% endif %}

Now, on my course page the same form appears for each section and I get the code for the section that the person filled-out the form on in the [webform_submission:source-entity:field_code:value] token!!

A couple of the resources that helped me: https://www.youtube.com/watch?v=B_ZyCOVKPqA https://www.webwash.net/moving-forward-webform-drupal-8/#attaching-a-webform-to-a-node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.