function EntityFieldValueTrait::getFieldValue

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/EntityFieldValueTrait.php \Drupal\Core\Entity\EntityFieldValueTrait::getFieldValue()

Gets the value of a field property directly, bypassing the typed data API.

For certain use cases, it can be desirable to avoid the overhead of creating FieldItemList and ItemList objects in order to access certain properties of entities. This is particularly true where the access would be the only interaction with the entity system for an entire response, or where a very large number of entities are being dealt with at once. This method can be used in those cases, but it is marked protected and @internal to discourage use, since it is not robust for dealing with the full lifecycle of entity creation or updates, or for computed fields and properties.

@internal

Parameters

string $field_name: The field name.

string $property: The field property, usually 'value' for single property field types.

int $delta: The field delta.

Return value

mixed The value of the field property, or NULL.

File

core/lib/Drupal/Core/Entity/EntityFieldValueTrait.php, line 41

Class

EntityFieldValueTrait
Adds a ::getFieldValue() method suitable for use with content entities.

Namespace

Drupal\Core\Entity

Code

protected function getFieldValue(string $field_name, string $property, int $delta = 0) : mixed {
  // Attempt to get the value from the values directly if the field is not
  // initialized yet.
  if (!isset($this->fields[$field_name]) && isset($this->values[$field_name])) {
    $langcode = match (TRUE) {  \array_key_exists($this->activeLangcode, $this->values[$field_name]) => $this->activeLangcode,
      \array_key_exists(LanguageInterface::LANGCODE_DEFAULT, $this->values[$field_name]) => LanguageInterface::LANGCODE_DEFAULT,
      default => NULL,
    
    };
    if ($langcode !== NULL) {
      // If there are field values, try to get the property value.
      return match (TRUE) {  isset($this->values[$field_name][$langcode][$delta][$property]) => $this->values[$field_name][$langcode][$delta][$property],
        isset($this->values[$field_name][$langcode][$property]) => $this->values[$field_name][$langcode][$property],
        !is_array($this->values[$field_name][$langcode]) => $this->values[$field_name][$langcode],
        default => NULL,
      
      };
    }
  }
  // Fall back to access the property through the field object.
  $field_value = $this->get($field_name)
    ->get($delta);
  if ($field_value !== NULL) {
    return $field_value->{$property};
  }
  // $delta does not exist in value list.
  return NULL;
}

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