Commit 24c6a029 authored by andypost's avatar andypost Committed by larowlan

Issue #2687075 by andypost, gaydabura, aleevas, larowlan, jibran: Taxonomy...

Issue #2687075 by andypost, gaydabura, aleevas, larowlan, jibran: Taxonomy hierarchy is not exported
parent c6a79ee2
<?php
namespace Drupal\default_content;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* Adds customized normalizer to handle taxonomy hierarchy.
*/
class DefaultContentServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
$modules = $container->getParameter('container.modules');
// @todo Get rid of after https://www.drupal.org/node/2543726
if (isset($modules['taxonomy'])) {
// Add a normalizer service for term entities.
$service_definition = new Definition('Drupal\default_content\Normalizer\TermEntityNormalizer', [
new Reference('rest.link_manager'),
new Reference('entity.manager'),
new Reference('module_handler'),
]);
// The priority must be higher than that of
// serializer.normalizer.entity.hal in hal.services.yml
$service_definition->addTag('normalizer', ['priority' => 30]);
$container->setDefinition('default_content.normalizer.taxonomy_term.halt', $service_definition);
}
}
}
<?php
namespace Drupal\default_content\Normalizer;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\hal\Normalizer\ContentEntityNormalizer;
use Drupal\rest\LinkManager\LinkManagerInterface;
/**
* Defines a class for normalizing terms to ensure parent is stored.
*/
class TermEntityNormalizer extends ContentEntityNormalizer {
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = 'Drupal\taxonomy\TermInterface';
/**
* Term storage.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $termStorage;
/**
* {@inheritdoc}
*/
public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($link_manager, $entity_manager, $module_handler);
$this->termStorage = $entity_manager->getStorage('taxonomy_term');
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = array()) {
if ($parents = $this->termStorage->loadParents($entity->id())) {
$entity->parent->setValue(array_keys($parents));
}
return parent::normalize($entity, $format, $context);
}
}
......@@ -62,6 +62,21 @@ class DefaultContentManagerIntegrationTest extends KernelTestBase {
// Ensure the proper UUID is part of it.
$this->assertEqual($exported_decoded->uuid[0]->value, $term->uuid());
$this->assertEqual($exported, $expected);
// Tests export of taxonomy parent field.
// @todo Get rid of after https://www.drupal.org/node/2543726
$child_term = $term = Term::create([
'vid' => $vocabulary->id(),
'name' => 'child_name',
'parent' => $term->id(),
]);
$child_term->save();
// Make sure parent relation is exported.
$exported = $this->defaultContentManager->exportContent('taxonomy_term', $child_term->id());
$relation_uri = 'http://drupal.org/rest/relation/taxonomy_term/test/parent';
$exported_decoded = json_decode($exported);
$this->assertFalse(empty($exported_decoded->_links->{$relation_uri}));
$this->assertFalse(empty($exported_decoded->_embedded->{$relation_uri}));
}
/**
......
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