Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
7340fee0
Commit
7340fee0
authored
Oct 12, 2014
by
Alex Pott
Browse files
Issue
#2349605
by larowlan: Fixed EntityReferenceItem is fragile about entity save order.
parent
c2ac32c9
Changes
2
Hide whitespace changes
Inline
Side-by-side
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
View file @
7340fee0
...
...
@@ -7,6 +7,7 @@
namespace
Drupal\Core\Field\Plugin\Field\FieldType
;
use
Drupal\Core\Entity\Entity
;
use
Drupal\Core\Entity\EntityTypeInterface
;
use
Drupal\Core\Entity\TypedData\EntityDataDefinition
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
...
...
@@ -193,8 +194,7 @@ public function isEmpty() {
if
(
$target_id
!==
NULL
)
{
return
FALSE
;
}
// Allow auto-create entities.
if
(
$this
->
hasUnsavedEntity
())
{
if
(
$this
->
entity
&&
$this
->
entity
instanceof
Entity
)
{
return
FALSE
;
}
return
TRUE
;
...
...
@@ -206,6 +206,12 @@ public function isEmpty() {
public
function
preSave
()
{
if
(
$this
->
hasUnsavedEntity
())
{
$this
->
entity
->
save
();
}
// 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
();
}
}
...
...
core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php
View file @
7340fee0
...
...
@@ -168,4 +168,25 @@ public function testConfigEntityReferenceItem() {
$entity
->
save
();
}
/**
* Test saving order sequence doesn't matter.
*/
public
function
testEntitySaveOrder
()
{
// The term entity is unsaved here.
$term
=
entity_create
(
'taxonomy_term'
,
array
(
'name'
=>
$this
->
randomMachineName
(),
'vid'
=>
$this
->
term
->
bundle
(),
'langcode'
=>
LanguageInterface
::
LANGCODE_NOT_SPECIFIED
,
));
$entity
=
entity_create
(
'entity_test'
);
// Now assign the unsaved term to the field.
$entity
->
field_test_taxonomy_term
->
entity
=
$term
;
$entity
->
name
->
value
=
$this
->
randomMachineName
();
// Now save the term.
$term
->
save
();
// And then the entity.
$entity
->
save
();
$this
->
assertEqual
(
$entity
->
field_test_taxonomy_term
->
entity
->
id
(),
$term
->
id
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment