Commit 96e7a2dd authored by larowlan's avatar larowlan

Rest link domain is now configurable

parent 97c8d228
services:
default_content.manager:
class: Drupal\default_content\DefaultContentManager
arguments: ['@serializer', '@plugin.manager.rest', '@current_user', '@entity.manager']
default_content.link_manager.type:
class: Drupal\default_content\LinkManager\TypeLinkManager
arguments: ['@cache.default', '@unrouted_url_assembler']
default_content.link_manager.relation:
class: Drupal\default_content\LinkManager\RelationLinkManager
arguments: ['@cache.default', '@entity.manager']
arguments: ['@serializer', '@plugin.manager.rest', '@current_user', '@entity.manager', '@rest.link_manager']
......@@ -12,6 +12,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Session\AccountInterface;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Gliph\Graph\DirectedAdjacencyList;
use Gliph\Traversal\DepthFirst;
......@@ -23,6 +24,8 @@ use Symfony\Component\Serializer\Serializer;
*/
class DefaultContentManager implements DefaultContentManagerInterface {
const LINK_DOMAIN = 'http://drupal.org';
/**
* The serializer service.
*
......@@ -72,6 +75,13 @@ class DefaultContentManager implements DefaultContentManagerInterface {
*/
protected $vertexes = array();
/**
* The link manager service.
*
* @var \Drupal\rest\LinkManager\LinkManagerInterface
*/
protected $linkManager;
/**
* Constructs the default content manager.
*
......@@ -79,15 +89,18 @@ class DefaultContentManager implements DefaultContentManagerInterface {
* The serializer service.
* @param \Drupal\rest\Plugin\Type\ResourcePluginManager $resource_plugin_manager
* The rest resource plugin manager.
* @param \Drupal\Core\Session|AccountInterface $current_user .
* @param \Drupal\Core\Session|AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* The link manager service.
*/
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, AccountInterface $current_user, EntityManager $entity_manager) {
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, AccountInterface $current_user, EntityManager $entity_manager, LinkManagerInterface $link_manager) {
$this->serializer = $serializer;
$this->resourcePluginManager = $resource_plugin_manager;
$this->entityManager = $entity_manager;
$this->linkManager = $link_manager;
}
/**
......@@ -109,6 +122,9 @@ class DefaultContentManager implements DefaultContentManagerInterface {
continue;
}
$files = $this->scanner()->scan($folder . '/' . $entity_type_id);
// Default content uses drupal.org as domain.
// @todo Make this use a uri like default-content:.
$this->linkManager->setLinkDomain(static::LINK_DOMAIN);
// Parse all of the files and sort them in order of dependency.
foreach ($files as $file) {
$contents = $this->parseFile($file);
......@@ -124,6 +140,8 @@ class DefaultContentManager implements DefaultContentManagerInterface {
'@first' => $file_map[$self]->uri,
'@second' => $file->uri,
);
// Reset link domain.
$this->linkManager->setLinkDomain(FALSE);
throw new \Exception(SafeMarkup::format('Default content with href @href exists twice: @first @second', $args));
}
......@@ -149,7 +167,7 @@ class DefaultContentManager implements DefaultContentManagerInterface {
// @todo what if no dependencies?
$sorted = $this->sortTree();
foreach($sorted as $vertex) {
foreach ($sorted as $vertex) {
if (!empty($file_map[$vertex->link])) {
$file = $file_map[$vertex->link];
$entity_type_id = $file->entity_type_id;
......@@ -166,6 +184,8 @@ class DefaultContentManager implements DefaultContentManagerInterface {
}
// Reset the tree.
$this->resetTree();
// Reset link domain.
$this->linkManager->setLinkDomain(FALSE);
return $created;
}
......@@ -176,7 +196,11 @@ class DefaultContentManager implements DefaultContentManagerInterface {
$storage = $this->entityManager->getStorage($entity_type_id);
$entity = $storage->load($entity_id);
return $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$this->linkManager->setLinkDomain(static::LINK_DOMAIN);
$return = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
// Reset link domain.
$this->linkManager->setLinkDomain(FALSE);
return $return;
}
/**
......@@ -196,10 +220,13 @@ class DefaultContentManager implements DefaultContentManagerInterface {
$entities = array_merge($entities, $this->getEntityReferencesRecursive($entity));
$serialized_entities_per_type = [];
$this->linkManager->setLinkDomain(static::LINK_DOMAIN);
// Serialize all entities and key them by entity TYPE and uuid.
foreach ($entities as $entity) {
$serialized_entities_per_type[$entity->getEntityTypeId()][$entity->uuid()] = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
}
$this->linkManager->setLinkDomain(FALSE);
return $serialized_entities_per_type;
}
......
<?php
/**
* @file
* Contains \Drupal\default_content\DefaultContentServiceProvider.
*/
namespace Drupal\default_content;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Reference;
/**
* Creates a service modifier to hijack the rest typed link manager service.
*/
class DefaultContentServiceProvider implements ServiceModifierInterface, ServiceProviderInterface {
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {}
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
if ($link_manager = $container->getDefinition('rest.link_manager')) {
$link_manager->replaceArgument(0, new Reference('default_content.link_manager.type'));
$link_manager->replaceArgument(1, new Reference('default_content.link_manager.relation'));
$container->setDefinition('rest.link_manager', $link_manager);
}
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\default_content\Tests;
use Drupal\default_content\DefaultContentManager;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\KernelTestBase;
......@@ -57,6 +58,7 @@ class DefaultContentManagerIntegrationTest extends KernelTestBase {
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
\Drupal::service('rest.link_manager')->setLinkDomain(DefaultContentManager::LINK_DOMAIN);
$expected = $serializer->serialize($term, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$exported = $this->defaultContentManager->exportContent('taxonomy_term', $term->id());
......@@ -89,6 +91,7 @@ class DefaultContentManagerIntegrationTest extends KernelTestBase {
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
\Drupal::service('rest.link_manager')->setLinkDomain(DefaultContentManager::LINK_DOMAIN);
$expected_node = $serializer->serialize($node, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$expected_user = $serializer->serialize($user, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
......
......@@ -42,8 +42,7 @@ class DefaultContentTest extends WebTestBase {
$this->rebuildContainer();
$node = $this->drupalGetNodeByTitle('Imported node');
$this->assertEqual($node->body->value, 'Crikey it works!');
// Content is always imported as anonymous.
$this->assertEqual($node->uid->target_id, 0);
$this->assertEqual($node->uid->target_id, 2);
$this->assertEqual($node->getType(), 'page');
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadMultiple();
$term = reset($terms);
......
......@@ -6,7 +6,7 @@ entity_type: node
bundle: page
label: Tags
description: 'Select the tag.'
required: 1
required: true
default_value: { }
default_value_function: ''
settings:
......
......@@ -10,7 +10,7 @@ settings:
module: taxonomy
active: true
locked: false
cardinality: '1'
cardinality: 1
translatable: false
indexes: { }
dependencies:
......
......@@ -8,12 +8,12 @@
},
"http://drupal.org/rest/relation/node/page/uid": [
{
"href": "http://drupal.org/user/1"
"href": "http://drupal.org/user/2"
}
],
"http://drupal.org/rest/relation/node/page/revision_uid": [
{
"href": "http://drupal.org/user/1"
"href": "http://drupal.org/user/2"
}
],
"http://drupal.org/rest/relation/node/page/field_tags":[
......
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