Problem/Motivation

This relates to using webform in conjunction with the contributed paragraphs module, which provides a Paragraph content entity type. Paragraphs have a parent entity.

When a paragraph entity contains a webform entity reference field, it's usually desirable to use that paragraph's parent as the webform's source entity, rather than the paragraph entity itself.

There's currently no consistent way to do this.

A workaround exists for embedded forms: set set submission source entity to no). That allows the source entity to be derived from the current page. No such workaround is possible with the link to webform formatter.

Steps to reproduce

  1. Create a node type with a "paragraphs" field.
  2. Create a paragraph type that has a "webform" entity reference field.
  3. On the manage display settings for the paragraph type, choose link to webform for the webform field.
  4. Create a webform.
  5. Create a node, add a paragraph and populate the field in step 2 with the webform in step 4.
  6. View the node. The webform's source entity will be that of the paragraph, not the node.

Proposed resolution

The WebformSourceEntityManager class has a function getMainSourceEntity which seems to test for this: if the entity is a paragraph, use the parent. Otherwise use the entity itself.

Adapt the link to formatter in some way to use this function.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

erik.erskine created an issue. See original summary.

jrockowitz’s picture

Version: 6.0.2 » 8.x-5.x-dev
Category: Feature request » Bug report
Priority: Normal » Major

Ouch!!! This is a major bug impacting 8.x-5.x and 6.x

I completely agree that the source entity for paragraphs is the main node, and this is a regression.

I am not sure if or how to apply the patch because some sites might be relying on the fact that paragraphs are mistakenly used as the source entity.

jrockowitz’s picture

jrockowitz’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: 3209462-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
766 bytes
paulocs’s picture

I tested patch #6 but the link still refers to the paragraph as source_entity.
I created a new patch and I'm using the same approach as in WebformEntityReferenceEntityFormatter.

jrockowitz’s picture

It is okay if the link refers to the paragraph. when the submission form is generated the source entity should be the node.

paulocs’s picture

Got it! I thought the link should refers to the node.
Should I merge #6 into #7 or just re-add patch #6?

jrockowitz’s picture

We need to confirm if this is regression. If it is a regression we might need to provide an update hook to change any paragraph source entity to point to the main node.

jrockowitz’s picture

paulocs’s picture

I don't know how it is possible to know the right webform submission entity_type and entity_id to update them in a hook.
I did not find anything that tracked it.
Eg: how can I know which node id is the correct one for the webform submission if I have two different nodes that have a paragraph field with the webform in it.

jrockowitz’s picture

First, we need to install an older version (around #3021262: Unable to get token value [webform_submission:source-entity:?] from a paragraph field) of webform and see how paragraphs were handled.

What I have done previously is check out an older release tag and gradually increment the release number.

paulocs’s picture

Okay. I did a little a research and I think I found when the problem was introduced.
The bug start to occur after this change.
In WebformSubmission.php there is the method preCreate() that handle the $source_entity.
See:

$request_handler = \Drupal::service('webform.request');
    $source_entity = $request_handler->getCurrentSourceEntity('webform');
    $values += [
      'entity_type' => ($source_entity) ? $source_entity->getEntityTypeId() : NULL,
      'entity_id' => ($source_entity) ? $source_entity->id() : NULL,
    ];

It is here where the source_entity and entity_id are handle.

jrockowitz’s picture

Can you pinpoint the issue number where I introduced the regression? From there we can determine which release introduced this regression.

paulocs’s picture

jrockowitz’s picture

I am having a very hard time understanding how paragraph source entity handling is currently set up (and I wrote the code). I am not every sure if there is a regression or just some poorly written code.

I am going to have to use the webform_test_paragraphs.module to walk through all the possible scenarios and document what is happening.

darvanen’s picture

Came here to say two things:

  1. Technically the paragraph is the source entity, at least in my setup. It has fields I want to use in the submission handler.
  2. The code currently doesn't respect paragraph revisions - if I change the value of a field and save, the [webform_submission:source-entity:field_my_field] token picks up the old value.

Regarding 1: Would [webform_submission:root_entity] work for the node and leave the immediate parent available as [webform_submission:source-entity]? Personally I think after 2 years, reversing the change would be a bigger risk.

Regarding

if the entity is a paragraph, use the parent.

Don't forget that sometimes people nest paragraphs.

Happy to do work on the code or more research, just need some direction.

jrockowitz’s picture

@Darvane I appreciate the feedback and help. I have to find the time to do an assessment of the problem (because I created it) and then I need help reviewing and resolving the problem.

I think what is happening is the source entity is changing for paragraphs based on different contexts (ie. the source entity on the field is different than when a webform is built and rendered.)

darvanen’s picture

Status: Needs review » Needs work

That sounds like a good place to start, ​there's a WebfomSubmission->getSourceEntity() that is used by tokens and WebformRequest->getCurrentSourceEntity() that is called by a bunch of methods in WebformSubmission.

The former uses the WebformSourceEntityManager, the latter does not.

$source_entity is set twice, differently, within WebformSubmission->preCreate() though possibly for different purposes.

I don't see any reference to revisions anywhere in any of the three files (though a text-find may have missed it somehow).

I'm going to keep digging for a while and see if I can make heads or tails of this - as far as I'm concerned, a maintainer's responisibility is to make sure incoming code fits the design/plan and passes the requisite policies etc, not fix everything, even bugs they "created" ;)

Your guidance of course, is most welcome - for instance, should we split this issue into two? Or are these two issues so intertwined that they should be worked on together?:

- What is the source entity? The Paragraph or the eventual Node - or other entity containing Paragraphs that is loaded by the page? Can we have both?
- Why are changes to paragraph field values not flowing through to webform_submission:source-entity tokens?

jrockowitz’s picture

- What is the source entity? The Paragraph or the eventual Node - or other entity containing Paragraphs that is loaded by the page? Can we have both?

I think the issue is that via the field you can decide if the paragraph or the node is the source entity but on the WebformSubmissionForm the source entity for tokens is always the node.

- Why are changes to paragraph field values not flowing through to webform_submission:source-entity tokens?

The patch from #6 is revealing this issue.

My concern is any fix or change is going to mess up existing installations.

darvanen’s picture

Ah... I see.

So patch #6 makes the submission form use the node instead of the paragraph for the source entity, which is what I came here to request a re-think of. This would break my "mistaken" usage of this token.

It's easily changed with enough warning but we're using a field on the paragraph to populate a handler which gives the submitting user a tag on Mailchimp that is the string set by the editor on the paragraph.

Is it possible to keep `source_entity` as the immediate parent as it has been for the past X? years and introduce a new token like `root_entity` for the use case in the OP?

jrockowitz’s picture

jrockowitz’s picture

Title: Webform entity reference field: allow a paragraph's parent entity to be used as the webform's source entity » [Paragraphs] Webform entity reference field: allow a paragraph's parent entity to be used as the webform's source entity
jrockowitz’s picture

Version: 8.x-5.x-dev » 6.2.x-dev
darvanen’s picture

In case it makes any difference, the project I was working on has removed that implementation described in #22 :)

jrockowitz’s picture

These paragraph issues are so tricky. I think the best we can do is develop reliable, documented workarounds, and patches.

darvanen’s picture

Sounds about right to me, it's like a hydra.

jrockowitz’s picture

Paragraphs = Hydra might make into my next webform presentation.