Problem/Motivation

Current the webform submission form includes the source entity type and id in the form ID.

hook_form_{BASE_FORM_ID}_{WEBFORM_ID}_{ENTITY_TYPE}_{ENTITY_ID}_{OPERATION}_form_alter()

Most form alteration should target just the webform id and operation.

hook_form_{BASE_FORM_ID}_{WEBFORM_ID}_{OPERATION}_form_alter()

Maybe we should remove the source entity type and id from the form id.

Proposed resolution

Remove source entity from webform submission form ids

Remaining tasks

  • Remove source entity from form id
  • Fix broken tests
  • Write change record

User interface changes

TDB

API changes

TDB

Data model changes

TDB

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jrockowitz created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review
FileSize
623 bytes

Status: Needs review » Needs work

The last submitted patch, 2: 2949523-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
1.71 KB

Status: Needs review » Needs work

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

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
2.37 KB
bucefal91’s picture

hello, Jacob! :)

Yes, that looks fine to me. I do not have much to suggest. The only thing I see is the new WebformSubmissionForm::getFormId() looks just as its parent. So we can remove the method as a whole and use parent's implementation of that method. Hehe, you can tell I really like removing code :)

Just out of curiosity I glanced into webform_form_webform_submission_form_alter() and there are some findings.
It seems that you're running another set of form_alters in there:

  $hooks = [];
  $hooks[] = 'form_webform_submission_' . $webform->id() . '_form';
  if ($source_entity) {
    $hooks[] = 'form_webform_submission_' . $webform->id() . '_' . $source_entity->getEntityTypeId() . '_' . $source_entity->id() . '_form';
  }

  $form_id = $form_object->getFormId();
  \Drupal::service('module_handler')->alter($hooks, $form, $form_state, $form_id);
  \Drupal::service('theme.manager')->alter($hooks, $form, $form_state, $form_id);

Notice, that webform_form_webform_submission_form_alter() has an IF statement to add one more hook if the current webform has source entity. Maybe you want to remove that part too?

Lastly, I attach the patch that removes ::getFormId().

jrockowitz’s picture

@bucefal91 Does this change make sense? Is including the source entity in the webforms id making it too specific?

This change is going to break some people code but simplify the webform module's form alter hooks.

bucefal91’s picture

Yep, it sounds reasonable to me. I do not expect many people would care for the source entity. And those (hopefully few) who do care, still can include necessary IF statements inside of their implementation of hook_form_webform_submission_WEBFORM_ID_form_alter().

I think with the patch committed there is still plenty machinery to alter the form for custom module developers.

  • jrockowitz committed 042bbc8 on 8.x-5.x
    Issue #2949523 by jrockowitz, bucefal91: Remove source entity from...
jrockowitz’s picture

Status: Needs review » Fixed
Berdir’s picture

FYI: One thing this broke for us is a captcha configuration for the old form_id. I know that this can be added directly in webform and I asume most people are doing it like that, and it actually only was there for historical reasons in the default config of our distribution (originally we used contact.module, had an captcha example form for it, that was then migrated to webform at some point but we kept the configuration in captcha).

jrockowitz’s picture

@Berdir If you have to write a custom update hook to fix the regression with CAPTCHA module. Please create a new ticket with the code snippet. I would be open to adding it the webform module's update hooks.

jrockowitz’s picture

Now this change has caused this regression #2957002: Same webform multiple times on the same page. This patch might have to be reverted.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kmajzlik’s picture

Please re-open this. It is not fixed after commit revert. There is no easy solution to add Captcha now if you have webform shown on all nodes of certain type for example.

Probably there can be some setting on Webform entity.

Or maybe do something with getBaseFormId() ?

jrockowitz’s picture

Please create a new ticket. I do want to note that we can't make any changes to any form ids because it would break everyone's current form alter hooks.