Host entity not set in Registration Form formatter in a listing

Created on 13 November 2023, 8 months ago
Updated 15 November 2023, 7 months ago

Problem/Motivation

Error: Call to a member function addCacheableDependencies() on null in Drupal\registration\Form\RegisterForm->addCacheableDependencies() (line 598 of /code/web/modules/composer/registration/src/Form/RegisterForm.php)

Displayed when viewing a Node with registrations enabled from a view displayed in a layout_builder layout. Context is lost, and so the addCacheableDependencies() gets an error.

Steps to reproduce

1. Create a node type and enable registration. Then create a node with the registration.
2. Create a Teaser display mode for that node type, where the registration form is set as disabled from view.
3. Create a view that displays that node type rendered with that teaser display.
4. On another entity/node type (in my case a Taxonomy term), enable layout builder for the default display and place the view on it as a block.
5. View the Taxonomy term page.

Proposed resolution

Update the addCacheableDependencies to check to make sure that host_entity is available, prior to trying to add a cacheable dependency to it.

Remaining tasks

- Create patch, and submit it to this issue.
- Community review or alternative suggestions.

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Registration Core

Created by

πŸ‡ΊπŸ‡ΈUnited States cmcintosh

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @cmcintosh
  • πŸ‡ΊπŸ‡ΈUnited States cmcintosh

    Attaching patch for now.

  • Assigned to john.oltman
  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    Thanks for the post @cmcintosh! Patch looks good, I want to reproduce this locally, will get back later this week. Would like to include a test with the fix, so may open an MR eventually, tbd.

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    The problem is deeper - but I could only reproduce it by having the Registration field enabled for the teaser display. If you disable it, it will never call RegisterForm, because it never needs to render. So I think you probably meant to say that the field was enabled, with Registration form being the chosen Format for the field in that view mode.

    I'll have a MR posted soon.

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman
  • @johnoltman opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States cmcintosh

    I have seen it in both cases, additionally this is just a part of a bigger issue I think with how the form is working.

    in setHostEntity(FormStateInterface $form_state), we are first checking if $form_state already has a host entity, if not we fall back to using routeMatch paramaters, which will fail in this situation: $this->getRouteMatch()->getParameters().

    I think the more/better solution would be to add a third parameter onto the form method, that takes a \Drupal\Core\Entity\ContentEntityInterface object. Then in the Field Formatter, pass in the host entity when calling the form to be rendered. This would ensure that the host entity is always present.

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    Thanks - you may have missed that I had already committed a fix roughly along the lines of what you are proposing. A third argument is not needed since the form state can be initialized with the host entity. I kept the setHostEntity function but only as a fallback for other callers. This is committed to the dev branch and will be in the next release.

  • πŸ‡ΊπŸ‡ΈUnited States cmcintosh

    Hey John, yes I saw that the patch was accepted. Thanks for that. I overlooked that function was accessible, so that should handle other cases. Thanks again.

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    No problem. To clarify, although it was much appreciated, I did not use the patch since it was covering up the root cause. This is what I ended up doing which is basically your proposal to add the host entity to the form via the field formatter. https://git.drupalcode.org/project/registration/-/commit/147b90a39279e67...

  • Status changed to Fixed 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States john.oltman
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024