function ContentEntityStorageBase::getBundleFromValues

Same name in other branches
  1. 10 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::getBundleFromValues()
  2. 11.x core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::getBundleFromValues()

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.

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 162

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.