/** * Get the (reference) field information for a single item. * * @param ResourceFieldInterface $resource_field * The resource field. * * @throws \Drupal\restful\Exception\BadRequestException * * @return array * An array containing the following keys: * - 'name': Drupal's internal field name. Ex: field_article_related * - 'type': Either a field or a property. * - 'entity_type': The entity type this field points to. Not populated if * the field is not a reference (for instance the destination field used * in the where clause). * - 'bundles': The allowed bundles for this field. Not populated if the * field is not a reference (for instance the destination field used in * the where clause). */ protected function getFieldsFromPublicNameItem(ResourceFieldInterface $resource_field) { $property = $resource_field->getProperty(); $resource = $resource_field->getResource(); $item = array('name' => $property, 'type' => ResourceFieldEntity::propertyIsField($property) ? RelationalFilterInterface::TYPE_FIELD : RelationalFilterInterface::TYPE_PROPERTY, 'entity_type' => NULL, 'bundles' => array()); $item['column'] = $item['type'] == RelationalFilterInterface::TYPE_FIELD ? $resource_field->getColumn() : NULL; $instance_id = sprintf('%s:%d.%d', $resource['name'], $resource['majorVersion'], $resource['minorVersion']); /* @var ResourceEntity $resource */ $resource = restful()->getResourceManager()->getPluginCopy($instance_id, Request::create('', array(), RequestInterface::METHOD_GET)); // Variables for the next iteration. $definitions = $resource->getFieldDefinitions(); $item['entity_type'] = $resource->getEntityType(); $item['bundles'] = $resource->getBundles(); return array($item, $definitions); }
/** * Get value from a field rendered by Drupal field API's formatter. * * @param \EntityMetadataWrapper $property_wrapper * The property wrapper. Either \EntityDrupalWrapper or \EntityListWrapper. * @param \EntityDrupalWrapper $wrapper * The entity wrapper. * * @return mixed * A single or multiple values. * * @throws \Drupal\restful\Exception\ServerConfigurationException */ protected function formatterValue(\EntityMetadataWrapper $property_wrapper, \EntityDrupalWrapper $wrapper) { $value = NULL; if (!ResourceFieldEntity::propertyIsField($this->getProperty())) { // Property is not a field. throw new ServerConfigurationException(format_string('@property is not a configurable field, so it cannot be processed using field API formatter', array('@property' => $this->getProperty()))); } // Get values from the formatter. $output = field_view_field($this->getEntityType(), $wrapper->value(), $this->getProperty(), $this->getFormatter()); // Unset the theme, as we just want to get the value from the formatter, // without the wrapping HTML. unset($output['#theme']); if ($property_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; }