Issue #2496897 by sasanikolic, Arla, dawehner, miro_dietiker, Berdir: Throw helpful exception if link templates are missing leading /
parent
3724634d74
commit
c2f5a3e8e8
|
@ -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}
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Entity\Exception\InvalidLinkTemplateException.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Entity\Exception;
|
||||
|
||||
/**
|
||||
* Indicates that a link template does not follow the required pattern.
|
||||
*/
|
||||
class InvalidLinkTemplateException extends \Exception {
|
||||
|
||||
}
|
|
@ -265,6 +265,25 @@ class EntityManagerTest extends UnitTestCase {
|
|||
$this->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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue