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 = []) {
// If the fields to use were specified, only output those field values.
if (isset($context['included_fields'])) {
$fields = [];
$field_items = [];
foreach ($context['included_fields'] as $field_name) {
$fields[] = $entity->get($field_name);
$field_items[] = $entity->get($field_name);
}
}
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.
if (!$field->access('view', $context['account'])) {
continue;
......
......@@ -18,18 +18,18 @@ class FieldNormalizer extends SerializationFieldNormalizer {
/**
* {@inheritdoc}
*/
public function normalize($field, $format = NULL, array $context = []) {
public function normalize($field_items, $format = NULL, array $context = []) {
$normalized_field_items = [];
// Get the field definition.
$entity = $field->getEntity();
$field_name = $field->getName();
$field_definition = $field->getFieldDefinition();
$entity = $field_items->getEntity();
$field_name = $field_items->getName();
$field_definition = $field_items->getFieldDefinition();
// If this field is not translatable, it can simply be normalized without
// separating it into different translations.
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
// 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 = []) {
foreach ($entity->getTranslationLanguages() as $language) {
$context['langcode'] = $language->getId();
$translation = $entity->getTranslation($language->getId());
$translated_field = $translation->get($field_name);
$normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field, $format, $context));
$translated_field_items = $translation->get($field_name);
$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
// into the links property.
$normalized = NestedArray::mergeDeepArray($normalized_field_items);
return $normalized;
return NestedArray::mergeDeepArray($normalized_field_items);
}
/**
......@@ -62,10 +61,10 @@ public function normalize($field, $format = NULL, array $context = []) {
* @return array
* The array of normalized field items.
*/
protected function normalizeFieldItems($field, $format, $context) {
protected function normalizeFieldItems($field_items, $format, $context) {
$normalized_field_items = [];
if (!$field->isEmpty()) {
foreach ($field as $field_item) {
if (!$field_items->isEmpty()) {
foreach ($field_items as $field_item) {
$normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);
}
}
......
......@@ -26,9 +26,9 @@ class ComplexDataNormalizer extends NormalizerBase {
*/
public function normalize($object, $format = NULL, array $context = []) {
$attributes = [];
/** @var \Drupal\Core\TypedData\TypedDataInterface $field */
foreach ($object as $name => $field) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
foreach ($object as $name => $property) {
$attributes[$name] = $this->serializer->normalize($property, $format, $context);
}
return $attributes;
}
......
......@@ -15,15 +15,15 @@ class ContentEntityNormalizer extends EntityNormalizer {
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
public function normalize($entity, $format = NULL, array $context = []) {
$context += [
'account' => NULL,
];
$attributes = [];
foreach ($object as $name => $field) {
if ($field->access('view', $context['account'])) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
foreach ($entity as $name => $field_items) {
if ($field_items->access('view', $context['account'])) {
$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