You are here

protected function FieldableEdgeEntityBase::convertFieldValueToPropertyValue in Apigee Edge 8

Converts a field value to a property value.

Parameters

string $field_name: Name of a field.

Return value

mixed Value of a property.

1 call to FieldableEdgeEntityBase::convertFieldValueToPropertyValue()
FieldableEdgeEntityBase::onChange in src/Entity/FieldableEdgeEntityBase.php
Reacts to changes to a field.

File

src/Entity/FieldableEdgeEntityBase.php, line 406

Class

FieldableEdgeEntityBase
Base field support for Apigee Entities without making them content entities.

Namespace

Drupal\apigee_edge\Entity

Code

protected function convertFieldValueToPropertyValue(string $field_name) {

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $definition */
  $definition = $this
    ->getFieldDefinition($field_name);
  if ($definition
    ->getFieldStorageDefinition()
    ->getCardinality() === 1) {
    $value = $this
      ->get($field_name)->value;
  }
  else {

    // Extract values from multi-value fields the right way. Magic getter
    // would just return the first item from the list.
    // @see \Drupal\Core\Field\FieldItemList::__get()
    $value = [];
    foreach ($this
      ->get($field_name) as $index => $item) {
      $value[$index] = $item->value;
    }
  }

  // Take care of timestamp fields that value in the SDK is a
  // date object.
  if (static::propertyFieldType($field_name) === 'timestamp') {

    /** @var \DateTimeImmutable $value */
    $value = \DateTimeImmutable::createFromFormat('U', $value);
  }
  return $value;
}