Commit af73fa73 authored by alexpott's avatar alexpott

Issue #2016177 followup by amateescu, agentrickard: Regression: Refactor...

Issue #2016177 followup by amateescu, agentrickard: Regression: Refactor 'autocreate entity' handling from entity_reference().
parent 3280bad1
...@@ -109,7 +109,7 @@ function entity_reference_field_presave(EntityInterface $entity, $field, $instan ...@@ -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) { function entity_reference_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) {
$ids = array(); $ids = array();
foreach ($items as $delta => $item) { 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; $ids[$item['target_id']] = $delta;
} }
} }
......
...@@ -258,11 +258,12 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form ...@@ -258,11 +258,12 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form
*/ */
public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$target_type = $this->fieldDefinition->getFieldSetting('target_type'); $target_type = $this->fieldDefinition->getFieldSetting('target_type');
$handler_settings = $this->fieldDefinition->getFieldSetting('handler_settings');
$entity_info = entity_get_info($target_type); $entity_info = entity_get_info($target_type);
$query = \Drupal::entityQuery($target_type); $query = \Drupal::entityQuery($target_type);
if (!empty($this->instance['settings']['handler_settings']['target_bundles'])) { if (!empty($handler_settings['target_bundles'])) {
$query->condition($entity_info['entity_keys']['bundle'], $this->instance['settings']['handler_settings']['target_bundles'], 'IN'); $query->condition($entity_info['entity_keys']['bundle'], $handler_settings['target_bundles'], 'IN');
} }
if (isset($match) && isset($entity_info['entity_keys']['label'])) { if (isset($match) && isset($entity_info['entity_keys']['label'])) {
......
...@@ -40,7 +40,7 @@ public function prepareView(array $entities, $langcode, array &$items) { ...@@ -40,7 +40,7 @@ public function prepareView(array $entities, $langcode, array &$items) {
} }
} }
$target_type = $this->field['settings']['target_type']; $target_type = $this->getFieldSetting('target_type');
$target_entities = array(); $target_entities = array();
......
...@@ -155,7 +155,7 @@ protected function createNewEntity($label, $uid) { ...@@ -155,7 +155,7 @@ protected function createNewEntity($label, $uid) {
$target_bundles = $this->getSelectionHandlerSetting('target_bundles'); $target_bundles = $this->getSelectionHandlerSetting('target_bundles');
// Get the bundle. // Get the bundle.
if (!empty($target_bundles) && count($target_bundles) == 1) { if (!empty($target_bundles)) {
$bundle = reset($target_bundles); $bundle = reset($target_bundles);
} }
else { else {
......
...@@ -64,6 +64,9 @@ function setUp() { ...@@ -64,6 +64,9 @@ function setUp() {
), ),
))->save(); ))->save();
entity_get_display('node', $referencing->type, 'default')
->setComponent('test_field')
->save();
entity_get_form_display('node', $referencing->type, 'default') entity_get_form_display('node', $referencing->type, 'default')
->setComponent('test_field', array( ->setComponent('test_field', array(
'type' => 'entity_reference_autocomplete', 'type' => 'entity_reference_autocomplete',
...@@ -101,6 +104,7 @@ public function testAutoCreate() { ...@@ -101,6 +104,7 @@ public function testAutoCreate() {
$result = $query->execute(); $result = $query->execute();
$this->assertTrue($result, 'Referenced node was created.'); $this->assertTrue($result, 'Referenced node was created.');
$referenced_nid = key($result); $referenced_nid = key($result);
$referenced_node = node_load($referenced_nid);
// Assert the referenced node is associated with referencing node. // Assert the referenced node is associated with referencing node.
$result = \Drupal::entityQuery('node') $result = \Drupal::entityQuery('node')
...@@ -110,5 +114,10 @@ public function testAutoCreate() { ...@@ -110,5 +114,10 @@ public function testAutoCreate() {
$referencing_nid = key($result); $referencing_nid = key($result);
$referencing_node = node_load($referencing_nid); $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.'); $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.');
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment