ResourceFieldEntityReference.php in RESTful 7.2
File
src/Plugin/resource/Field/ResourceFieldEntityReference.php
View source
<?php
namespace Drupal\restful\Plugin\resource\Field;
use Drupal\restful\Exception\BadRequestException;
use Drupal\restful\Exception\InternalServerErrorException;
use Drupal\restful\Http\HttpHeaderBag;
use Drupal\restful\Http\Request;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\DataInterpreter\DataInterpreterInterface;
use Drupal\restful\Plugin\resource\DataProvider\DataProviderResource;
class ResourceFieldEntityReference extends ResourceFieldEntity implements ResourceFieldEntityReferenceInterface {
protected $referencedIdProperty;
public function __construct(array $field, RequestInterface $request) {
parent::__construct($field, $request);
if (!empty($field['referencedIdProperty'])) {
$this->referencedIdProperty = $field['referencedIdProperty'];
}
}
public function preprocess($value) {
if (!$value) {
return NULL;
}
$cardinality = $this
->getCardinality();
if ($cardinality != 1 && !is_array($value)) {
$value = explode(',', $value);
}
if ($cardinality != 1 && ResourceFieldBase::isArrayNumeric($value)) {
$this
->setCardinality(1);
$values = array();
foreach ($value as $item) {
$values[] = $this
->preprocess($item);
}
$this
->setCardinality($cardinality);
return $values;
}
if (!is_array($value) || empty($value['body'])) {
return !empty($value['id']) && array_keys($value) == array(
'id',
) ? $value['id'] : $value;
}
$merged_value = $this
->mergeEntityFromReference($value);
return $merged_value
->getInterpreter()
->getWrapper()
->getIdentifier();
}
protected function mergeEntityFromReference($value) {
$resource = $this
->getResource();
if (empty($resource) || empty($value['body'])) {
return $value;
}
$resource_data_provider = DataProviderResource::init(static::subRequest($value), $resource['name'], array(
$resource['majorVersion'],
$resource['minorVersion'],
));
$merged = $resource_data_provider
->merge(static::subRequestId($value), $value['body']);
return reset($merged);
}
public static function subRequest(array $value) {
if (empty($value['request'])) {
throw new BadRequestException('Malformed body payload. Missing "request" key for the sub-request.');
}
if (empty($value['request']['method'])) {
throw new BadRequestException('Malformed body payload. Missing "method" int the "request" key for the sub-request.');
}
$request_user_info = $value['request'] + array(
'path' => NULL,
'query' => array(),
'csrf_token' => NULL,
);
$headers = empty($request_user_info['headers']) ? array() : $request_user_info['headers'];
$request_user_info['headers'] = new HttpHeaderBag($headers);
$request_user_info['via_router'] = FALSE;
$request_user_info['cookies'] = $_COOKIE;
$request_user_info['files'] = $_FILES;
$request_user_info['server'] = $_SERVER;
return Request::create($request_user_info['path'], $request_user_info['query'], $request_user_info['method'], $request_user_info['headers'], $request_user_info['via_router'], $request_user_info['csrf_token'], $request_user_info['cookies'], $request_user_info['files'], $request_user_info['server']);
}
protected static function subRequestId($value) {
if ($value['request']['method'] == RequestInterface::METHOD_POST) {
return NULL;
}
return empty($value['id']) ? NULL : $value['id'];
}
public function value(DataInterpreterInterface $interpreter) {
$value = $this->decorated
->value($interpreter);
if (isset($value)) {
return $value;
}
if (!$this
->access('view', $interpreter)) {
return NULL;
}
$resource = $this
->getResource();
if ($resource || !empty($resource) && $resource['fullView'] !== FALSE || $this
->getFormatter()) {
return parent::value($interpreter);
}
$property_wrapper = $this
->propertyWrapper($interpreter);
if (!$property_wrapper
->value()) {
return NULL;
}
if ($property_wrapper instanceof \EntityListWrapper) {
$values = array();
foreach ($property_wrapper
->getIterator() as $item_wrapper) {
$values[] = $this
->referencedId($item_wrapper);
}
return $values;
}
return $this
->referencedId($property_wrapper);
}
protected function referencedId($property_wrapper) {
$identifier = $property_wrapper
->getIdentifier();
if (!$this->referencedIdProperty) {
return $identifier;
}
try {
return $identifier ? $property_wrapper->{$this->referencedIdProperty}
->value() : NULL;
} catch (\EntityMetadataWrapperException $e) {
return NULL;
}
}
public function getRequest() {
return $this->decorated
->getRequest();
}
public function setRequest(RequestInterface $request) {
$this->decorated
->setRequest($request);
}
public function getDefinition() {
return $this->decorated
->getDefinition();
}
}