rest.api.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file
  4. * Describes hooks provided by the RESTful Web Services module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Alter the resource plugin definitions.
  12. *
  13. * @param array $definitions
  14. * The collection of resource definitions.
  15. */
  16. function hook_rest_resource_alter(&$definitions) {
  17. if (isset($definitions['entity:node'])) {
  18. // We want to handle REST requests regarding nodes with our own plugin
  19. // class.
  20. $definitions['entity:node']['class'] = 'Drupal\mymodule\Plugin\rest\resource\NodeResource';
  21. // Serialized nodes should be expanded to my specific node class.
  22. $definitions['entity:node']['serialization_class'] = 'Drupal\mymodule\Entity\MyNode';
  23. }
  24. // We don't want Views to show up in the array of plugins at all.
  25. unset($definitions['entity:view']);
  26. }
  27. /**
  28. * Alter the REST type URI.
  29. *
  30. * Modules may wish to alter the type URI generated for a resource based on the
  31. * context of the serializer/normalizer operation.
  32. *
  33. * @param string $uri
  34. * The URI to alter.
  35. * @param array $context
  36. * The context from the serializer/normalizer operation.
  37. *
  38. * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
  39. * @see \Symfony\Component\Serializer\SerializerInterface::deserialize()
  40. * @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
  41. * @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
  42. */
  43. function hook_rest_type_uri_alter(&$uri, $context = array()) {
  44. if ($context['mymodule'] == TRUE) {
  45. $base = \Drupal::config('rest.settings')->get('link_domain');
  46. $uri = str_replace($base, 'http://mymodule.domain', $uri);
  47. }
  48. }
  49. /**
  50. * Alter the REST relation URI.
  51. *
  52. * Modules may wish to alter the relation URI generated for a resource based on
  53. * the context of the serializer/normalizer operation.
  54. *
  55. * @param string $uri
  56. * The URI to alter.
  57. * @param array $context
  58. * The context from the serializer/normalizer operation.
  59. *
  60. * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
  61. * @see \Symfony\Component\Serializer\SerializerInterface::deserialize()
  62. * @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
  63. * @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
  64. */
  65. function hook_rest_relation_uri_alter(&$uri, $context = array()) {
  66. if ($context['mymodule'] == TRUE) {
  67. $base = \Drupal::config('rest.settings')->get('link_domain');
  68. $uri = str_replace($base, 'http://mymodule.domain', $uri);
  69. }
  70. }
  71. /**
  72. * @} End of "addtogroup hooks".
  73. */