You are here

protected function ResourceFieldFileEntityReference::propertyIdentifier in RESTful 7.2

Helper function to get the identifier from a property wrapper.

Parameters

\EntityMetadataWrapper $property_wrapper: The property wrapper to get the ID from.

Return value

string An identifier.

Overrides ResourceFieldEntity::propertyIdentifier

File

src/Plugin/resource/Field/ResourceFieldFileEntityReference.php, line 27
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldFileEntityReference.

Class

ResourceFieldFileEntityReference

Namespace

Drupal\restful\Plugin\resource\Field

Code

protected function propertyIdentifier(\EntityMetadataWrapper $property_wrapper) {

  // The property wrapper is a reference to another entity get the entity
  // ID.
  $file_array = $property_wrapper
    ->value();
  $identifier = $file_array['fid'];
  $resource = $this
    ->getResource();

  // TODO: Make sure we still want to support fullView.
  if (!$resource || !$identifier || isset($resource['fullView']) && $resource['fullView'] === FALSE) {
    return $identifier;
  }

  // If there is a resource that we are pointing to, we need to use the id
  // field that that particular resource has in its configuration. Trying to
  // load by the entity id in that scenario will lead to a 404.
  // We'll load the plugin to get the idField configuration.
  $instance_id = sprintf('%s:%d.%d', $resource['name'], $resource['majorVersion'], $resource['minorVersion']);

  /* @var ResourceInterface $resource */
  $resource = restful()
    ->getResourceManager()
    ->getPluginCopy($instance_id, Request::create('', array(), RequestInterface::METHOD_GET));
  $plugin_definition = $resource
    ->getPluginDefinition();
  if (empty($plugin_definition['dataProvider']['idField'])) {
    return $identifier;
  }
  try {
    $file_wrapper = entity_metadata_wrapper('file', $file_array['fid']);
    return $file_wrapper->{$plugin_definition['dataProvider']['idField']}
      ->value();
  } catch (\EntityMetadataWrapperException $e) {
    return $identifier;
  }
}