diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 42e475a93bbd..2d03dff552d0 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -15,6 +15,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\Entity\ConfigEntityType; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Entity\Exception\AmbiguousEntityClassException; +use Drupal\Core\Entity\Exception\InvalidLinkTemplateException; use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -226,6 +227,21 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa $this->handlers = array(); } + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */ + parent::processDefinition($definition, $plugin_id); + + // All link templates must have a leading slash. + foreach ((array) $definition->getLinkTemplates() as $link_relation_name => $link_template) { + if ($link_template[0] != '/') { + throw new InvalidLinkTemplateException("Link template '$link_relation_name' for entity type '$plugin_id' must start with a leading slash, the current link template is '$link_template'"); + } + } + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/Exception/InvalidLinkTemplateException.php b/core/lib/Drupal/Core/Entity/Exception/InvalidLinkTemplateException.php new file mode 100644 index 000000000000..f64d39fc4bd6 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Exception/InvalidLinkTemplateException.php @@ -0,0 +1,14 @@ +entityManager->clearCachedDefinitions(); } + /** + * Tests the processDefinition() method. + * + * @covers ::processDefinition + * + * @expectedException \Drupal\Core\Entity\Exception\InvalidLinkTemplateException + * @expectedExceptionMessage Link template 'canonical' for entity type 'apple' must start with a leading slash, the current link template is 'path/to/apple' + */ + public function testProcessDefinition() { + $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); + $apple->expects($this->once()) + ->method('getLinkTemplates') + ->willReturn(['canonical' => 'path/to/apple']); + + $this->setUpEntityManager(array('apple' => $apple)); + + $this->entityManager->processDefinition($apple, 'apple'); + } + /** * Tests the getDefinition() method. *