Commit c2f5a3e8 authored by alexpott's avatar alexpott

Issue #2496897 by sasanikolic, Arla, dawehner, miro_dietiker, Berdir: Throw...

Issue #2496897 by sasanikolic, Arla, dawehner, miro_dietiker, Berdir: Throw helpful exception if link templates are missing leading /
parent 3724634d
......@@ -15,6 +15,7 @@
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 @@ public function clearCachedDefinitions() {
$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}
*/
......
<?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 @@ public function testClearCachedDefinitions() {
$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.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment