You are here

protected function RestfulEntityBase::getValueFromResource in RESTful 7

Get value from an entity reference field with "resource" property.

Parameters

EntityMetadataWrapper $wrapper: The wrapped object.

string $property: The property name (i.e. the field name).

array $resource: Array with resource names, keyed by the bundle name.

string $public_field_name: Field name in the output. This is used to store additional metadata useful for the formatter.

int $host_id: Host entity ID. Used to structure the value metadata.

Return value

mixed The value if found, or NULL if bundle not defined.

1 call to RestfulEntityBase::getValueFromResource()
RestfulEntityBase::getValueFromProperty in plugins/restful/RestfulEntityBase.php
Get value from a property.

File

plugins/restful/RestfulEntityBase.php, line 520
Contains RestfulEntityBase.

Class

RestfulEntityBase
An abstract implementation of RestfulEntityInterface.

Code

protected function getValueFromResource(EntityMetadataWrapper $wrapper, $property, $resource, $public_field_name = NULL, $host_id = NULL) {
  $handlers = $this->staticCache
    ->get(__CLASS__ . '::' . __FUNCTION__, array());
  if (!($entity = $wrapper
    ->value())) {
    return;
  }
  $target_type = $this
    ->getTargetTypeFromEntityReference($wrapper, $property);
  list($id, , $bundle) = entity_extract_ids($target_type, $entity);
  if (empty($resource[$bundle])) {

    // Bundle not mapped to a resource.
    return;
  }
  if (!$resource[$bundle]['full_view']) {

    // Show only the ID(s) of the referenced resource.
    return $wrapper
      ->value(array(
      'identifier' => TRUE,
    ));
  }
  if ($public_field_name) {
    $this->valueMetadata[$host_id][$public_field_name][] = array(
      'id' => $id,
      'entity_type' => $target_type,
      'bundle' => $bundle,
      'resource_name' => $resource[$bundle]['name'],
    );
  }
  if (empty($handlers[$bundle])) {
    $handlers[$bundle] = restful_get_restful_handler($resource[$bundle]['name'], $resource[$bundle]['major_version'], $resource[$bundle]['minor_version']);
  }
  $bundle_handler = $handlers[$bundle];

  // Pipe the parent request and account to the sub-request.
  $piped_request = $this
    ->getRequestForSubRequest();
  $bundle_handler
    ->setAccount($this
    ->getAccount());
  $bundle_handler
    ->setRequest($piped_request);
  return $bundle_handler
    ->viewEntity($id);
}