On current dev branch referencing new entity fails when entity is unpublished by default.
Steps to reproduce on simplytest.me:
- Create a project with inline_entity_form (8.x-1.0-alpha6) and English standard profile.
- Enable module inline entity form.
- Add new content type Foo and unset default option published in publishing options.
- Add field Foo reference of type content reference (Allowed number of values: unlimited) to node type Article.
- For Foo reference choose Reference method Default and Content types Foo.
- In form display settings choose inline entity form - Complex and allow users to add new nodes.
- Add new user John with permission to create Article and Foo.
- Login as John and add new Article including new referenced Foo.
The new Foo is created but not the Article. It fails with message "This entity (node: %label) cannot be referenced.".
This error does NOT occure when:
- publishing options for Foo is set to published.
- You are logged in as admin.
AND it also fails with the same message when choosing reference method Views: Filter by an entity reference view, no matter what publishing options for Foo is set.
Steps to reproduce on simplytest.me like above but:
- Add a content view named Foo Entity Reference of type Foo
- Add Entitiy Reference to the view and remove Filter criteria Content: Publishing status (= Yes) from it.
- Set default option published in publishing options for content type Foo.
- For Foo reference field of Article choose Reference method Views: Filter by an entity reference view and your newly added entity reference view.
In this case the referenced entity validation seems to fail because the selection handler is not an instance of SelectionWithAutocreateInterface (see ValidReferenceConstraintValidator line 100: "If the selection handler does not support referencing newly created entities, all of them should be invalidated.").
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | ief_new_entity_fails.patch | 1.53 KB | jdhildeb |
| #6 | ief_new_entity_fails-2753553-6.patch | 1.48 KB | mariacha1 |
Comments
Comment #2
pminfComment #3
flodevelop commentedI think the issue when you choose reference method views is resolved with this patch :
https://www.drupal.org/node/2800875
Comment #4
pminfIt may be related to https://www.drupal.org/node/2800875.
Comment #5
rakugaki commentedFor the first one it looks like validation fails at:
/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
So 2 options are to subclass NodeSelection and write a better validateReferenceableNewEntities()... or implement hook_node_grants so that count($this->moduleHandler->getImplementations('node_grants') !== 0.
Comment #6
mariacha1 commentedThe code from NodeSelection is exactly what was causing the issue, but this isn't a problem on the Drupal\Core\Entity\Element\EntityAutocomplete form element, just the inline_entity_form element. The difference is that EntityAutocomplete also calls the NodeSelection's createNewEntity() method, which sets the published state to True during form submission.
IEF doesn't need to call the createNewEntity() function, though, because it creates the entity in another way. Besides, maybe we don't want to automatically publish every node people submit through IEF (thus disregarding the default published state).
I'm including a patch that works around this by adding a new property to the NodeInlineForm object "nodePublished". That property holds whether or not the inline node wanted to be published when the user hit "submit". Before validation, I setPublished to TRUE (same as NodeSelection does), then set it back to whatever "nodePublished" was before validation.
Comment #7
mariacha1 commentedComment #8
crizThe patch from #6 doesn't work for me in the second case "when choosing reference method Views: Filter by an entity reference view" (referencing a custom entity).
However, this one fixed it: #2800875: Autocreate for Entity Reference selection handlers that extend ViewsSelection
Comment #9
LauraRocksThe patch from #6 didn't work for me, using 8.x-1.0-beta1 and Drupal 8.4.
Anyway, found this module https://www.drupal.org/project/entity_reference_unpublished and with this patch was able to get in installed and it look ok https://www.drupal.org/node/2917012
Comment #10
liquidcms commentedNot sure if different issue.. but i have the same setup as original post; but in my case the Foo is not created (and neither is the Article).
I get the same error: This entity (node: New Place) cannot be referenced.
As the same user i try to create Foo directly; and this works fine.
I have applied the patch in #6 as well as enabled the entity_reference_unpublished module - no help.
if i set Foo to be published on creation - everything works as expected.
Comment #11
liquidcms commentedAs a work around i added a Business Rule (Rules module sadly doesn't have an Unpublish action) to set any new Foo as unpublished; and then set Foo to publish on creation.
surprisingly, this worked.
Comment #12
pminfI tried to solve the view reference issue by using the Entity Reference Unpublished module (based on a patch for User can't reference unpublished content even when they have access to it) but without success.
However I could reference the new entity by using patch #70 from Autocreate for Entity Reference selection handlers that extend ViewsSelection and selecting the newly provided reference method "Views: Filter by an entity reference view and allow autocreation". I'm wondering how exactly the IEF issue is related to this core issue.
Comment #13
jdhildeb commentedI've updated the patch from #6 for compatibility with Drupal 9.5. It solves the first use case in the issue description.
Comment #14
rupertj commentedThe first case in this issue is caused by https://www.drupal.org/project/drupal/issues/2845144, I suspect.
The latest patch from that issue (67 right now) fixes the ability to reference new unpublished entities for me. The patch also improves the code that comment 5 from this issue said was at fault. (And certainly is where I've also traced the issue to myself).