ResourceFieldFileEntityReference.php in RESTful 7.2
File
src/Plugin/resource/Field/ResourceFieldFileEntityReference.php
View source
<?php
namespace Drupal\restful\Plugin\resource\Field;
use Drupal\restful\Http\Request;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\restful\Plugin\ResourcePluginManager;
class ResourceFieldFileEntityReference extends ResourceFieldEntityReference implements ResourceFieldEntityReferenceInterface {
protected function propertyIdentifier(\EntityMetadataWrapper $property_wrapper) {
$file_array = $property_wrapper
->value();
$identifier = $file_array['fid'];
$resource = $this
->getResource();
if (!$resource || !$identifier || isset($resource['fullView']) && $resource['fullView'] === FALSE) {
return $identifier;
}
$instance_id = sprintf('%s:%d.%d', $resource['name'], $resource['majorVersion'], $resource['minorVersion']);
$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;
}
}
protected function buildResourceMetadataItem($wrapper) {
$file_array = $wrapper
->value();
$wrapper = entity_metadata_wrapper('file', $file_array['fid']);
return parent::buildResourceMetadataItem($wrapper);
}
protected function referencedId($property_wrapper) {
$file_array = $property_wrapper
->value();
if (!$this->referencedIdProperty) {
return $file_array['fid'];
}
$wrapper = entity_metadata_wrapper('file', $file_array['fid']);
return $wrapper->{$this->referencedIdProperty}
->value();
}
public function getRequest() {
return $this->decorated
->getRequest();
}
public function setRequest(RequestInterface $request) {
$this->decorated
->setRequest($request);
}
}