Prior to 8.0-beta11, the domain used in the Relation and Type link managers in the rest module was hard-coded to that of the site.
This meant that solutions that wanted to alter these links had to implement a service modifier and take over rest type and relation link generation.
A new interface \Drupal\rest\LinkManager\ConfigurableLinkManagerInterface was added with method setLinkDomain(). This interface is implemented by \Drupal\rest\LinkManager\LinkManager. Modules generating serialized content via the serializer can call setLinkDomain prior to calling the serialize method to ensure the generated output contains the domain they require. The value can be subsequently reset by calling setLinkDomain(FALSE).
Site builder/developers can optionally override the default link domain globally/permanently using the rest.settings.link_domain configuration option.
The signatures of \Drupal\rest\LinkManager\RelationLinkManagerInterface::getRelationUri() and \Drupal\rest\LinkManager\TypeLinkManagerInterface::getTypeUri() have changed - an additional optional $context param has been added allowing calling modules to pass along meta-data as required.
Two new alter hooks hook_rest_type_uri_alter() and hook_rest_relation_uri_alter() - allowing contrib projects to alter the generated type and relation URIs respectively. Each hook receives the URI by reference and the context from the caller. If your module is the one calling the serialize/normalize methods on the serializer, you shouldn't use the alter hook - instead use the setLinkDomain method on the rest.link_manager service. The alter hooks are for when another module is calling the serialize/normalize methods and your modules wants to intervene.
The interface change will impact at minimum file_entity and default_content in contrib - however it will allow the code to be simplified as these modules will no longer need to replace the default rest.module link managers.
Before
Prior to this change to modify the domain used in links generated by the serializer
- Implement
\Drupal\Core\DependencyInjection\ServiceModifierInterface. - Globally hijack the
@rest.link_manager service. - Implement your own domain logic and hope no other module needs to do the same.
OR
- Serialize as is
- Result to str_replace/preg_replace to update domains
- e.g
// My site is barfoo.com, I want the links to say foobar.com. $serializer = \Drupal::service('serializer'); $output = $serializer->normalize($node, 'hal_json'); // All relation/type links in $output will reference http://barfoo.com. // Hope none of the content legitimately references barfoo.com. $output = str_replace('barfoo.com', 'foobar.com', $output);
After
-
$serializer = \Drupal::service('serializer'); $link_manager = \Drupal::service('rest.link_manager'); $link_manager->setLinkDomain('http://foobar.com'); $output = $serializer->normalize($node, 'hal_json'); // All relation/type links in $output will reference http://foobar.com. $link_manager->setLinkDomain(FALSE');