You are here

private function AttributesAwareFieldableEdgeEntityBase::setAttributeValueFromField in Apigee Edge 8

Sets attribute value from a field.

Parameters

string $field_name: Name of a field, which must not be a base field.

1 call to AttributesAwareFieldableEdgeEntityBase::setAttributeValueFromField()
AttributesAwareFieldableEdgeEntityBase::setPropertyValue in src/Entity/AttributesAwareFieldableEdgeEntityBase.php
Updates the property value on an entity by field name.

File

src/Entity/AttributesAwareFieldableEdgeEntityBase.php, line 126

Class

AttributesAwareFieldableEdgeEntityBase
For fieldable Edge entities that can use attributes as field storage.

Namespace

Drupal\apigee_edge\Entity

Code

private function setAttributeValueFromField(string $field_name) {

  // We need to unaltered field data value here not the field value returned
  // by $this->get($field_name)->value (magic getter).
  $field_value = $this
    ->get($field_name)
    ->getValue();

  // Property not found so let's save it as an attribute value.
  $attribute_value = $this
    ->fieldAttributeConverter()
    ->getAttributeValueFromField($this->entityTypeId, $field_name, $field_value);
  if ($attribute_value !== NULL) {
    $attribute_name = $this
      ->fieldAttributeConverter()
      ->getAttributeName($field_name);

    // Do not leave empty attributes. If generated attribute value is an
    // empty string let's remove it from the entity.
    // (Apigee Edge MGMT UI does not allow to save an entity with empty
    // attribute value, the API does.)
    if ($attribute_value === '') {
      $this->decorated
        ->deleteAttribute($attribute_name);
    }
    else {
      $this->decorated
        ->setAttribute($attribute_name, $attribute_value);
    }
  }
}