rest.api.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
  31. * hook_hal_type_uri_alter() instead. This exists solely for BC.
  32. *
  33. * @see https://www.drupal.org/node/2830467
  34. *
  35. * Modules may wish to alter the type URI generated for a resource based on the
  36. * context of the serializer/normalizer operation.
  37. *
  38. * @param string $uri
  39. * The URI to alter.
  40. * @param array $context
  41. * The context from the serializer/normalizer operation.
  42. *
  43. * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
  44. * @see \Symfony\Component\Serializer\SerializerInterface::deserialize()
  45. * @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
  46. * @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
  47. */
  48. function hook_rest_type_uri_alter(&$uri, $context = []) {
  49. if ($context['mymodule'] == TRUE) {
  50. $base = \Drupal::config('hal.settings')->get('link_domain');
  51. $uri = str_replace($base, 'http://mymodule.domain', $uri);
  52. }
  53. }
  54. /**
  55. * Alter the REST relation URI.
  56. *
  57. * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
  58. * hook_hal_relation_uri_alter() instead. This exists solely for BC.
  59. *
  60. * @see https://www.drupal.org/node/2830467
  61. *
  62. * Modules may wish to alter the relation URI generated for a resource based on
  63. * the context of the serializer/normalizer operation.
  64. *
  65. * @param string $uri
  66. * The URI to alter.
  67. * @param array $context
  68. * The context from the serializer/normalizer operation.
  69. *
  70. * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
  71. * @see \Symfony\Component\Serializer\SerializerInterface::deserialize()
  72. * @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
  73. * @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
  74. */
  75. function hook_rest_relation_uri_alter(&$uri, $context = []) {
  76. if ($context['mymodule'] == TRUE) {
  77. $base = \Drupal::config('hal.settings')->get('link_domain');
  78. $uri = str_replace($base, 'http://mymodule.domain', $uri);
  79. }
  80. }
  81. /**
  82. * @} End of "addtogroup hooks".
  83. */