Commit fb3c8d4e authored by catch's avatar catch

Issue #2143089 by imadalin, yched, dawehner, harsha012, Wim Leers, c.nish2k3,...

Issue #2143089 by imadalin, yched, dawehner, harsha012, Wim Leers, c.nish2k3, bradjones1, damiankloip, xjm: Clean up normalizer parameter and variable names to use 'field_items' consistently etc
parent 78ae2401
...@@ -74,15 +74,15 @@ public function normalize($entity, $format = NULL, array $context = []) { ...@@ -74,15 +74,15 @@ public function normalize($entity, $format = NULL, array $context = []) {
// If the fields to use were specified, only output those field values. // If the fields to use were specified, only output those field values.
if (isset($context['included_fields'])) { if (isset($context['included_fields'])) {
$fields = []; $field_items = [];
foreach ($context['included_fields'] as $field_name) { foreach ($context['included_fields'] as $field_name) {
$fields[] = $entity->get($field_name); $field_items[] = $entity->get($field_name);
} }
} }
else { else {
$fields = $entity->getFields(); $field_items = $entity->getFields();
} }
foreach ($fields as $field) { foreach ($field_items as $field) {
// Continue if the current user does not have access to view this field. // Continue if the current user does not have access to view this field.
if (!$field->access('view', $context['account'])) { if (!$field->access('view', $context['account'])) {
continue; continue;
......
...@@ -18,18 +18,18 @@ class FieldNormalizer extends SerializationFieldNormalizer { ...@@ -18,18 +18,18 @@ class FieldNormalizer extends SerializationFieldNormalizer {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($field, $format = NULL, array $context = []) { public function normalize($field_items, $format = NULL, array $context = []) {
$normalized_field_items = []; $normalized_field_items = [];
// Get the field definition. // Get the field definition.
$entity = $field->getEntity(); $entity = $field_items->getEntity();
$field_name = $field->getName(); $field_name = $field_items->getName();
$field_definition = $field->getFieldDefinition(); $field_definition = $field_items->getFieldDefinition();
// If this field is not translatable, it can simply be normalized without // If this field is not translatable, it can simply be normalized without
// separating it into different translations. // separating it into different translations.
if (!$field_definition->isTranslatable()) { if (!$field_definition->isTranslatable()) {
$normalized_field_items = $this->normalizeFieldItems($field, $format, $context); $normalized_field_items = $this->normalizeFieldItems($field_items, $format, $context);
} }
// Otherwise, the languages have to be extracted from the entity and passed // Otherwise, the languages have to be extracted from the entity and passed
// in to the field item normalizer in the context. The langcode is appended // in to the field item normalizer in the context. The langcode is appended
...@@ -38,15 +38,14 @@ public function normalize($field, $format = NULL, array $context = []) { ...@@ -38,15 +38,14 @@ public function normalize($field, $format = NULL, array $context = []) {
foreach ($entity->getTranslationLanguages() as $language) { foreach ($entity->getTranslationLanguages() as $language) {
$context['langcode'] = $language->getId(); $context['langcode'] = $language->getId();
$translation = $entity->getTranslation($language->getId()); $translation = $entity->getTranslation($language->getId());
$translated_field = $translation->get($field_name); $translated_field_items = $translation->get($field_name);
$normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field, $format, $context)); $normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field_items, $format, $context));
} }
} }
// Merge deep so that links set in entity reference normalizers are merged // Merge deep so that links set in entity reference normalizers are merged
// into the links property. // into the links property.
$normalized = NestedArray::mergeDeepArray($normalized_field_items); return NestedArray::mergeDeepArray($normalized_field_items);
return $normalized;
} }
/** /**
...@@ -62,10 +61,10 @@ public function normalize($field, $format = NULL, array $context = []) { ...@@ -62,10 +61,10 @@ public function normalize($field, $format = NULL, array $context = []) {
* @return array * @return array
* The array of normalized field items. * The array of normalized field items.
*/ */
protected function normalizeFieldItems($field, $format, $context) { protected function normalizeFieldItems($field_items, $format, $context) {
$normalized_field_items = []; $normalized_field_items = [];
if (!$field->isEmpty()) { if (!$field_items->isEmpty()) {
foreach ($field as $field_item) { foreach ($field_items as $field_item) {
$normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context); $normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);
} }
} }
......
...@@ -26,9 +26,9 @@ class ComplexDataNormalizer extends NormalizerBase { ...@@ -26,9 +26,9 @@ class ComplexDataNormalizer extends NormalizerBase {
*/ */
public function normalize($object, $format = NULL, array $context = []) { public function normalize($object, $format = NULL, array $context = []) {
$attributes = []; $attributes = [];
/** @var \Drupal\Core\TypedData\TypedDataInterface $field */ /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
foreach ($object as $name => $field) { foreach ($object as $name => $property) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context); $attributes[$name] = $this->serializer->normalize($property, $format, $context);
} }
return $attributes; return $attributes;
} }
......
...@@ -15,15 +15,15 @@ class ContentEntityNormalizer extends EntityNormalizer { ...@@ -15,15 +15,15 @@ class ContentEntityNormalizer extends EntityNormalizer {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($object, $format = NULL, array $context = []) { public function normalize($entity, $format = NULL, array $context = []) {
$context += [ $context += [
'account' => NULL, 'account' => NULL,
]; ];
$attributes = []; $attributes = [];
foreach ($object as $name => $field) { foreach ($entity as $name => $field_items) {
if ($field->access('view', $context['account'])) { if ($field_items->access('view', $context['account'])) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context); $attributes[$name] = $this->serializer->normalize($field_items, $format, $context);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment