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

CommentFileSizeAuthor
#2 3401297-addCacheableDependency.patch670 bytescmcintosh
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cmcintosh created an issue. See original summary.

cmcintosh’s picture

StatusFileSize
new670 bytes

Attaching patch for now.

john.oltman’s picture

Assigned: cmcintosh » 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.

john.oltman’s picture

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.

john.oltman’s picture

Title: Error: Call to a member function addCacheableDependencies() on null in Drupal\registration\Form\RegisterForm->addCacheableDependencies when viewing entities with Registration enabled, when displayed in a view block on a layout_builder enabled display » Host entity not set in Registration Form formatter in a listing

  • john.oltman committed 147b90a3 on 3.0.x
    Issue #3401297: Host entity not set in Registration Form formatter in a...
cmcintosh’s picture

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.

john.oltman’s picture

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.

cmcintosh’s picture

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.

john.oltman’s picture

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...

john.oltman’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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