You are here

protected function ResourceFieldEntity::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.

1 call to ResourceFieldEntity::propertyIdentifier()
ResourceFieldEntity::singleValue in src/Plugin/resource/Field/ResourceFieldEntity.php
Returns the value for the current single field.
1 method overrides ResourceFieldEntity::propertyIdentifier()
ResourceFieldFileEntityReference::propertyIdentifier in src/Plugin/resource/Field/ResourceFieldFileEntityReference.php
Helper function to get the identifier from a property wrapper.

File

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

Class

ResourceFieldEntity
Class ResourceFieldEntity.

Namespace

Drupal\restful\Plugin\resource\Field

Code

protected function propertyIdentifier(\EntityMetadataWrapper $property_wrapper) {
  if ($property_wrapper instanceof \EntityDrupalWrapper) {

    // The property wrapper is a reference to another entity get the entity
    // ID.
    $identifier = $this
      ->referencedId($property_wrapper);
    $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 {
      return $property_wrapper->{$plugin_definition['dataProvider']['idField']}
        ->value();
    } catch (\EntityMetadataWrapperException $e) {
      return $identifier;
    }
  }

  // The property is a regular one, get the value out of it and use it as
  // the embedded identifier.
  return $this
    ->fieldValue($property_wrapper);
}