You are here

protected function RestfulEntityBase::getValueFromFieldFormatter in RESTful 7

Get value from a field rendered by Drupal field API's formatter.

Parameters

EntityMetadataWrapper $wrapper: The wrapped entity.

EntityMetadataWrapper $sub_wrapper: The wrapped property.

array $info: The public field info array.

Return value

mixed A single or multiple values.

1 call to RestfulEntityBase::getValueFromFieldFormatter()
RestfulEntityBase::viewEntity in plugins/restful/RestfulEntityBase.php
View an entity.

File

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

Class

RestfulEntityBase
An abstract implementation of RestfulEntityInterface.

Code

protected function getValueFromFieldFormatter(\EntityMetadataWrapper $wrapper, \EntityMetadataWrapper $sub_wrapper, array $info) {
  $property = $info['property'];
  if (!static::propertyIsField($property)) {

    // Property is not a field.
    throw new \RestfulServerConfigurationException(format_string('@property is not a configurable field, so it cannot be processed using field API formatter', array(
      '@property' => $property,
    )));
  }

  // Get values from the formatter.
  $output = field_view_field($this
    ->getEntityType(), $wrapper
    ->value(), $property, $info['formatter']);

  // Unset the theme, as we just want to get the value from the formatter,
  // without the wrapping HTML.
  unset($output['#theme']);
  if ($sub_wrapper instanceof EntityListWrapper) {

    // Multiple values.
    foreach (element_children($output) as $delta) {
      $value[] = drupal_render($output[$delta]);
    }
  }
  else {

    // Single value.
    $value = drupal_render($output);
  }
  return $value;
}