Commit 24e3c5d1 authored by alexpott's avatar alexpott

Issue #2083615 by Gábor Hojtsy, YesCT: Use links annotation for config entity...

Issue #2083615 by Gábor Hojtsy, YesCT: Use links annotation for config entity URI like for content entities.
parent d75787df
......@@ -188,4 +188,11 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
}
}
/**
* {@inheritdoc}
*/
public function uri() {
return parent::uri('edit-form');
}
}
......@@ -148,36 +148,109 @@ public function label($langcode = NULL) {
}
/**
* Implements \Drupal\Core\Entity\EntityInterface::uri().
* Returns the URI elements of the entity.
*
* URI templates might be set in the links array in an annotation, for
* example:
* @code
* links = {
* "canonical" = "/node/{node}",
* "edit-form" = "/node/{node}/edit",
* "version-history" = "/node/{node}/revisions"
* }
* @endcode
* or specified in a callback function set like:
* @code
* uri_callback = "contact_category_uri",
* @endcode
* If looking for the canonical URI, and it was not set in the links array
* or in a uri_callback function, the path is set using the default template:
* entity/entityType/id.
*
* @param string $rel
* The link relationship type, for example: canonical or edit-form.
*
* @return array
* An array containing the 'path' and 'options' keys used to build the URI
* of the entity, and matching the signature of url().
*/
public function uri() {
$bundle = $this->bundle();
// A bundle-specific callback takes precedence over the generic one for the
// entity type.
public function uri($rel = 'canonical') {
$entity_info = $this->entityInfo();
$bundles = entity_get_bundles($this->entityType);
if (isset($bundles[$bundle]['uri_callback'])) {
$uri_callback = $bundles[$bundle]['uri_callback'];
}
elseif (isset($entity_info['uri_callback'])) {
$uri_callback = $entity_info['uri_callback'];
// The links array might contain URI templates set in annotations.
$link_templates = isset($entity_info['links']) ? $entity_info['links'] : array();
if (isset($link_templates[$rel])) {
// If there is a template for the given relationship type, do the
// placeholder replacement and use that as the path.
$template = $link_templates[$rel];
$replacements = $this->uriPlaceholderReplacements();
$uri['path'] = str_replace(array_keys($replacements), array_values($replacements), $template);
// @todo Remove this once http://drupal.org/node/1888424 is in and we can
// move the BC handling of / vs. no-/ to the generator.
$uri['path'] = trim($uri['path'], '/');
// Pass the entity data to url() so that alter functions do not need to
// look up this entity again.
$uri['options']['entity_type'] = $this->entityType;
$uri['options']['entity'] = $this;
return $uri;
}
// Invoke the callback to get the URI. If there is no callback, use the
// default URI format.
if (isset($uri_callback) && function_exists($uri_callback)) {
$uri = $uri_callback($this);
// Only use these defaults for a canonical link (that is, a link to self).
// Other relationship types are not supported by this logic.
if ($rel == 'canonical') {
$bundle = $this->bundle();
// A bundle-specific callback takes precedence over the generic one for
// the entity type.
$bundles = entity_get_bundles($this->entityType);
if (isset($bundles[$bundle]['uri_callback'])) {
$uri_callback = $bundles[$bundle]['uri_callback'];
}
elseif (isset($entity_info['uri_callback'])) {
$uri_callback = $entity_info['uri_callback'];
}
// Invoke the callback to get the URI. If there is no callback, use the
// default URI format.
if (isset($uri_callback) && function_exists($uri_callback)) {
$uri = $uri_callback($this);
}
else {
$uri = array(
'path' => 'entity/' . $this->entityType . '/' . $this->id(),
);
}
// Pass the entity data to url() so that alter functions do not need to
// look up this entity again.
$uri['options']['entity_type'] = $this->entityType;
$uri['options']['entity'] = $this;
return $uri;
}
else {
$uri = array(
'path' => 'entity/' . $this->entityType . '/' . $this->id(),
}
/**
* Returns an array of placeholders for this entity.
*
* Individual entity classes may override this method to add additional
* placeholders if desired. If so, they should be sure to replicate the
* property caching logic.
*
* @return array
* An array of URI placeholders.
*/
protected function uriPlaceholderReplacements() {
if (empty($this->uriPlaceholderReplacements)) {
$this->uriPlaceholderReplacements = array(
'{entityType}' => $this->entityType(),
'{bundle}' => $this->bundle(),
'{id}' => $this->id(),
'{uuid}' => $this->uuid(),
'{' . $this->entityType() . '}' => $this->id(),
);
}
// Pass the entity data to url() so that alter functions do not need to
// look up this entity again.
$uri['options']['entity_type'] = $this->entityType;
$uri['options']['entity'] = $this;
return $uri;
return $this->uriPlaceholderReplacements;
}
/**
......
......@@ -207,61 +207,6 @@ public function uuid() {
return $this->get('uuid')->value;
}
/**
* {@inheritdoc}
*/
public function uri($rel = 'canonical') {
$entity_info = $this->entityInfo();
$link_templates = isset($entity_info['links']) ? $entity_info['links'] : array();
if (isset($link_templates[$rel])) {
$template = $link_templates[$rel];
$replacements = $this->uriPlaceholderReplacements();
$uri['path'] = str_replace(array_keys($replacements), array_values($replacements), $template);
// @todo Remove this once http://drupal.org/node/1888424 is in and we can
// move the BC handling of / vs. no-/ to the generator.
$uri['path'] = trim($uri['path'], '/');
// Pass the entity data to url() so that alter functions do not need to
// look up this entity again.
$uri['options']['entity_type'] = $this->entityType;
$uri['options']['entity'] = $this;
return $uri;
}
// For a canonical link (that is, a link to self), look up the stack for
// default logic. Other relationship types are not supported by parent
// classes.
if ($rel == 'canonical') {
return parent::uri();
}
}
/**
* Returns an array of placeholders for this entity.
*
* Individual entity classes may override this method to add additional
* placeholders if desired. If so, they should be sure to replicate the
* property caching logic.
*
* @return array
* An array of URI placeholders.
*/
protected function uriPlaceholderReplacements() {
if (empty($this->uriPlaceholderReplacements)) {
$this->uriPlaceholderReplacements = array(
'{entityType}' => $this->entityType(),
'{bundle}' => $this->bundle(),
'{id}' => $this->id(),
'{uuid}' => $this->uuid(),
'{' . $this->entityType() . '}' => $this->id(),
);
}
return $this->uriPlaceholderReplacements;
}
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::get().
*/
......
......@@ -37,6 +37,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/block/custom-blocks/manage/{custom_block_type}"
* }
* )
*/
......@@ -77,19 +80,6 @@ class CustomBlockType extends ConfigEntityBase implements CustomBlockTypeInterfa
*/
public $description;
/**
* Overrides \Drupal\Core\Entity\Entity::uri().
*/
public function uri() {
return array(
'path' => 'admin/structure/block/custom-blocks/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
)
);
}
/**
* {@inheritdoc}
*/
......
......@@ -62,7 +62,7 @@ function testCRUD() {
$this->assertIdentical($empty->isNewRevision(), FALSE);
$this->assertIdentical($empty->entityType(), 'config_test');
$uri = $empty->uri();
$this->assertIdentical($uri['path'], 'admin/structure/config_test/manage/');
$this->assertIdentical($uri['path'], 'admin/structure/config_test/manage');
$this->assertIdentical($empty->isDefaultRevision(), TRUE);
// Verify that an empty entity cannot be saved.
......
......@@ -10,18 +10,6 @@
require_once dirname(__FILE__) . '/config_test.hooks.inc';
/**
* Entity URI callback.
*
* @param Drupal\config_test\Entity\ConfigTest $config_test
* A ConfigTest entity.
*/
function config_test_uri(ConfigTest $config_test) {
return array(
'path' => 'admin/structure/config_test/manage/' . $config_test->id(),
);
}
/**
* Implements hook_menu().
*/
......
......@@ -24,12 +24,14 @@
* "default" = "Drupal\config_test\ConfigTestFormController"
* }
* },
* uri_callback = "config_test_uri",
* config_prefix = "config_query_test.dynamic",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/config_test/manage/{config_query_test}"
* }
* )
*
......
......@@ -28,13 +28,15 @@
* },
* "access" = "Drupal\config_test\ConfigTestAccessController"
* },
* uri_callback = "config_test_uri",
* config_prefix = "config_test.dynamic",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* "status" = "status"
* },
* links = {
* "edit-form" = "admin/structure/config_test/manage/{config_test}"
* }
* )
*/
......
......@@ -234,21 +234,6 @@ function contact_category_load($id) {
return entity_load('contact_category', $id);
}
/**
* Entity URI callback.
*
* @param Drupal\contact\Entity\Category $category
* A contact category entity.
*
* @return array
* An array with 'path' as the key and the path to the category as the value.
*/
function contact_category_uri(Category $category) {
return array(
'path' => 'admin/structure/contact/manage/' . $category->id(),
);
}
/**
* Implements hook_mail().
*/
......
......@@ -30,13 +30,15 @@
* "delete" = "Drupal\contact\Form\CategoryDeleteForm"
* }
* },
* uri_callback = "contact_category_uri",
* config_prefix = "contact.category",
* bundle_of = "contact_message",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/contact/manage/{contact_category}"
* }
* )
*/
......
......@@ -48,22 +48,12 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/display-modes/form/manage/{form_mode}"
* }
* )
*/
class EntityFormMode extends EntityDisplayModeBase implements EntityFormModeInterface {
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/structure/display-modes/form/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
}
......@@ -49,22 +49,12 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/display-modes/view/manage/{view_mode}"
* }
* )
*/
class EntityViewMode extends EntityDisplayModeBase implements EntityViewModeInterface {
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/structure/display-modes/view/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
}
......@@ -38,6 +38,9 @@
* "uuid" = "uuid",
* "weight" = "weight",
* "status" = "status"
* },
* links = {
* "edit-form" = "admin/config/content/formats/manage/{filter_format}"
* }
* )
*/
......@@ -188,19 +191,6 @@ public function disable() {
return $this;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/config/content/formats/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -77,15 +77,6 @@ function image_help($path, $arg) {
}
}
/**
* Entity URI callback for image style.
*/
function image_style_entity_uri(ImageStyle $style) {
return array(
'path' => 'admin/config/media/image-styles/manage/' . $style->id(),
);
}
/**
* Implements hook_menu().
*/
......
......@@ -35,12 +35,14 @@
* "list" = "Drupal\image\ImageStyleListController",
* "access" = "Drupal\image\ImageStyleAccessController"
* },
* uri_callback = "image_style_entity_uri",
* config_prefix = "image.style",
* entity_keys = {
* "id" = "name",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/media/image-styles/manage/{image_style}"
* }
* )
*/
......
......@@ -37,6 +37,9 @@
* "label" = "label",
* "weight" = "weight",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/regional/language/edit/{language_entity}"
* }
* )
*/
......@@ -97,17 +100,4 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
$this->langcode = 'en';
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/config/regional/language/edit/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
}
......@@ -36,6 +36,9 @@
* "id" = "type",
* "label" = "name",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/types/manage/{node_type}"
* }
* )
*/
......@@ -136,19 +139,6 @@ public function id() {
return $this->type;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/structure/types/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -36,6 +36,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/media/picturemapping/{picture_mapping}"
* }
* )
*/
......@@ -162,17 +165,4 @@ public function hasMappings() {
}
return $mapping_found;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/config/media/picturemapping/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
}
......@@ -37,6 +37,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/user-interface/shortcut/manage/{shortcut_set}"
* }
* )
*/
......@@ -70,19 +73,6 @@ class ShortcutSet extends ConfigEntityBase implements ShortcutSetInterface {
*/
public $links = array();
/**
* Overrides \Drupal\Core\Entity\Entity::uri().
*/
public function uri() {
return array(
'path' => 'admin/config/user-interface/shortcut/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -31,6 +31,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/system/actions/configure/{action}"
* }
* )
*/
......@@ -137,19 +140,6 @@ public function getType() {
return $this->type;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/config/system/actions/configure/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -36,6 +36,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/regional/date-time/formats/manage/{date_format}"
* }
* )
*/
......@@ -81,19 +84,6 @@ class DateFormat extends ConfigEntityBase implements DateFormatInterface {
*/
protected $locales = array();
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/config/regional/date-time/formats/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -28,6 +28,9 @@
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/menu/manage/{menu}"
* }
* )
*/
......
......@@ -37,6 +37,9 @@
* "label" = "name",
* "weight" = "weight",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/taxonomy/manage/{taxonomy_vocabulary}"
* }
* )
*/
......@@ -96,19 +99,6 @@ public function id() {
return $this->vid;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/structure/taxonomy/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -35,6 +35,9 @@
* "uuid" = "uuid",
* "weight" = "weight",
* "label" = "label"
* },
* links = {
* "edit-form" = "admin/people/roles/manage/{user_role}"
* }
* )
*/
......@@ -75,19 +78,6 @@ class Role extends ConfigEntityBase implements RoleInterface {
*/
public $permissions = array();
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'admin/people/roles/manage/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
}
/**
* {@inheritdoc}
*/
......
......@@ -33,6 +33,9 @@
* "label" = "label",
* "uuid" = "uuid",
* "status" = "status"
* },
* links = {
* "edit-form" = "admin/structure/views/view/{view}"
* }
* )
*/
......@@ -134,19 +137,6 @@ public function getExecutable() {
return $this->executable;
}
/**
* Overrides Drupal\Core\Entity\EntityInterface::uri().