You are here

protected function EntityReferenceAutocompleteWidget::getAutocreateBundle in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::getAutocreateBundle()

Returns the name of the bundle which will be used for autocreated entities.

Return value

string The bundle name. If autocreate is not active, NULL will be returned.

1 call to EntityReferenceAutocompleteWidget::getAutocreateBundle()
EntityReferenceAutocompleteWidget::formElement in core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php
Returns the form for a single field widget.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php, line 159

Class

EntityReferenceAutocompleteWidget
Plugin implementation of the 'entity_reference_autocomplete' widget.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

protected function getAutocreateBundle() {
  $bundle = NULL;
  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');
    }
    elseif (count($target_bundles) == 1) {
      $bundle = reset($target_bundles);
    }
    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.
      trigger_error(sprintf("The 'Create referenced entities if they don't already exist' option is enabled but a specific destination bundle is not set. You should re-visit and fix the settings of the '%s' (%s) field.", $this->fieldDefinition
        ->getLabel(), $this->fieldDefinition
        ->getName()), E_USER_WARNING);
    }
  }
  return $bundle;
}