Unverified Commit 0f469b60 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2821352 by chr.fritsch, jian he, Pancho, RaphaelBriskie, jonathanshaw,...

Issue #2821352 by chr.fritsch, jian he, Pancho, RaphaelBriskie, jonathanshaw, alexpott, amateescu, larowlan: EntityReferenceAutocompleteWidget::getAutocreateBundle() unnecessarily requires the 'target_bundles' setting
parent ca497516
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -395,13 +395,16 @@ public function validateReferenceableEntities(array $ids) {
   */
  public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $bundle_key = $entity_type->getKey('bundle');
    $label_key = $entity_type->getKey('label');

    $entity = $this->entityTypeManager->getStorage($entity_type_id)->create([
      $bundle_key => $bundle,
      $label_key => $label,
    ]);
    $values = [
      $entity_type->getKey('label') => $label,
    ];

    if ($bundle_key = $entity_type->getKey('bundle')) {
      $values[$bundle_key] = $bundle;
    }

    $entity = $this->entityTypeManager->getStorage($entity_type_id)->create($values);

    if ($entity instanceof EntityOwnerInterface) {
      $entity->setOwnerId($uid);
+12 −5
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
      '#placeholder' => $this->getSetting('placeholder'),
    ];

    if ($this->getSelectionHandlerSetting('auto_create') && ($bundle = $this->getAutocreateBundle())) {
    if ($bundle = $this->getAutocreateBundle()) {
      $element['#autocreate'] = [
        'bundle' => $bundle,
        'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id(),
@@ -154,16 +154,23 @@ public function massageFormValues(array $values, array $form, FormStateInterface
   * Returns the name of the bundle which will be used for autocreated entities.
   *
   * @return string
   *   The bundle name.
   *   The bundle name. If autocreate is not active, NULL will be returned.
   */
  protected function getAutocreateBundle() {
    $bundle = NULL;
    if ($this->getSelectionHandlerSetting('auto_create') && $target_bundles = $this->getSelectionHandlerSetting('target_bundles')) {
    if ($this->getSelectionHandlerSetting('auto_create')) {
      $target_bundles = $this->getSelectionHandlerSetting('target_bundles');
      // If there's no target bundle at all, use the target_type. It's the
      // default for bundleless entity types.
      if (empty($target_bundles)) {
        $bundle = $this->getFieldSetting('target_type');
      }
      // If there's only one target bundle, use it.
      if (count($target_bundles) == 1) {
      elseif (count($target_bundles) == 1) {
        $bundle = reset($target_bundles);
      }
      // Otherwise use the target bundle stored in selection handler settings.
      // If there's more than one target bundle, 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.
+48 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ class EntityReferenceAutoCreateTest extends BrowserTestBase {

  use EntityReferenceTestTrait;

  public static $modules = ['node', 'taxonomy'];
  public static $modules = ['node', 'taxonomy', 'entity_test'];

  /**
   * {@inheritdoc}
@@ -237,4 +237,51 @@ public function testMultipleTargetBundles() {
    // $this->assertErrorLogged($error_message);
  }

  /**
   * Tests autocreation for an entity that has no bundles.
   */
  public function testNoBundles() {
    $account = $this->drupalCreateUser([
      'access content',
      "create $this->referencingType content",
      'administer entity_test content',
    ]);
    $this->drupalLogin($account);

    $field_name = mb_strtolower($this->randomMachineName());
    $handler_settings = [
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', $this->referencingType, $field_name, $this->randomString(), 'entity_test_no_bundle_with_label', 'default', $handler_settings);
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', $this->referencingType)
      ->setComponent($field_name, ['type' => 'entity_reference_autocomplete'])
      ->save();

    $node_title = $this->randomMachineName();
    $name = $this->randomMachineName();
    $edit = [
      $field_name . '[0][target_id]' => $name,
      'title[0][value]' => $node_title,
    ];

    $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');

    // Assert referenced entity was created.
    $result = \Drupal::entityQuery('entity_test_no_bundle_with_label')
      ->condition('name', $name)
      ->execute();
    $this->assertNotEmpty($result, 'Referenced entity was created.');
    $referenced_id = key($result);

    // Assert the referenced entity is associated with referencing node.
    $result = \Drupal::entityQuery('node')
      ->condition('type', $this->referencingType)
      ->execute();
    $this->assertCount(1, $result);
    $referencing_nid = key($result);
    $referencing_node = Node::load($referencing_nid);
    $this->assertEqual($referenced_id, $referencing_node->$field_name->target_id, 'Newly created node is referenced from the referencing entity.');
  }

}
+28 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\entity_test\Entity;

/**
 * Test entity class with no bundle but with label.
 *
 * @ContentEntityType(
 *   id = "entity_test_no_bundle_with_label",
 *   label = @Translation("Entity Test without bundle but with label"),
 *   base_table = "entity_test_no_bundle_with_label",
 *   handlers = {
 *     "views_data" = "Drupal\views\EntityViewsData"
 *   },
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "revision" = "revision_id",
 *   },
 *   admin_permission = "administer entity_test content",
 *   links = {
 *     "add-form" = "/entity_test_no_bundle_with_label/add",
 *   },
 * )
 */
class EntityTestNoBundleWithLabel extends EntityTest {

}