Commit 77725b60 authored by alexpott's avatar alexpott
Browse files

Issue #2281645 by dawehner, andypost: Make entity annotations use link...

Issue #2281645 by dawehner, andypost: Make entity annotations use link templates instead of route names
parent e298180f
......@@ -164,8 +164,9 @@ public function urlInfo($rel = 'canonical', array $options = []) {
$link_templates = $this->linkTemplates();
if (isset($link_templates[$rel])) {
// If there is a template for the given relationship type, generate the path.
$uri = new Url($link_templates[$rel], $this->urlRouteParameters($rel));
$route_parameters = $this->urlRouteParameters($rel);
$route_name = "entity.{$this->entityTypeId}." . str_replace(array('-', 'drupal:'), array('_', ''), $rel);
$uri = new Url($route_name, $route_parameters);
}
else {
$bundle = $this->bundle();
......@@ -224,7 +225,7 @@ public function hasLinkTemplate($rel) {
* Returns an array link templates.
*
* @return array
* An array of link templates containing route names.
* An array of link templates containing paths.
*/
protected function linkTemplates() {
return $this->getEntityType()->getLinkTemplates();
......
......@@ -105,9 +105,9 @@ public function label();
* example:
* @code
* links = {
* "canonical" = "entity.node.canonical",
* "edit-form" = "entity.node.edit_form",
* "version-history" = "entity.node.version_history"
* "canonical" = "/node/{node}",
* "edit-form" = "/node/{node}/edit",
* "version-history" = "/node/{node}/revisions"
* }
* @endcode
* or specified in a callback function set like:
......
......@@ -546,8 +546,12 @@ public function hasLinkTemplate($key) {
/**
* {@inheritdoc}
*/
public function setLinkTemplate($key, $route_name) {
$this->links[$key] = $route_name;
public function setLinkTemplate($key, $path) {
if ($path[0] !== '/') {
throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
}
$this->links[$key] = $path;
return $this;
}
......
......@@ -455,7 +455,7 @@ public function getLinkTemplates();
* The link type.
*
* @return string|bool
* The route name for this link, or FALSE if it doesn't exist.
* The path for this link, or FALSE if it doesn't exist.
*/
public function getLinkTemplate($key);
......@@ -475,12 +475,15 @@ public function hasLinkTemplate($key);
*
* @param string $key
* The name of a link.
* @param string $route_name
* The route name to use for the link.
* @param string $path
* The route path to use for the link.
*
* @return $this
*
* @throws \InvalidArgumentException
* Thrown when the path does not start with a leading slash.
*/
public function setLinkTemplate($key, $route_name);
public function setLinkTemplate($key, $path);
/**
* Gets the callback for the label of the entity.
......
......@@ -44,6 +44,6 @@ function action_entity_type_build(array &$entity_types) {
->setFormClass('edit', 'Drupal\action\ActionEditForm')
->setFormClass('delete', 'Drupal\action\Form\ActionDeleteForm')
->setListBuilderClass('Drupal\action\ActionListBuilder')
->setLinkTemplate('delete-form', 'entity.action.delete_form')
->setLinkTemplate('edit-form', 'entity.action.edit_form');
->setLinkTemplate('delete-form', '/admin/config/system/actions/configure/{action}/delete')
->setLinkTemplate('edit-form', '/admin/config/system/actions/configure/{action}');
}
......@@ -33,9 +33,9 @@
* }
* },
* links = {
* "canonical" = "entity.aggregator_feed.canonical",
* "edit-form" = "entity.aggregator_feed.edit_form",
* "delete-form" = "entity.aggregator_feed.delete_form",
* "canonical" = "/aggregator/sources/{aggregator_feed}",
* "edit-form" = "/aggregator/sources/{aggregator_feed}/configure",
* "delete-form" = "/aggregator/sources/{aggregator_feed}/delete",
* },
* field_ui_base_route = "aggregator.admin_overview",
* base_table = "aggregator_feed",
......
......@@ -36,8 +36,8 @@
* "id" = "id"
* },
* links = {
* "delete-form" = "entity.block.delete_form",
* "edit-form" = "entity.block.edit_form"
* "delete-form" = "/admin/structure/block/manage/{block}/delete",
* "edit-form" = "/admin/structure/block/manage/{block}"
* }
* )
*/
......
......@@ -45,6 +45,15 @@ entity.block_content.canonical:
requirements:
_entity_access: 'block_content.update'
entity.block_content.edit_form:
path: '/block/{block_content}'
defaults:
_entity_form: 'block_content.edit'
options:
_admin_route: TRUE
requirements:
_entity_access: 'block_content.update'
entity.block_content.delete_form:
path: '/block/{block_content}/delete'
defaults:
......
......@@ -39,9 +39,9 @@
* revision_table = "block_content_revision",
* data_table = "block_content_field_data",
* links = {
* "canonical" = "entity.block_content.canonical",
* "delete-form" = "entity.block_content.delete_form",
* "edit-form" = "entity.block_content.canonical",
* "canonical" = "/block/{block_content}",
* "delete-form" = "/block/{block_content}/delete",
* "edit-form" = "/block/{block_content}",
* },
* translatable = TRUE,
* entity_keys = {
......
......@@ -35,8 +35,8 @@
* "label" = "label"
* },
* links = {
* "delete-form" = "entity.block_content_type.delete_form",
* "edit-form" = "entity.block_content_type.edit_form"
* "delete-form" = "/admin/structure/block/block-content/manage/{block_content_type}/delete",
* "edit-form" = "/admin/structure/block/block-content/manage/{block_content_type}"
* }
* )
*/
......
......@@ -89,8 +89,8 @@ function book_entity_type_build(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['node']
->setFormClass('book_outline', 'Drupal\book\Form\BookOutlineForm')
->setLinkTemplate('book-outline-form', 'entity.node.book_outline_form')
->setLinkTemplate('book-remove-form', 'entity.node.book_remove_form');
->setLinkTemplate('book-outline-form', '/node/{node}/outline')
->setLinkTemplate('book-remove-form', '/node/{node}/outline/remove');
}
/**
......
......@@ -47,9 +47,9 @@
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "entity.comment.canonical",
* "delete-form" = "entity.comment.delete_form",
* "edit-form" = "entity.comment.edit_form",
* "canonical" = "/comment/{comment}",
* "delete-form" = "/comment/{comment}/delete",
* "edit-form" = "/comment/{comment}/edit",
* },
* bundle_entity_type = "comment_type",
* field_ui_base_route = "entity.comment_type.edit_form",
......
......@@ -34,9 +34,9 @@
* "label" = "label"
* },
* links = {
* "delete-form" = "entity.comment_type.delete_form",
* "edit-form" = "entity.comment_type.edit_form",
* "add-form" = "entity.comment_type.add_form"
* "delete-form" = "/admin/structure/comment/manage/{comment_type}/delete",
* "edit-form" = "/admin/structure/comment/manage/{comment_type}",
* "add-form" = "/admin/structure/comment/types/add"
* }
* )
*/
......
......@@ -224,7 +224,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $type = '
if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', array($comment))->isAllowed()) {
$links['translate'] = array(
'title' => $this->t('Translate'),
'url' => Url::fromRoute('content_translation.translation_overview_comment', ['comment' => $comment->id()], $comment_uri_options + ['query' => $destination]),
'url' => Url::fromRoute('entity.comment.content_translation_overview', ['comment' => $comment->id()], $comment_uri_options + ['query' => $destination]),
);
}
$options[$comment->id()]['operations']['data'] = array(
......
......@@ -108,7 +108,7 @@ public function testCommentTypeEditing() {
$this->drupalGet('admin/structure/comment');
$this->assertRaw('Bar', 'New name was displayed.');
$this->clickLink('Manage fields');
$this->assertUrl(\Drupal::url('field_ui.overview_comment', array('comment_type' => 'comment'), array('absolute' => TRUE)), [], 'Original machine name was used in URL.');
$this->assertUrl(\Drupal::url('entity.comment_type.field_ui_fields', ['comment_type' => 'comment'], ['absolute' => TRUE]), [], 'Original machine name was used in URL.');
$this->assertTrue($this->cssSelect('tr#comment-body'), 'Body field exists.');
// Remove the body field.
......
......@@ -50,8 +50,8 @@ function config_test_entity_type_alter(array &$entity_types) {
// Create a clone of config_test that does not have a status.
$entity_types['config_test_no_status'] = clone $entity_types['config_test'];
$config_test_no_status = &$entity_types['config_test_no_status'];
$config_test_no_status->setLinkTemplate('edit-form', 'entity.config_test.edit_form_config_test_no_status');
$config_test_no_status->setLinkTemplate('delete-form', 'entity.config_test.delete_form_config_test_no_status');
$config_test_no_status->setLinkTemplate('edit-form', '/admin/structure/config_test/manage/{config_test_no_status}');
$config_test_no_status->setLinkTemplate('delete-form', '/admin/structure/config_test/manage/{config_test_no_status}/delete');
$keys = $config_test_no_status->getKeys();
unset($keys['status']);
......
......@@ -34,10 +34,10 @@
* "status" = "status"
* },
* links = {
* "edit-form" = "entity.config_test.edit_form",
* "delete-form" = "entity.config_test.delete_form",
* "enable" = "entity.config_test.enable",
* "disable" = "entity.config_test.disable"
* "edit-form" = "/admin/structure/config_test/manage/{config_test}",
* "delete-form" = "/admin/structure/config_test/manage/{config_test}/delete",
* "enable" = "/admin/structure/config_test/manage/{config_test}/enable",
* "disable" = "/admin/structure/config_test/manage/{config_test}/disable"
* }
* )
*/
......
......@@ -36,13 +36,13 @@ function hook_config_translation_info(&$info) {
$route_provider = \Drupal::service('router.route_provider');
// If field UI is not enabled, the base routes of the type
// "field_ui.field_edit_$entity_type" are not defined.
// "entity.field_config.{$entity_type}_field_edit_form" are not defined.
if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
// Add fields entity mappers to all fieldable entity types defined.
foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
$base_route = NULL;
try {
$base_route = $route_provider->getRouteByName('field_ui.field_edit_' . $entity_type_id);
$base_route = $route_provider->getRouteByName('entity.field_config.' . $entity_type_id . '_field_edit_form');
}
catch (RouteNotFoundException $e) {
// Ignore non-existent routes.
......@@ -51,7 +51,7 @@ function hook_config_translation_info(&$info) {
// Make sure entity type has field UI enabled and has a base route.
if ($entity_type->get('field_ui_base_route') && !empty($base_route)) {
$info[$entity_type_id . '_fields'] = array(
'base_route_name' => 'field_ui.field_edit_' . $entity_type_id,
'base_route_name' => 'entity.field_config.' . $entity_type_id . '_field_edit_form',
'entity_type' => 'field_config',
'title' => t('!label field'),
'class' => '\Drupal\config_translation\ConfigFieldMapper',
......
......@@ -8,6 +8,7 @@
use Drupal\config_translation\Plugin\Derivative\ConfigTranslationLocalTasks;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\FieldConfigInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
......@@ -83,7 +84,7 @@ function config_translation_entity_type_alter(array &$entity_types) {
elseif ($entity_type_id == 'field_config') {
$class = 'Drupal\config_translation\Controller\ConfigTranslationFieldListBuilder';
// Will be filled in dynamically, see \Drupal\field\Entity\FieldConfig::linkTemplates().
$entity_type->setLinkTemplate('drupal:config-translation-overview', 'config_translation.item.overview.');
$entity_type->setLinkTemplate('config-translation-overview', $entity_type->getLinkTemplate('edit-form') . '/translate');
}
else {
$class = 'Drupal\config_translation\Controller\ConfigTranslationEntityListBuilder';
......@@ -91,7 +92,7 @@ function config_translation_entity_type_alter(array &$entity_types) {
$entity_type->setHandlerClass('config_translation_list', $class);
if ($entity_type->hasLinkTemplate('edit-form')) {
$entity_type->setLinkTemplate('drupal:config-translation-overview', 'config_translation.item.overview.' . $entity_type->getLinkTemplate('edit-form'));
$entity_type->setLinkTemplate('config-translation-overview', $entity_type->getLinkTemplate('edit-form') . '/translate');
}
}
}
......@@ -105,14 +106,14 @@ function config_translation_config_translation_info(&$info) {
$route_provider = \Drupal::service('router.route_provider');
// If field UI is not enabled, the base routes of the type
// "field_ui.field_edit_$entity_type" are not defined.
// "entity.field_config.{$entity_type}_field_edit_form" are not defined.
if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
// Add fields entity mappers to all fieldable entity types defined.
foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
// Make sure entity type has field UI enabled and has a base route.
if ($entity_type->get('field_ui_base_route')) {
$info[$entity_type_id . '_fields'] = array(
'base_route_name' => 'field_ui.field_edit_' . $entity_type_id,
'base_route_name' => "entity.field_config.{$entity_type_id}_field_edit_form",
'entity_type' => 'field_config',
'title' => '!label field',
'class' => '\Drupal\config_translation\ConfigFieldMapper',
......@@ -139,9 +140,10 @@ function config_translation_config_translation_info(&$info) {
}
// Use the entity type as the plugin ID.
$base_route_name = "entity.$entity_type_id.edit_form";
$info[$entity_type_id] = array(
'class' => '\Drupal\config_translation\ConfigEntityMapper',
'base_route_name' => $entity_type->getLinkTemplate('edit-form'),
'base_route_name' => $base_route_name,
'title' => '!label !entity_type',
'names' => array(),
'entity_type' => $entity_type_id,
......@@ -157,12 +159,18 @@ function config_translation_entity_operation(EntityInterface $entity) {
$operations = array();
$entity_type = $entity->getEntityType();
if ($entity_type->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface') &&
$entity->hasLinkTemplate('drupal:config-translation-overview') &&
$entity->hasLinkTemplate('config-translation-overview') &&
\Drupal::currentUser()->hasPermission('translate configuration')) {
$link_template = 'config-translation-overview';
if ($entity instanceof FieldConfigInterface) {
$link_template = "config-translation-overview.{$entity->getTargetEntityTypeId()}";
}
$operations['translate'] = array(
'title' => t('Translate'),
'weight' => 50,
'url' => $entity->urlInfo('drupal:config-translation-overview'),
'url' => $entity->urlInfo($link_template),
);
}
......
......@@ -242,6 +242,13 @@ public function getContextualLinkGroup() {
}
}
/**
* {@inheritdoc}
*/
public function getOverviewRouteName() {
return 'entity.' . $this->entityType . '.config_translation_overview';
}
/**
* {@inheritdoc}
*/
......
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