Issue #2016177 followup by amateescu, agentrickard: Regression: Refactor 'autocreate entity' handling from entity_reference().

8.0.x
Alex Pott 2013-06-29 20:08:33 +01:00
parent 3280bad1b9
commit af73fa7333
5 changed files with 15 additions and 5 deletions

View File

@ -109,7 +109,7 @@ function entity_reference_field_presave(EntityInterface $entity, $field, $instan
function entity_reference_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) {
$ids = array();
foreach ($items as $delta => $item) {
if (!empty($item['target_id']) && !$item['entity'] && !$item['entity']->isNew()) {
if (!empty($item['target_id']) && (!empty($item['entity']) && !$item['entity']->isNew())) {
$ids[$item['target_id']] = $delta;
}
}

View File

@ -258,11 +258,12 @@ class SelectionBase implements SelectionInterface {
*/
public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$target_type = $this->fieldDefinition->getFieldSetting('target_type');
$handler_settings = $this->fieldDefinition->getFieldSetting('handler_settings');
$entity_info = entity_get_info($target_type);
$query = \Drupal::entityQuery($target_type);
if (!empty($this->instance['settings']['handler_settings']['target_bundles'])) {
$query->condition($entity_info['entity_keys']['bundle'], $this->instance['settings']['handler_settings']['target_bundles'], 'IN');
if (!empty($handler_settings['target_bundles'])) {
$query->condition($entity_info['entity_keys']['bundle'], $handler_settings['target_bundles'], 'IN');
}
if (isset($match) && isset($entity_info['entity_keys']['label'])) {

View File

@ -40,7 +40,7 @@ abstract class EntityReferenceFormatterBase extends FormatterBase {
}
}
$target_type = $this->field['settings']['target_type'];
$target_type = $this->getFieldSetting('target_type');
$target_entities = array();

View File

@ -155,7 +155,7 @@ abstract class AutocompleteWidgetBase extends WidgetBase {
$target_bundles = $this->getSelectionHandlerSetting('target_bundles');
// Get the bundle.
if (!empty($target_bundles) && count($target_bundles) == 1) {
if (!empty($target_bundles)) {
$bundle = reset($target_bundles);
}
else {

View File

@ -64,6 +64,9 @@ class EntityReferenceAutoCreateTest extends WebTestBase {
),
))->save();
entity_get_display('node', $referencing->type, 'default')
->setComponent('test_field')
->save();
entity_get_form_display('node', $referencing->type, 'default')
->setComponent('test_field', array(
'type' => 'entity_reference_autocomplete',
@ -101,6 +104,7 @@ class EntityReferenceAutoCreateTest extends WebTestBase {
$result = $query->execute();
$this->assertTrue($result, 'Referenced node was created.');
$referenced_nid = key($result);
$referenced_node = node_load($referenced_nid);
// Assert the referenced node is associated with referencing node.
$result = \Drupal::entityQuery('node')
@ -110,5 +114,10 @@ class EntityReferenceAutoCreateTest extends WebTestBase {
$referencing_nid = key($result);
$referencing_node = node_load($referencing_nid);
$this->assertEqual($referenced_nid, $referencing_node->test_field[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'], 'Newly created node is referenced from the referencing node.');
// Now try to view the node and check that the referenced node is shown.
$this->drupalGet('node/' . $referencing_node->id());
$this->assertText($referencing_node->label(), 'Referencing node label found.');
$this->assertText($referenced_node->label(), 'Referenced node label found.');
}
}