You are here

protected function ResourceFieldEntity::singleValue in RESTful 7.2

Returns the value for the current single field.

This implementation will also add some metadata to the resource field object about the entity it is referencing.

Parameters

\EntityMetadataWrapper $property_wrapper: The property wrapper. Either \EntityDrupalWrapper or \EntityListWrapper.

\EntityDrupalWrapper $wrapper: The entity wrapper.

object $account: The user account.

Return value

mixed A single value for the field.

Throws

\Drupal\restful\Exception\BadRequestException

\Drupal\restful\Exception\ServerConfigurationException

1 call to ResourceFieldEntity::singleValue()
ResourceFieldEntity::value in src/Plugin/resource/Field/ResourceFieldEntity.php
Gets the value for the field given a data source.

File

src/Plugin/resource/Field/ResourceFieldEntity.php, line 305
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldEntity

Class

ResourceFieldEntity
Class ResourceFieldEntity.

Namespace

Drupal\restful\Plugin\resource\Field

Code

protected function singleValue(\EntityMetadataWrapper $property_wrapper, \EntityDrupalWrapper $wrapper, $account) {
  if ($resource = $this
    ->getResource()) {

    // TODO: The resource input data in the field definition has changed.
    // Now it does not need to be keyed by bundle since you don't even need
    // an entity to use the resource based field.
    $embedded_identifier = $this
      ->propertyIdentifier($property_wrapper);

    // Allow embedding entities with ID 0, like the anon user.
    if (empty($embedded_identifier) && $embedded_identifier !== 0) {
      return NULL;
    }
    if (isset($resource['fullView']) && $resource['fullView'] === FALSE) {
      return $embedded_identifier;
    }

    // We support dot notation for the sparse fieldsets. That means that
    // clients can specify the fields to show based on the "fields" query
    // string parameter.
    $parsed_input = array(
      'fields' => implode(',', $this
        ->nestedDottedChildren('fields')),
      'include' => implode(',', $this
        ->nestedDottedChildren('include')),
      'filter' => $this
        ->nestedDottedChildren('filter'),
    );
    $request = Request::create('', array_filter($parsed_input), RequestInterface::METHOD_GET);

    // Get a plugin (that can be altered with decorators.
    $embedded_resource = restful()
      ->getResourceManager()
      ->getPluginCopy(sprintf('%s:%d.%d', $resource['name'], $resource['majorVersion'], $resource['minorVersion']));

    // Configure the plugin copy with the sub-request and sub-path.
    $embedded_resource
      ->setPath($embedded_identifier);
    $embedded_resource
      ->setRequest($request);
    $embedded_resource
      ->setAccount($account);
    $metadata = $this
      ->getMetadata($wrapper
      ->getIdentifier());
    $metadata = $metadata ?: array();
    $metadata[] = $this
      ->buildResourceMetadataItem($property_wrapper);
    $this
      ->addMetadata($wrapper
      ->getIdentifier(), $metadata);
    try {

      // Get the contents to embed in place of the reference ID.

      /* @var ResourceFieldCollection $embedded_entity */
      $embedded_entity = $embedded_resource
        ->getDataProvider()
        ->view($embedded_identifier);
    } catch (InaccessibleRecordException $e) {

      // If you don't have access to the embedded entity is like not having
      // access to the property.
      return NULL;
    } catch (UnprocessableEntityException $e) {

      // If you access a nonexistent embedded entity.
      return NULL;
    }

    // Test if the $embedded_entity meets the filter or not.
    if (empty($parsed_input['filter'])) {
      return $embedded_entity;
    }
    foreach ($parsed_input['filter'] as $filter) {

      // Filters only apply if the target is the current field.
      if (!empty($filter['target']) && $filter['target'] == $this
        ->getPublicName() && !$embedded_entity
        ->evalFilter($filter)) {

        // This filter is not met.
        return NULL;
      }
    }
    return $embedded_entity;
  }
  if ($this
    ->getFormatter()) {

    // Get value from field formatter.
    $value = $this
      ->formatterValue($property_wrapper, $wrapper);
  }
  else {

    // Single value.
    $value = $this
      ->fieldValue($property_wrapper);
  }
  return $value;
}