You are here

protected function DynamicEntityReferenceWidget::getAutocreateBundle in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget\DynamicEntityReferenceWidget::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.

Overrides EntityReferenceAutocompleteWidget::getAutocreateBundle

2 calls to DynamicEntityReferenceWidget::getAutocreateBundle()
DynamicEntityReferenceWidget::elementValidate in src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php
DynamicEntityReferenceWidget::formElement in src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php, line 221

Class

DynamicEntityReferenceWidget
Plugin implementation of the 'dynamic_entity_reference autocomplete' widget.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget

Code

protected function getAutocreateBundle($target_type = NULL) {
  if ($target_type === NULL) {
    return parent::getAutocreateBundle();
  }
  $bundle = NULL;
  if ($this
    ->getSelectionHandlerSetting('auto_create', $target_type)) {

    // If the 'target_bundles' setting is restricted to a single choice, we
    // can use that.
    if (($target_bundles = $this
      ->getSelectionHandlerSetting('target_bundles', $target_type)) && count($target_bundles) == 1) {
      $bundle = reset($target_bundles);
    }
    else {

      // @todo Expose a proper UI for choosing the bundle for autocreated
      // entities in https://www.drupal.org/node/2412569.
      $bundles = \Drupal::service('entity_type.bundle.info')
        ->getBundleInfo($target_type);
      $bundle = key($bundles);
    }
  }
  return $bundle;
}