diff --git a/src/Feeds/Target/EntityReference.php b/src/Feeds/Target/EntityReference.php index 25c2fec2b427a2cc4fb969ad78522916fc3d353e..4705a34cce18450a70cc90289ac3784235b93abf 100644 --- a/src/Feeds/Target/EntityReference.php +++ b/src/Feeds/Target/EntityReference.php @@ -322,7 +322,7 @@ class EntityReference extends FieldTargetBase implements ConfigurableTargetInter return $target_ids; } - if ($this->configuration['autocreate'] && $field === $this->getLabelKey()) { + if ($this->hasAutocreateSupport() && $this->configuration['autocreate'] && $field === $this->getLabelKey()) { return [$this->createEntity($search)]; } @@ -362,18 +362,30 @@ class EntityReference extends FieldTargetBase implements ConfigurableTargetInter return $entity->id(); } + /** + * Determines if this target has autocreate support. + * + * @return bool + * TRUE if supported, FALSE otherwise. + */ + protected function hasAutocreateSupport() { + return TRUE; + } + /** * {@inheritdoc} */ public function defaultConfiguration() { $config = parent::defaultConfiguration() + [ 'reference_by' => $this->getLabelKey(), - 'autocreate' => FALSE, - 'autocreate_bundle' => FALSE, ]; if (array_key_exists('feeds_item', $this->getPotentialFields())) { $config['feeds_item'] = FALSE; } + if ($this->hasAutocreateSupport()) { + $config['autocreate'] = FALSE; + $config['autocreate_bundle'] = FALSE; + } return $config; } @@ -425,41 +437,43 @@ class EntityReference extends FieldTargetBase implements ConfigurableTargetInter ], ]; - $form['autocreate'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Autocreate entity'), - '#default_value' => $this->configuration['autocreate'], - '#states' => [ - 'visible' => [ - ':input[name="mappings[' . $delta . '][settings][reference_by]"]' => [ - 'value' => $this->getLabelKey(), - ], - ], - ], - ]; - - $bundles = $this->getBundles(); - if (count($bundles) > 0) { - - // Check that recent field configuration changes haven't invalidated any - // previous selection. - if (!in_array($this->configuration['autocreate_bundle'], $bundles)) { - $this->configuration['autocreate_bundle'] = reset($bundles); - } - - $form['autocreate_bundle'] = [ - '#type' => 'select', - '#title' => $this->t('Bundle to autocreate'), - '#options' => $bundles, - '#default_value' => $this->configuration['autocreate_bundle'], + if ($this->hasAutocreateSupport()) { + $form['autocreate'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Autocreate entity'), + '#default_value' => $this->configuration['autocreate'], '#states' => [ 'visible' => [ - ':input[name="mappings[' . $delta . '][settings][autocreate]"]' => [ - ['checked' => TRUE, 'visible' => TRUE], + ':input[name="mappings[' . $delta . '][settings][reference_by]"]' => [ + 'value' => $this->getLabelKey(), ], ], ], ]; + + $bundles = $this->getBundles(); + if (count($bundles) > 0) { + + // Check that recent field configuration changes haven't invalidated any + // previous selection. + if (!in_array($this->configuration['autocreate_bundle'], $bundles)) { + $this->configuration['autocreate_bundle'] = reset($bundles); + } + + $form['autocreate_bundle'] = [ + '#type' => 'select', + '#title' => $this->t('Bundle to autocreate'), + '#options' => $bundles, + '#default_value' => $this->configuration['autocreate_bundle'], + '#states' => [ + 'visible' => [ + ':input[name="mappings[' . $delta . '][settings][autocreate]"]' => [ + ['checked' => TRUE, 'visible' => TRUE], + ], + ], + ], + ]; + } } return $form; @@ -488,7 +502,7 @@ class EntityReference extends FieldTargetBase implements ConfigurableTargetInter ]; } - if ($this->configuration['reference_by'] === $this->getLabelKey()) { + if ($this->hasAutocreateSupport() && $this->configuration['reference_by'] === $this->getLabelKey()) { $create = $this->configuration['autocreate'] ? $this->t('Yes') : $this->t('No'); $summary[] = $this->t('Autocreate entities: %create', ['%create' => $create]); if ($this->configuration['autocreate'] && in_array($this->configuration['autocreate_bundle'], $this->getBundles())) { diff --git a/src/Feeds/Target/File.php b/src/Feeds/Target/File.php index b45926340dd439a19555a12d1c2a4d86cf89beed..b86ad3e47d58185379eabc12bfc84b5442d629ed 100644 --- a/src/Feeds/Target/File.php +++ b/src/Feeds/Target/File.php @@ -415,4 +415,11 @@ class File extends EntityReference { } } + /** + * {@inheritdoc} + */ + protected function hasAutocreateSupport() { + return FALSE; + } + } diff --git a/tests/src/Functional/Feeds/Target/EntityReferenceTest.php b/tests/src/Functional/Feeds/Target/EntityReferenceTest.php index 92ccc987013e40b4f6bc088dcccb2a337ebdaa85..fb5aef9220ed1d0b084e1ba168ccf66610b511bf 100644 --- a/tests/src/Functional/Feeds/Target/EntityReferenceTest.php +++ b/tests/src/Functional/Feeds/Target/EntityReferenceTest.php @@ -80,6 +80,8 @@ class EntityReferenceTest extends FeedsBrowserTestBase { $this->assertSession()->optionExists('mappings[2][settings][autocreate_bundle]', 'foo'); $this->assertSession()->optionExists('mappings[2][settings][autocreate_bundle]', 'qux'); $this->assertSession()->optionNotExists('mappings[2][settings][autocreate_bundle]', 'bar'); + // Assert that the autocreate field exists. + $this->assertSession()->fieldExists('mappings[2][settings][autocreate]'); } } diff --git a/tests/src/Functional/Feeds/Target/FileTest.php b/tests/src/Functional/Feeds/Target/FileTest.php index d097d73efd406f1e77aab4ee9f2fa5833d2b71c9..ddb1ac7ee4f1d855ad112a76450731416eadaf48 100644 --- a/tests/src/Functional/Feeds/Target/FileTest.php +++ b/tests/src/Functional/Feeds/Target/FileTest.php @@ -39,7 +39,7 @@ class FileTest extends FeedsBrowserTestBase { /** * Tests importing several files. */ - public function test() { + public function testImport() { // Create a feed type for importing nodes with files. $feed_type = $this->createFeedTypeForCsv([ 'title' => 'title', @@ -78,6 +78,36 @@ class FileTest extends FeedsBrowserTestBase { } } + /** + * Tests that configuring a file target on the mapping page works. + */ + public function testConfigureTarget() { + // Create a feed type. + $feed_type = $this->createFeedTypeForCsv([ + 'guid' => 'guid', + 'title' => 'title', + ]); + + // Go to the mapping page, and a target to 'field_file'. + $edit = [ + 'add_target' => 'field_file', + ]; + $this->drupalGet('/admin/structure/feeds/manage/' . $feed_type->id() . '/mapping'); + $this->submitForm($edit, 'Save'); + + // Check editing target configuration. + $edit = []; + $this->submitForm($edit, 'target-settings-2'); + + // Assert that certain fields appear. + $this->assertSession()->fieldExists('mappings[2][settings][reference_by]'); + $this->assertSession()->fieldExists('mappings[2][settings][existing]'); + + // Assert that the autocreate field does not exist, since the file target + // does not support that feature. + $this->assertSession()->fieldNotExists('mappings[2][settings][autocreate]'); + } + /** * Lists test files. */