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) { ...@@ -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) { ...@@ -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() { public function uri($rel = 'canonical') {
$bundle = $this->bundle();
// A bundle-specific callback takes precedence over the generic one for the
// entity type.
$entity_info = $this->entityInfo(); $entity_info = $this->entityInfo();
$bundles = entity_get_bundles($this->entityType);
if (isset($bundles[$bundle]['uri_callback'])) { // The links array might contain URI templates set in annotations.
$uri_callback = $bundles[$bundle]['uri_callback']; $link_templates = isset($entity_info['links']) ? $entity_info['links'] : array();
}
elseif (isset($entity_info['uri_callback'])) { if (isset($link_templates[$rel])) {
$uri_callback = $entity_info['uri_callback']; // 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 // Only use these defaults for a canonical link (that is, a link to self).
// default URI format. // Other relationship types are not supported by this logic.
if (isset($uri_callback) && function_exists($uri_callback)) { if ($rel == 'canonical') {
$uri = $uri_callback($this); $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 return $this->uriPlaceholderReplacements;
// look up this entity again.
$uri['options']['entity_type'] = $this->entityType;
$uri['options']['entity'] = $this;
return $uri;
} }
/** /**
......
...@@ -207,61 +207,6 @@ public function uuid() { ...@@ -207,61 +207,6 @@ public function uuid() {
return $this->get('uuid')->value; 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(). * Implements \Drupal\Core\TypedData\ComplexDataInterface::get().
*/ */
......
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/block/custom-blocks/manage/{custom_block_type}"
* } * }
* ) * )
*/ */
...@@ -77,19 +80,6 @@ class CustomBlockType extends ConfigEntityBase implements CustomBlockTypeInterfa ...@@ -77,19 +80,6 @@ class CustomBlockType extends ConfigEntityBase implements CustomBlockTypeInterfa
*/ */
public $description; 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} * {@inheritdoc}
*/ */
......
...@@ -62,7 +62,7 @@ function testCRUD() { ...@@ -62,7 +62,7 @@ function testCRUD() {
$this->assertIdentical($empty->isNewRevision(), FALSE); $this->assertIdentical($empty->isNewRevision(), FALSE);
$this->assertIdentical($empty->entityType(), 'config_test'); $this->assertIdentical($empty->entityType(), 'config_test');
$uri = $empty->uri(); $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); $this->assertIdentical($empty->isDefaultRevision(), TRUE);
// Verify that an empty entity cannot be saved. // Verify that an empty entity cannot be saved.
......
...@@ -10,18 +10,6 @@ ...@@ -10,18 +10,6 @@
require_once dirname(__FILE__) . '/config_test.hooks.inc'; 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(). * Implements hook_menu().
*/ */
......
...@@ -24,12 +24,14 @@ ...@@ -24,12 +24,14 @@
* "default" = "Drupal\config_test\ConfigTestFormController" * "default" = "Drupal\config_test\ConfigTestFormController"
* } * }
* }, * },
* uri_callback = "config_test_uri",
* config_prefix = "config_query_test.dynamic", * config_prefix = "config_query_test.dynamic",
* entity_keys = { * entity_keys = {
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/config_test/manage/{config_query_test}"
* } * }
* ) * )
* *
......
...@@ -28,13 +28,15 @@ ...@@ -28,13 +28,15 @@
* }, * },
* "access" = "Drupal\config_test\ConfigTestAccessController" * "access" = "Drupal\config_test\ConfigTestAccessController"
* }, * },
* uri_callback = "config_test_uri",
* config_prefix = "config_test.dynamic", * config_prefix = "config_test.dynamic",
* entity_keys = { * entity_keys = {
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid", * "uuid" = "uuid",
* "status" = "status" * "status" = "status"
* },
* links = {
* "edit-form" = "admin/structure/config_test/manage/{config_test}"
* } * }
* ) * )
*/ */
......
...@@ -234,21 +234,6 @@ function contact_category_load($id) { ...@@ -234,21 +234,6 @@ function contact_category_load($id) {
return entity_load('contact_category', $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(). * Implements hook_mail().
*/ */
......
...@@ -30,13 +30,15 @@ ...@@ -30,13 +30,15 @@
* "delete" = "Drupal\contact\Form\CategoryDeleteForm" * "delete" = "Drupal\contact\Form\CategoryDeleteForm"
* } * }
* }, * },
* uri_callback = "contact_category_uri",
* config_prefix = "contact.category", * config_prefix = "contact.category",
* bundle_of = "contact_message", * bundle_of = "contact_message",
* entity_keys = { * entity_keys = {
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/contact/manage/{contact_category}"
* } * }
* ) * )
*/ */
......
...@@ -48,22 +48,12 @@ ...@@ -48,22 +48,12 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/display-modes/form/manage/{form_mode}"
* } * }
* ) * )
*/ */
class EntityFormMode extends EntityDisplayModeBase implements EntityFormModeInterface { 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 @@ ...@@ -49,22 +49,12 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/display-modes/view/manage/{view_mode}"
* } * }
* ) * )
*/ */
class EntityViewMode extends EntityDisplayModeBase implements EntityViewModeInterface { 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 @@ ...@@ -38,6 +38,9 @@
* "uuid" = "uuid", * "uuid" = "uuid",
* "weight" = "weight", * "weight" = "weight",
* "status" = "status" * "status" = "status"
* },
* links = {
* "edit-form" = "admin/config/content/formats/manage/{filter_format}"
* } * }
* ) * )
*/ */
...@@ -188,19 +191,6 @@ public function disable() { ...@@ -188,19 +191,6 @@ public function disable() {
return $this; 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} * {@inheritdoc}
*/ */
......
...@@ -77,15 +77,6 @@ function image_help($path, $arg) { ...@@ -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(). * Implements hook_menu().
*/ */
......
...@@ -35,12 +35,14 @@ ...@@ -35,12 +35,14 @@
* "list" = "Drupal\image\ImageStyleListController", * "list" = "Drupal\image\ImageStyleListController",
* "access" = "Drupal\image\ImageStyleAccessController" * "access" = "Drupal\image\ImageStyleAccessController"
* }, * },
* uri_callback = "image_style_entity_uri",
* config_prefix = "image.style", * config_prefix = "image.style",
* entity_keys = { * entity_keys = {
* "id" = "name", * "id" = "name",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/media/image-styles/manage/{image_style}"
* } * }
* ) * )
*/ */
......
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
* "label" = "label", * "label" = "label",
* "weight" = "weight", * "weight" = "weight",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/regional/language/edit/{language_entity}"
* } * }
* ) * )
*/ */
...@@ -97,17 +100,4 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { ...@@ -97,17 +100,4 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
$this->langcode = 'en'; $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 @@ ...@@ -36,6 +36,9 @@
* "id" = "type", * "id" = "type",
* "label" = "name", * "label" = "name",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/structure/types/manage/{node_type}"
* } * }
* ) * )
*/ */
...@@ -136,19 +139,6 @@ public function id() { ...@@ -136,19 +139,6 @@ public function id() {
return $this->type; 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} * {@inheritdoc}
*/ */
......
...@@ -36,6 +36,9 @@ ...@@ -36,6 +36,9 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/media/picturemapping/{picture_mapping}"
* } * }
* ) * )
*/ */
...@@ -162,17 +165,4 @@ public function hasMappings() { ...@@ -162,17 +165,4 @@ public function hasMappings() {
} }
return $mapping_found; 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 @@ ...@@ -37,6 +37,9 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {
* "edit-form" = "admin/config/user-interface/shortcut/manage/{shortcut_set}"
* } * }
* ) * )
*/ */
...@@ -70,19 +73,6 @@ class ShortcutSet extends ConfigEntityBase implements ShortcutSetInterface { ...@@ -70,19 +73,6 @@ class ShortcutSet extends ConfigEntityBase implements ShortcutSetInterface {
*/ */
public $links = array(); 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} * {@inheritdoc}
*/ */
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
* "id" = "id", * "id" = "id",
* "label" = "label", * "label" = "label",
* "uuid" = "uuid" * "uuid" = "uuid"
* },
* links = {