Commit 59227265 authored by samuel.mortenson's avatar samuel.mortenson Committed by Samuel Mortenson

Issue #3006617 by samuel.mortenson: Many exports still contain integer IDs

parent d8d1ce49
......@@ -9,6 +9,17 @@ use Drupal\serialization\Normalizer\ContentEntityNormalizer as BaseContentEntity
*/
class ContentEntityNormalizer extends BaseContentEntityNormalizer {
/**
* A list of field names that should be excluded from normalization.
*
* Should only be used when more generic logic cannot be used.
*
* @var array
*/
protected $fieldDenyList = [
'metatag',
];
/**
* {@inheritdoc}
*/
......@@ -27,6 +38,17 @@ class ContentEntityNormalizer extends BaseContentEntityNormalizer {
if ($id_key && isset($values[$id_key])) {
unset($values[$id_key]);
}
foreach ($this->fieldDenyList as $field_name) {
if (isset($values[$field_name])) {
unset($values[$field_name]);
}
}
foreach (['entity_revision_parent_type_field', 'entity_revision_parent_id_field', 'entity_revision_parent_field_name_field'] as $key) {
$field_name = $entity->getEntityType()->get($key);
if ($field_name && isset($values[$field_name])) {
unset($values[$field_name]);
}
}
return $values;
}
......
<?php
namespace Drupal\tome\Normalizer;
use Drupal\serialization\Normalizer\FieldItemNormalizer as BaseFieldItemNormalizer;
/**
* Normalizer for Path fields.
*/
class PathItemNormalizer extends BaseFieldItemNormalizer {
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = '\Drupal\path\Plugin\Field\FieldType\PathItem';
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
$values = parent::normalize($object, $format, $context);
if (isset($values['pid'])) {
unset($values['pid']);
}
return $values;
}
}
......@@ -3,12 +3,11 @@
namespace Drupal\tome\Normalizer;
use Drupal\pathauto\PathautoState;
use Drupal\serialization\Normalizer\FieldItemNormalizer as BaseFieldItemNormalizer;
/**
* Normalizer for Pathauto.
*/
class PathautoItemNormalizer extends BaseFieldItemNormalizer {
class PathautoItemNormalizer extends PathItemNormalizer {
/**
* {@inheritdoc}
......
......@@ -2,8 +2,6 @@
namespace Drupal\tome\Normalizer;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
/**
* Normalizes/denormalizes Drupal user entities into an array structure.
*/
......
......@@ -46,6 +46,7 @@ class ContentEntityNormalizerTest extends UnitTestCase {
'field_foo' => $field_list,
'id_key' => $field_list,
'revision_key' => $field_list,
'metatag' => $field_list,
];
$typed_data = $this->prophesize(ComplexDataInterface::class);
$typed_data->getProperties(TRUE)
......@@ -64,12 +65,14 @@ class ContentEntityNormalizerTest extends UnitTestCase {
->willReturn('id_key');
$entity_type->getKey(Argument::exact('revision'))
->willReturn('revision_key');
$entity_type->get(Argument::any())
->willReturn(NULL);
$content_entity->getEntityType(Argument::cetera())
->willReturn($entity_type);
$values = $normalizer->normalize($content_entity->reveal());
$this->assertEquals(['field_foo' => 'foo'], $values, 'The ID and revision keys are removed properly.');
$this->assertEquals(['field_foo' => 'foo'], $values, 'The ID, revision, and denied keys are removed properly.');
}
}
......@@ -65,6 +65,8 @@ class UserEntityNormalizerTest extends UnitTestCase {
->willReturn('id_key');
$entity_type->getKey(Argument::exact('revision'))
->willReturn('revision_key');
$entity_type->get(Argument::any())
->willReturn(NULL);
$content_entity->getEntityType(Argument::cetera())
->willReturn($entity_type);
......
......@@ -41,6 +41,11 @@ services:
arguments: ['@entity.repository']
serializer.normalizer.pathauto_tome:
class: Drupal\tome\Normalizer\PathautoItemNormalizer
tags:
- { name: normalizer, priority: 9 }
arguments: ['@entity.repository']
serializer.normalizer.path_tome:
class: Drupal\tome\Normalizer\PathItemNormalizer
tags:
- { name: normalizer, priority: 8 }
arguments: ['@entity.repository']
......
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