Problem/Motivation

If you are using the Entity Field API to create and save entities programatically, Entity Reference fields are picky about the order in which the entities are saved.
Example
Create an unsaved term.
Create an unsaved node and assign the term to a term ER field.
Save the term.
Save the node.
The ER field loses the value.

Proposed resolution

Make isEmpty handle this case.
Make preSave deal with it too.

Remaining tasks

Review

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan’s picture

Issue summary: View changes
FileSize
1.31 KB
3.9 KB

Meh, wrong patch

larowlan’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: er-save-order.pass_.patch, failed testing.

The last submitted patch, 1: er-save-order.fail_.patch, failed testing.

Status: Needs work » Needs review

larowlan queued 1: er-save-order.pass_.patch for re-testing.

larowlan queued 1: er-save-order.fail_.patch for re-testing.

The last submitted patch, 1: er-save-order.fail_.patch, failed testing.

andypost’s picture

Status: Needs review » Needs work
  1. +++ b/core/includes/theme.inc
    @@ -1493,6 +1493,7 @@ function template_preprocess_feed_icon(&$variables) {
    +  $variables['attributes']['class'] = array('feed-icon');
    

    is this related?

  2. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
    @@ -208,6 +212,9 @@ public function preSave() {
    +    if (empty($this->target_id) && $this->entity) {
    +      $this->target_id = $this->entity->id();
    

    this needs code comment

  3. +++ b/core/modules/system/templates/feed-icon.html.twig
    @@ -15,4 +15,4 @@
    -<a href="{{ url }}"{{ attributes.addClass('feed-icon') }}>{{ icon }}</a>
    +<a href="{{ url }}"{{ attributes }}>{{ icon }}</a>
    
    +++ b/core/modules/system/templates/status-messages.html.twig
    @@ -24,13 +24,7 @@
    -  <div class="{{ attributes.addClass(classes).class }}" role="contentinfo" aria-label="{{ status_headings[type] }}">
    +  <div class="messages messages--{{ type }}" role="contentinfo" aria-label="{{ status_headings[type] }}">
    

    unrelated?

larowlan’s picture

Status: Needs work » Needs review
FileSize
846 bytes
2.56 KB

yep unrelated hunks - fixed and added comment

Berdir’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
@@ -193,6 +194,9 @@ public function isEmpty() {
+    if ($this->entity && $this->entity instanceof Entity && !$this->entity->isNew()) {
+      return FALSE;
+    }
     // Allow auto-create entities.
     if ($this->hasUnsavedEntity()) {
       return FALSE;

@@ -208,6 +212,13 @@ public function preSave() {
       $this->target_id = $this->entity->id();
     }
+    // Handle the case where an unsaved entity was directly set using the public
+    // 'entity' property and then saved before this entity. In this case
+    // ::hasUnsavedEntity() will return FALSE but $this->target_id will still be
+    // empty.
+    if (empty($this->target_id) && $this->entity) {
+      $this->target_id = $this->entity->id();
+    }

It looks like those two cases could be combined better then?

Can't the two checks simply be combined to a single $this->entity instanceof EntityInterface in isEmpty() and below, remove the target_id call inside hasUnsavedEntity() ?

larowlan’s picture

Status: Needs work » Needs review
FileSize
2.54 KB
1.09 KB

Thanks - great points - and thanks for explaining on irc

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. You had to be running into this or you wouldn't have created the issue, so I guess others will too.

larowlan’s picture

Yep was working on content staging previews

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: er-save-order.4.patch, failed testing.

Berdir queued 11: er-save-order.4.patch for re-testing.

Berdir’s picture

Status: Needs work » Reviewed & tested by the community

This was a random fail.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7340fee and pushed to 8.0.x. Thanks!

  • alexpott committed 7340fee on 8.0.x
    Issue #2349605 by larowlan: Fixed EntityReferenceItem is fragile about...

Status: Fixed » Closed (fixed)

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