You are here

public function ResourceFieldEntityReference::value in RESTful 7.2

Gets the value for the field given a data source.

Parameters

DataInterpreterInterface $interpreter: The data source object. Interacts with the data storage.

Return value

mixed The value for the public field.

Throws

IncompatibleFieldDefinitionException

Overrides ResourceFieldEntity::value

File

src/Plugin/resource/Field/ResourceFieldEntityReference.php, line 179
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldEntityReference.

Class

ResourceFieldEntityReference
Class ResourceFieldEntityReference.

Namespace

Drupal\restful\Plugin\resource\Field

Code

public function value(DataInterpreterInterface $interpreter) {
  $value = $this->decorated
    ->value($interpreter);
  if (isset($value)) {

    // Let the decorated resolve callbacks.
    return $value;
  }

  // Check user has access to the property.
  if (!$this
    ->access('view', $interpreter)) {
    return NULL;
  }
  $resource = $this
    ->getResource();

  // If the field definition does not contain a resource, or it is set
  // explicitly to fullView FALSE, then return only the entity ID.
  if ($resource || !empty($resource) && $resource['fullView'] !== FALSE || $this
    ->getFormatter()) {

    // Let the resource embedding to the parent class.
    return parent::value($interpreter);
  }

  // Since this is a reference field (a field that points to other entities,
  // we can know for sure that the property wrappers are instances of
  // \EntityDrupalWrapper or lists of them.
  $property_wrapper = $this
    ->propertyWrapper($interpreter);
  if (!$property_wrapper
    ->value()) {

    // If there is no referenced entity, return.
    return NULL;
  }

  // If this is a multivalue field, then call recursively on the items.
  if ($property_wrapper instanceof \EntityListWrapper) {
    $values = array();
    foreach ($property_wrapper
      ->getIterator() as $item_wrapper) {
      $values[] = $this
        ->referencedId($item_wrapper);
    }
    return $values;
  }

  /* @var $property_wrapper \EntityDrupalWrapper */
  return $this
    ->referencedId($property_wrapper);
}