Commit 9801d9a5 authored by webchick's avatar webchick

Issue #2345793 by tim.plunkett | xjm: Entities should provide a method for a...

Issue #2345793 by tim.plunkett | xjm: Entities should provide a method for a properly generated link to the entity.
parent 688f773d
......@@ -357,6 +357,13 @@ public function url($rel = 'edit-form', $options = array()) {
return parent::url($rel, $options);
}
/**
* {@inheritdoc}
*/
public function link($text = NULL, $rel = 'edit-form', array $options = []) {
return parent::link($text, $rel, $options);
}
/**
* {@inheritdoc}
*/
......
......@@ -15,6 +15,7 @@
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
......@@ -229,6 +230,19 @@ protected function linkTemplates() {
return $this->getEntityType()->getLinkTemplates();
}
/**
* {@inheritdoc}
*/
public function link($text = NULL, $rel = 'canonical', array $options = []) {
if (is_null($text)) {
$text = $this->label();
}
$url = $this->urlInfo($rel);
$options += $url->getOptions();
$url->setOptions($options);
return (new Link($text, $url))->toString();
}
/**
* {@inheritdoc}
*/
......
......@@ -141,6 +141,23 @@ public function urlInfo($rel = 'canonical', array $options = array());
*/
public function url($rel = 'canonical', $options = array());
/**
* Generates the HTML for a link to this entity.
*
* @param string|null $text
* (optional) The link text for the anchor tag as a translated string.
* If NULL, it will use the entity's label. Defaults to NULL.
* @param string $rel
* (optional) The link relationship type. Defaults to 'canonical'.
* @param array $options
* See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* the available options.
*
* @return string
* An HTML string containing a link to the entity.
*/
public function link($text = NULL, $rel = 'canonical', array $options = []);
/**
* Returns the internal path for this entity.
*
......
......@@ -7,11 +7,15 @@
namespace Drupal\Core;
use Drupal\Core\Routing\LinkGeneratorTrait;
/**
* Defines an object that holds information about a link.
*/
class Link {
use LinkGeneratorTrait;
/**
* The text of the link.
*
......@@ -116,4 +120,11 @@ public function setUrl(Url $url) {
return $this;
}
/**
* Generates the HTML for this Link object.
*/
public function toString() {
return $this->getLinkGenerator()->generateFromLink($this);
}
}
......@@ -92,7 +92,7 @@ public function save(array $form, FormStateInterface $form_state) {
$block_type = $this->entity;
$status = $block_type->save();
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
$logger = $this->logger('block_content');
if ($status == SAVED_UPDATED) {
drupal_set_message(t('Custom block type %label has been updated.', array('%label' => $block_type->label())));
......
......@@ -44,7 +44,7 @@ public function buildHeader() {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['type'] = \Drupal::linkGenerator()->generateFromUrl($entity->label(), $entity->urlInfo());
$row['type'] = $entity->link();
$row['description'] = Xss::filterAdmin($entity->description);
return $row + parent::buildRow($entity);
}
......
......@@ -147,7 +147,7 @@ public function save(array $form, FormStateInterface $form_state) {
$comment_type = $this->entity;
$status = $comment_type->save();
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message(t('Comment type %label has been updated.', array('%label' => $comment_type->label())));
$this->logger->notice('Comment type %label has been updated.', array('%label' => $comment_type->label(), 'link' => $edit_link));
......
......@@ -96,8 +96,7 @@ public function save(array $form, FormStateInterface $form_state) {
$status = $contact_form->save();
$contact_settings = $this->config('contact.settings');
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Contact form %label has been updated.', array('%label' => $contact_form->label())));
$this->logger('contact')->notice('Contact form %label has been updated.', array('%label' => $contact_form->label(), 'link' => $edit_link));
......
......@@ -182,7 +182,7 @@ public function save(array $form, FormStateInterface $form_state) {
$status = $menu->save();
$edit_link = $this->linkGenerator->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Menu %label has been updated.', array('%label' => $menu->label())));
$this->logger('menu')->notice('Menu %label has been updated.', array('%label' => $menu->label(), 'link' => $edit_link));
......
......@@ -138,7 +138,7 @@ public function save(array $form, FormStateInterface $form_state) {
$vocabulary->name = trim($vocabulary->name);
$status = $vocabulary->save();
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created new vocabulary %name.', array('%name' => $vocabulary->name)));
......
......@@ -59,7 +59,7 @@ public function save(array $form, FormStateInterface $form_state) {
$entity->set('label', trim($entity->label()));
$status = $entity->save();
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Role %label has been updated.', array('%label' => $entity->label())));
$this->logger('user')->notice('Role %label has been updated.', array('%label' => $entity->label(), 'link' => $edit_link));
......
......@@ -930,6 +930,13 @@ public function urlInfo($rel = 'edit-form', array $options = []) {
return $this->storage->urlInfo($rel, $options);
}
/**
* {@inheritdoc}
*/
public function link($text = NULL, $rel = 'edit-form', array $options = []) {
return $this->storage->link($text, $rel, $options);
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Entity\EntityLinkTest.
*/
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Entity\Entity
* @group Entity
*/
class EntityLinkTest extends UnitTestCase {
/**
* The mocked entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityManager;
/**
* The tested link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $linkGenerator;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('link_generator', $this->linkGenerator);
\Drupal::setContainer($container);
}
/**
* @covers ::link
*
* @dataProvider providerTestLink
*/
public function testLink($entity_label, $link_text, $expected_text, $link_rel = 'canonical', array $link_options = []) {
$route_name_map = [
'canonical' => 'entity.test_entity_type.canonical',
'edit-form' => 'entity.test_entity_type.edit_form',
];
$route_name = $route_name_map[$link_rel];
$entity_id = 'test_entity_id';
$entity_type_id = 'test_entity_type';
$expected = '<a href="/test_entity_type/test_entity_id">' . $expected_text . '</a>';
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type->expects($this->once())
->method('getLinkTemplates')
->willReturn($route_name_map);
$entity_type->expects($this->any())
->method('getKey')
->with('label')
->willReturn('label');
$this->entityManager
->expects($this->any())
->method('getDefinition')
->with($entity_type_id)
->will($this->returnValue($entity_type));
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', [
['id' => $entity_id, 'label' => $entity_label],
$entity_type_id
]);
$expected_link = Link::createFromRoute(
$expected_text,
$route_name,
[$entity_type_id => $entity_id],
['entity_type' => $entity_type_id, 'entity' => $entity] + $link_options
)->setLinkGenerator($this->linkGenerator);
$this->linkGenerator->expects($this->once())
->method('generateFromLink')
->with($this->equalTo($expected_link))
->willReturn($expected);
$this->assertSame($expected, $entity->link($link_text, $link_rel, $link_options));
}
/**
* Provides test data for testLink().
*/
public function providerTestLink() {
$data = [];
$data[] = [
'some_entity_label',
'qwerqwer',
'qwerqwer',
];
$data[] = [
'some_entity_label',
NULL,
'some_entity_label',
];
$data[] = [
'some_entity_label',
'0',
'0',
];
$data[] = [
'some_entity_label',
'qwerqwer',
'qwerqwer',
'edit-form',
];
$data[] = [
'some_entity_label',
'qwerqwer',
'qwerqwer',
'edit-form',
];
$data[] = [
'some_entity_label',
'qwerqwer',
'qwerqwer',
'edit-form',
['foo' => 'qwer'],
];
return $data;
}
}
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