3 namespace Drupal\rest\PathProcessor;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
7 use Symfony\Component\HttpFoundation\Request;
10 * Path processor to maintain BC for entity REST resource URLs from Drupal 8.0.
12 class PathProcessorEntityResourceBC implements InboundPathProcessorInterface {
15 * The entity type manager.
17 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19 protected $entityTypeManager;
22 * Creates a new PathProcessorEntityResourceBC instance.
24 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
25 * The entity type manager.
27 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
28 $this->entityTypeManager = $entity_type_manager;
34 public function processInbound($path, Request $request) {
35 if ($request->getMethod() === 'POST' && strpos($path, '/entity/') === 0) {
36 $parts = explode('/', $path);
37 $entity_type_id = array_pop($parts);
39 // Until Drupal 8.3, no entity types specified a link template for the
40 // 'create' link relation type. As of Drupal 8.3, all core content entity
41 // types provide this link relation type. This inbound path processor
42 // provides automatic backwards compatibility: it allows both the old
43 // default from \Drupal\rest\Plugin\rest\resource\EntityResource, i.e.
44 // "/entity/{entity_type}" and the link template specified in a particular
45 // entity type. The former is rewritten to the latter
46 // specific one if it exists.
47 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
48 if ($entity_type->hasLinkTemplate('create')) {
49 return $entity_type->getLinkTemplate('create');