diff --git a/src/Controller/JsonApiEntityResource.php b/src/Controller/JsonApiEntityResource.php new file mode 100644 index 0000000000000000000000000000000000000000..a51a16551268a9d1af6a5761a913f3f22a90d227 --- /dev/null +++ b/src/Controller/JsonApiEntityResource.php @@ -0,0 +1,17 @@ +<?php + +namespace Drupal\jsonapi_links\Controller; + +use Drupal\jsonapi\Controller\EntityResource; + +/** + * Overrides EntityResource JSON API. + * + * All code bellow is copied form EntityAccessChecker and required + * only to replace ResourceObject. + */ +class JsonApiEntityResource extends EntityResource { + + use MethodOverrideTrait; + +} diff --git a/src/Controller/JsonApiExtrasEntityResource.php b/src/Controller/JsonApiExtrasEntityResource.php new file mode 100644 index 0000000000000000000000000000000000000000..a9f3b461a3eaefb9b86d54c3f6a2dbad532c34b3 --- /dev/null +++ b/src/Controller/JsonApiExtrasEntityResource.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\jsonapi_links\Controller; + +// This controller is implemented by jsonapi_extras, +// so we need to inherit it instead of the default jsonapi controller. +use Drupal\jsonapi_defaults\Controller\EntityResource; + +/** + * Overrides EntityResource JSON API. + * + * All code bellow is copied form EntityAccessChecker and required + * only to replace ResourceObject. + */ +class JsonApiExtrasEntityResource extends EntityResource { + + use MethodOverrideTrait; + +} diff --git a/src/Controller/EntityResource.php b/src/Controller/MethodOverrideTrait.php similarity index 96% rename from src/Controller/EntityResource.php rename to src/Controller/MethodOverrideTrait.php index 4658579b9d2caf198cd75a8af6882d796e620df1..d5eac445a2c1a2e921bb76f8f4c32f54b227f261 100644 --- a/src/Controller/EntityResource.php +++ b/src/Controller/MethodOverrideTrait.php @@ -9,13 +9,13 @@ use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\RevisionableEntityBundleInterface; use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\Entity\RevisionLogInterface; -use Drupal\jsonapi\Controller\EntityResource as JsonapiEntityResource; use Drupal\jsonapi\Exception\EntityAccessDeniedHttpException; use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel; use Drupal\jsonapi\JsonApiResource\Relationship; use Drupal\jsonapi\JsonApiResource\ResourceObjectData; use Drupal\jsonapi\ResourceType\ResourceType; use Drupal\jsonapi\ResourceType\ResourceTypeField; + // jsonapi_links: changed the namespace for ResourceObject. use Drupal\jsonapi_links\JsonApiResource\ResourceObject; use Symfony\Component\HttpFoundation\Request; @@ -23,12 +23,9 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\ConflictHttpException; /** - * Overrides EntityResource JSON API. - * - * All code bellow is copied form EntityAccessChecker and required - * only to replace ResourceObject. + * Provides general methods overrides for the JSON:API module. */ -class EntityResource extends JsonapiEntityResource { +trait MethodOverrideTrait { /** * {@inheritdoc} diff --git a/src/JsonapiLinksServiceProvider.php b/src/JsonapiLinksServiceProvider.php index 6c796d1561e33afaaf7e35c15d12eb2526ed249c..7fa0a7c94fb651cc71d46bb3e8cb0c97cd65ae05 100644 --- a/src/JsonapiLinksServiceProvider.php +++ b/src/JsonapiLinksServiceProvider.php @@ -23,8 +23,14 @@ class JsonapiLinksServiceProvider extends ServiceProviderBase { ->setClass('Drupal\jsonapi\Normalizer\ImpostorFrom\jsonapi_links\ResourceObjectNormalizer'); // Overrides json api controller. - $container->getDefinition('jsonapi.entity_resource') - ->setClass('Drupal\jsonapi_links\Controller\EntityResource'); + if (isset($container_modules['jsonapi_defaults'])) { + $container->getDefinition('jsonapi.entity_resource') + ->setClass('Drupal\jsonapi_links\Controller\JsonApiExtrasEntityResource'); + } + else { + $container->getDefinition('jsonapi.entity_resource') + ->setClass('Drupal\jsonapi_links\Controller\JsonApiEntityResource'); + } // Overrides json api access checker. $container->getDefinition('jsonapi.entity_access_checker')