You are here

public function FieldableEdgeEntityBase::set in Apigee Edge 8

Sets a field value.

Parameters

string $field_name: The name of the field to set; e.g., 'title' or 'name'.

mixed $value: The value to set, or NULL to unset the field.

bool $notify: (optional) Whether to notify the entity of the change. Defaults to TRUE. If the update stems from the entity, set it to FALSE to avoid being notified again.

Return value

$this

Throws

\InvalidArgumentException If the specified field does not exist.

Overrides FieldableEntityInterface::set

1 call to FieldableEdgeEntityBase::set()
App::set in src/Entity/App.php
Sets a field value.
1 method overrides FieldableEdgeEntityBase::set()
App::set in src/Entity/App.php
Sets a field value.

File

src/Entity/FieldableEdgeEntityBase.php, line 435

Class

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

Namespace

Drupal\apigee_edge\Entity

Code

public function set($field_name, $value, $notify = TRUE) {

  // Do not try to set value of a field that does not exist.
  if (!$this
    ->hasField($field_name)) {

    // According to spec an exception should be thrown in this case.
    throw new InvalidArgumentException(sprintf('"%s" field does not exist on "s" entity.', $field_name, get_class($this)));
  }

  // Value that is compatible with what a mapped base field can accept.
  $field_value = $value;
  if (is_object($value)) {

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

      /** @var \DateTimeImmutable $value */
      $field_value = $value
        ->getTimestamp();
    }
    else {
      $field_value = (string) $value;
    }
  }

  // Save field's value as a field. This calls onChange() that saves
  // field value to the related property.
  $this
    ->get($field_name)
    ->setValue($field_value, $notify);
  return $this;
}