You are here

protected function ContentEntityStorageBase::getBundleFromValues in Drupal 10

Retrieves the bundle from an array of values.

Parameters

array $values: An array of values to set, keyed by field name.

Return value

string|null The bundle or NULL if not set.

Throws

\Drupal\Core\Entity\EntityStorageException When a corresponding bundle cannot be found and is expected.

2 calls to ContentEntityStorageBase::getBundleFromValues()
ContentEntityStorageBase::create in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Constructs a new entity object, without permanently saving it.
ContentEntityStorageBase::doCreate in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Performs storage-specific creation of entities.

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 165

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

protected function getBundleFromValues(array $values) : ?string {
  $bundle = NULL;

  // Make sure we have a reasonable bundle key. If not, bail early.
  if (!$this->bundleKey || !isset($values[$this->bundleKey])) {
    return NULL;
  }

  // Normalize the bundle value. This is an optimized version of
  // \Drupal\Core\Field\FieldInputValueNormalizerTrait::normalizeValue()
  // because we just need the scalar value.
  $bundle_value = $values[$this->bundleKey];
  if (!is_array($bundle_value)) {

    // The bundle value is a scalar, use it as-is.
    $bundle = $bundle_value;
  }
  elseif (is_numeric(array_keys($bundle_value)[0])) {

    // The bundle value is a field item list array, keyed by delta.
    $bundle = reset($bundle_value[0]);
  }
  else {

    // The bundle value is a field item array, keyed by the field's main
    // property name.
    $bundle = reset($bundle_value);
  }
  return $bundle;
}