summaryrefslogtreecommitdiffstats
path: root/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
diff options
context:
space:
mode:
authorxjm2017-02-28 10:13:35 -0600
committerxjm2017-02-28 10:13:35 -0600
commit6cca8a6da0b3ec90523422ed6a30424d04303319 (patch)
tree4302050c1c00c029cba7eacfc9a76c982f1b004f /core/modules/rest/src/Plugin/rest/resource/EntityResource.php
parentb1242b2928e7830412fa8fde0d2e916bc09e0d1b (diff)
Issue #2751325 by damiankloip, Grayside, dawehner, Wim Leers, catch, tedbow, alexpott, himanshu-dixit, Jo Fitzgerald, xjm, andrewbelcher, skyredwang, effulgentsia, hampercm, eelkeblok: All serialized values are strings, should be integers/booleans when appropriate
Diffstat (limited to 'core/modules/rest/src/Plugin/rest/resource/EntityResource.php')
-rw-r--r--core/modules/rest/src/Plugin/rest/resource/EntityResource.php39
1 files changed, 38 insertions, 1 deletions
diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
index 3e35f83..a66d5ab 100644
--- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
+++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
@@ -11,6 +11,8 @@ use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\TypedData\PrimitiveInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LoggerInterface;
@@ -196,6 +198,41 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
}
/**
+ * Gets the values from the field item list casted to the correct type.
+ *
+ * Values are casted to the correct type so we can determine whether or not
+ * something has changed. REST formats such as JSON support typed data but
+ * Drupal's database API will return values as strings. Currently, only
+ * primitive data types know how to cast their values to the correct type.
+ *
+ * @param \Drupal\Core\Field\FieldItemListInterface $field_item_list
+ * The field item list to retrieve its data from.
+ *
+ * @return mixed[][]
+ * The values from the field item list casted to the correct type. The array
+ * of values returned is a multidimensional array keyed by delta and the
+ * property name.
+ */
+ protected function getCastedValueFromFieldItemList(FieldItemListInterface $field_item_list) {
+ $value = $field_item_list->getValue();
+
+ foreach ($value as $delta => $field_item_value) {
+ /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
+ $field_item = $field_item_list->get($delta);
+ $properties = $field_item->getProperties(TRUE);
+ // Foreach field value we check whether we know the underlying property.
+ // If we exists we try to cast the value.
+ foreach ($field_item_value as $property_name => $property_value) {
+ if (isset($properties[$property_name]) && ($property = $field_item->get($property_name)) && $property instanceof PrimitiveInterface) {
+ $value[$delta][$property_name] = $property->getCastedValue();
+ }
+ }
+ }
+
+ return $value;
+ }
+
+ /**
* Responds to entity PATCH requests.
*
* @param \Drupal\Core\Entity\EntityInterface $original_entity
@@ -239,7 +276,7 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
}
// Unchanged values for entity keys don't need access checking.
- if ($original_entity->get($field_name)->getValue() === $entity->get($field_name)->getValue()) {
+ if ($this->getCastedValueFromFieldItemList($original_entity->get($field_name)) === $this->getCastedValueFromFieldItemList($entity->get($field_name))) {
continue;
}
// It is not possible to set the language to NULL as it is automatically