You are here

protected function Select2EntityReferenceWidget::getAutocreateBundle in Select 2 8

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

@uses \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::getAutocreateBundle(). This is copied from core.

Return value

string The bundle name.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to Select2EntityReferenceWidget::getAutocreateBundle()
Select2EntityReferenceWidget::formElement in src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php, line 253

Class

Select2EntityReferenceWidget
Plugin implementation of the 'select2' widget.

Namespace

Drupal\select2\Plugin\Field\FieldWidget

Code

protected function getAutocreateBundle() {
  $bundle = NULL;
  if ($this
    ->getSelectionHandlerSetting('auto_create')) {

    // If a bundle is explicitly defined, use it.
    if ($bundle = $this
      ->getSelectionHandlerSetting('auto_create_bundle')) {
      return $bundle;
    }
    $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);
    }
    else {

      // 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;
}