diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php index 493d6a4f67..a6da7ab898 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php @@ -145,12 +145,13 @@ public function massageFormValues(array $values, array $form, FormStateInterface */ protected function getAutocreateBundle() { $bundle = NULL; - if ($this->getSelectionHandlerSetting('auto_create') && $target_bundles = $this->getSelectionHandlerSetting('target_bundles')) { + if ($this->getSelectionHandlerSetting('auto_create')) { // If there's only one target bundle, use it. + $target_bundles = $this->getSelectionHandlerSetting('target_bundles'); if (count($target_bundles) == 1) { $bundle = reset($target_bundles); } - // Otherwise use the target bundle stored in selection handler settings. + // Otherwise use the autocreate bundle stored in selection handler settings. elseif (!$bundle = $this->getSelectionHandlerSetting('auto_create_bundle')) { // If no bundle has been set as auto create target means that there is // an inconsistency in entity reference field settings. @@ -161,7 +162,6 @@ protected function getAutocreateBundle() { ), E_USER_WARNING); } } - return $bundle; } diff --git a/core/modules/views/config/schema/views.entity_reference.schema.yml b/core/modules/views/config/schema/views.entity_reference.schema.yml index f13645ab43..02ff1e6cc6 100644 --- a/core/modules/views/config/schema/views.entity_reference.schema.yml +++ b/core/modules/views/config/schema/views.entity_reference.schema.yml @@ -20,3 +20,9 @@ entity_reference_selection.views: sequence: type: string label: 'Argument' + auto_create: + type: boolean + label: 'Create referenced entities if they don''t already exist' + auto_create_bundle: + type: string + label: 'Bundle assigned to the auto-created entities.' diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index aa791651fc..0d7412a199 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -4,10 +4,12 @@ use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Entity\EntityReferenceSelection\SelectionTrait; +use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; use Drupal\views\Views; +use Drupal\user\EntityOwnerInterface; /** * Plugin implementation of the 'selection' entity_reference. @@ -19,7 +21,7 @@ * weight = 0 * ) */ -class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPluginInterface { +class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPluginInterface, SelectionWithAutocreateInterface { use SelectionTrait; @@ -56,6 +58,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $entity_type = $this->entityManager->getDefinition($this->configuration['target_type']); $view_storage = $this->entityManager->getStorage('view'); + // Merge-in default values. + $selection_handler_settings = $this->getConfiguration()['handler_settings']; + $selection_handler_settings += array( + 'auto_create' => FALSE, + 'auto_create_bundle' => NULL, + ); + $options = []; foreach ($displays as $data) { list($view_id, $display_id) = $data; @@ -91,6 +100,35 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#required' => FALSE, '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.'), ]; + + $form['auto_create'] = array( + '#type' => 'checkbox', + '#title' => $this->t("Create referenced entities if they don't already exist"), + '#default_value' => $selection_handler_settings['auto_create'], + '#weight' => -2, + ); + if ($entity_type->hasKey('bundle')) { + $entity_type = $this->configuration['target_type']; + $bundles = $this->entityManager->getBundleInfo($entity_type); + $bundle_options = array(); + foreach ($bundles as $bundle_name => $bundle_info) { + $bundle_options[$bundle_name] = $bundle_info['label']; + } + natsort($bundle_options); + $form['auto_create_bundle'] = [ + '#type' => 'select', + '#title' => $this->t('Store new items in'), + '#options' => $bundle_options, + '#default_value' => $selection_handler_settings['auto_create_bundle'], + '#access' => count($bundles) > 1, + '#states' => [ + 'visible' => [ + ':input[name="settings[handler_settings][auto_create]"]' => ['checked' => TRUE], + ], + ], + '#weight' => -1, + ]; + } } else { if ($this->currentUser->hasPermission('administer views') && $this->moduleHandler->moduleExists('views_ui')) { @@ -193,6 +231,35 @@ public function validateReferenceableEntities(array $ids) { return $result; } + /** + * {@inheritdoc} + */ + public function createNewEntity($entity_type_id, $bundle, $label, $uid) { + $entity_type = $this->entityManager->getDefinition($entity_type_id); + $bundle_key = $entity_type->getKey('bundle'); + $label_key = $entity_type->getKey('label'); + $entity = $this->entityManager->getStorage($entity_type_id)->create(array( + $bundle_key => $bundle, + $label_key => $label, + )); + if ($entity instanceof EntityOwnerInterface) { + $entity->setOwnerId($uid); + } + return $entity; + } + + /** + * {@inheritdoc} + */ + public function validateReferenceableNewEntities(array $entities) { + return array_filter($entities, function ($entity) { + if (isset($this->configuration['handler_settings']['auto_create_bundle'])) { + return ($entity->bundle() === $this->configuration['handler_settings']['auto_create_bundle']); + } + return TRUE; + }); + } + /** * Element validate; Check View is valid. */