Commit 6da8ebd0 authored by webchick's avatar webchick

Issue #2141329 by tim.plunkett: Replace getAdminPath() with getAdminRouteInfo() in Field UI.

parent 2b696779
......@@ -72,13 +72,6 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
*/
protected $fieldDefinitions;
/**
* Local cache for URI placeholder substitution values.
*
* @var array
*/
protected $uriPlaceholderReplacements;
/**
* Local cache for the available language objects.
*
......
......@@ -44,6 +44,13 @@ abstract class Entity implements EntityInterface {
*/
protected $routeProvider;
/**
* Local cache for URI placeholder substitution values.
*
* @var array
*/
protected $uriPlaceholderReplacements;
/**
* Constructs an Entity object.
*
......@@ -151,7 +158,7 @@ public function uri($rel = 'canonical') {
$entity_info = $this->entityInfo();
// The links array might contain URI templates set in annotations.
$link_templates = $entity_info->getLinkTemplates();
$link_templates = $this->linkTemplates();
$template = NULL;
if (isset($link_templates[$rel])) {
......@@ -213,6 +220,16 @@ public function uri($rel = 'canonical') {
return $uri;
}
/**
* Returns an array link templates.
*
* @return array
* An array of link templates containing route names.
*/
protected function linkTemplates() {
return $this->entityInfo()->getLinkTemplates();
}
/**
* Returns an array of placeholders for this entity.
*
......@@ -245,10 +262,9 @@ protected function uriPlaceholderReplacements() {
* An array of link relationships supported by this entity.
*/
public function uriRelationships() {
return array_keys($this->entityInfo()->getLinkTemplates());
return array_keys($this->linkTemplates());
}
/**
* {@inheritdoc}
*/
......
......@@ -303,29 +303,16 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $
/**
* {@inheritdoc}
*/
public function getAdminPath($entity_type, $bundle) {
$admin_path = '';
public function getAdminRouteInfo($entity_type, $bundle) {
$entity_info = $this->getDefinition($entity_type);
// Check for an entity type's admin base path.
if ($admin_form = $entity_info->getLinkTemplate('admin-form')) {
$route_parameters[$entity_info->getBundleEntityType()] = $bundle;
$admin_path = \Drupal::urlGenerator()->getPathFromRoute($admin_form, $route_parameters);
return array(
'route_name' => $admin_form,
'route_parameters' => array(
$entity_info->getBundleEntityType() => $bundle,
),
);
}
return $admin_path;
}
/**
* {@inheritdoc}
*/
public function getAdminRouteInfo($entity_type, $bundle) {
$entity_info = $this->getDefinition($entity_type);
return array(
'route_name' => "field_ui.overview_$entity_type",
'route_parameters' => array(
$entity_info->getBundleEntityType() => $bundle,
)
);
}
/**
......
......@@ -144,24 +144,6 @@ public function clearCachedDefinitions();
*/
public function getViewBuilder($entity_type);
/**
* Returns the administration path for an entity type's bundle.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The name of the bundle.
*
* @return string
* The administration path for an entity type bundle, if it exists.
*
* @deprecated since version 8.0
* System paths should not be used - use route names and parameters.
*
* @see self::getAdminRouteInfo()
*/
public function getAdminPath($entity_type, $bundle);
/**
* Creates a new list controller instance.
*
......
......@@ -13,6 +13,7 @@
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\field_ui\FieldUI;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -125,7 +126,7 @@ public function overviewBundles() {
foreach ($field_info_map['bundles'] as $bundle) {
if (isset($entity_bundles[$entity_type][$bundle])) {
// Add the current instance.
if ($field_ui_enabled && ($route_info = $this->entityManager()->getAdminRouteInfo($entity_type, $bundle))) {
if ($field_ui_enabled && $route_info = FieldUI::getOverviewRouteInfo($entity_type, $bundle)) {
$row['data']['usage']['data']['#items'][] = $this->l($entity_bundles[$entity_type][$bundle]['label'], $route_info['route_name'], $route_info['route_parameters']);
}
else {
......@@ -208,7 +209,7 @@ public function bundleInfo($commented_entity_type, $field_name) {
// Loop over all of bundles to which this comment field is attached.
foreach ($field_info->getBundles() as $bundle) {
// Add the current instance to the list of bundles.
if ($field_ui_enabled && ($route_info = $this->entityManager()->getAdminRouteInfo($commented_entity_type, $bundle))) {
if ($field_ui_enabled && $route_info = FieldUI::getOverviewRouteInfo($commented_entity_type, $bundle)) {
// Add a link to configure the fields on the given bundle and entity
// type combination.
$build['usage']['#items'][] = $this->l($entity_bundle_info[$bundle]['label'], $route_info['route_name'], $route_info['route_parameters']);
......
......@@ -507,21 +507,25 @@ public function isTranslatable() {
/**
* {@inheritdoc}
*/
public function uri() {
$path = \Drupal::entityManager()->getAdminPath($this->entity_type, $this->bundle);
// Use parent URI as fallback, if path is empty.
if (empty($path)) {
return parent::uri();
protected function linkTemplates() {
$link_templates = parent::linkTemplates();
if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
$link_templates['edit-form'] = 'field_ui.instance_edit_' . $this->entity_type;
}
return $link_templates;
}
return array(
'path' => $path . '/fields/' . $this->id(),
'options' => array(
'entity_type' => $this->entityType,
'entity' => $this,
),
);
/**
* {@inheritdoc}
*/
protected function uriPlaceholderReplacements() {
if (empty($this->uriPlaceholderReplacements)) {
parent::uriPlaceholderReplacements();
$entity_info = \Drupal::entityManager()->getDefinition($this->entity_type);
$key = '{' . $entity_info->getBundleEntityType() . '}';
$this->uriPlaceholderReplacements[$key] = $this->bundle;
}
return $this->uriPlaceholderReplacements;
}
/**
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity\EntityViewModeInterface;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Plugin\Derivative\FieldUiLocalTask;
/**
......@@ -203,8 +204,8 @@ function field_ui_entity_operation_alter(array &$operations, EntityInterface $en
* @see field_ui_form_node_type_form_alter()
*/
function field_ui_form_node_type_form_submit($form, &$form_state) {
if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
$form_state['redirect_route'] = \Drupal::entityManager()->getAdminRouteInfo('node', $form_state['values']['type']);
if ($form_state['triggering_element']['#parents'][0] === 'save_continue' && $route_info = FieldUI::getOverviewRouteInfo('node', $form_state['values']['type'])) {
$form_state['redirect_route'] = $route_info;
}
}
......
......@@ -169,8 +169,15 @@ protected function getTableHeader() {
/**
* {@inheritdoc}
*/
protected function getOverviewPath($mode) {
return $this->entityManager->getAdminPath($this->entity_type, $this->bundle) . "/display/$mode";
protected function getOverviewRoute($mode) {
return array(
'route_name' => 'field_ui.display_overview_view_mode_' . $this->entity_type,
'route_parameters' => array(
$this->bundleEntityType => $this->bundle,
'view_mode_name' => $mode,
),
'options' => array(),
);
}
/**
......
......@@ -133,8 +133,8 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
'#extra' => array_keys($extra_fields),
);
if (empty($field_definitions) && empty($extra_fields)) {
drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href="@link">Manage fields</a> page.', array('@link' => url($this->adminPath . '/fields'))), 'warning');
if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity_type, $this->bundle)) {
drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href="@link">Manage fields</a> page.', array('@link' => $this->url($route_info['route_name'], $route_info['route_parameters'], $route_info['options']))), 'warning');
return $form;
}
......@@ -566,8 +566,8 @@ public function submitForm(array &$form, array &$form_state) {
}
$display_mode_label = $display_modes[$mode]['label'];
$path = $this->getOverviewPath($mode);
drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to <a href="@url">configure them</a>.', array('%display_mode' => $display_mode_label, '@url' => url($path))));
$route = $this->getOverviewRoute($mode);
drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to <a href="@url">configure them</a>.', array('%display_mode' => $display_mode_label, '@url' => $this->url($route['route_name'], $route['route_parameters'], $route['options']))));
}
$statuses[$mode] = !empty($value);
}
......@@ -820,15 +820,21 @@ protected function saveDisplayStatuses($display_statuses) {
abstract protected function getTableHeader();
/**
* Returns the path of a specific form or view mode form.
* Returns the route info of a specific form or view mode form.
*
* @param string $mode
* The form or view mode.
*
* @return string
* An internal path.
* @return array
* An associative array with the following keys:
* - route_name: The name of the route.
* - route_parameters: (optional) An associative array of parameter names
* and values.
* - options: (optional) An associative array of additional options. See
* \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* comprehensive documentation.
*/
abstract protected function getOverviewPath($mode);
abstract protected function getOverviewRoute($mode);
/**
* Alters the widget or formatter settings form.
......
......@@ -111,8 +111,12 @@ public function buildRow(EntityInterface $field) {
$usage = array();
foreach ($field->getBundles() as $bundle) {
$admin_path = $this->entityManager->getAdminPath($field->entity_type, $bundle);
$usage[] = $admin_path ? l($this->bundles[$field->entity_type][$bundle]['label'], $admin_path . '/fields') : $this->bundles[$field->entity_type][$bundle]['label'];
if ($route_info = FieldUI::getOverviewRouteInfo($field->entity_type, $bundle)) {
$usage[] = \Drupal::l($this->bundles[$field->entity_type][$bundle]['label'], $route_info['route_name'], $route_info['route_parameters'], $route_info['options']);
}
else {
$usage[] = $this->bundles[$field->entity_type][$bundle]['label'];
}
}
$row['data']['usage'] = implode(', ', $usage);
return $row;
......
......@@ -122,7 +122,10 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
// Fields.
foreach ($instances as $name => $instance) {
$field = $instance->getField();
$admin_field_path = $this->adminPath . '/fields/' . $instance->id();
$route_parameters = array(
$this->bundleEntityType => $this->bundle,
'field_instance' => $instance->id(),
);
$table[$name] = array(
'#attributes' => array(
'id' => drupal_html_class($name),
......@@ -136,7 +139,8 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
'type' => array(
'#type' => 'link',
'#title' => $field_types[$field->getType()]['label'],
'#href' => $admin_field_path . '/field',
'#route_name' => 'field_ui.field_edit_' . $this->entity_type,
'#route_parameters' => $route_parameters,
'#options' => array('attributes' => array('title' => $this->t('Edit field settings.'))),
),
);
......@@ -144,17 +148,20 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
$links = array();
$links['edit'] = array(
'title' => $this->t('Edit'),
'href' => $admin_field_path,
'route_name' => 'field_ui.instance_edit_' . $this->entity_type,
'route_parameters' => $route_parameters,
'attributes' => array('title' => $this->t('Edit instance settings.')),
);
$links['field-settings'] = array(
'title' => $this->t('Field settings'),
'href' => $admin_field_path . '/field',
'route_name' => 'field_ui.field_edit_' . $this->entity_type,
'route_parameters' => $route_parameters,
'attributes' => array('title' => $this->t('Edit field settings.')),
);
$links['delete'] = array(
'title' => $this->t('Delete'),
'href' => "$admin_field_path/delete",
'route_name' => 'field_ui.delete_' . $this->entity_type,
'route_parameters' => $route_parameters,
'attributes' => array('title' => $this->t('Delete instance.')),
);
// Allow altering the operations on this entity listing.
......@@ -419,8 +426,12 @@ public function submitForm(array &$form, array &$form_state) {
// Always show the field settings step, as the cardinality needs to be
// configured for new fields.
$destinations[] = $this->adminPath. '/fields/' . $new_instance->id() . '/field';
$destinations[] = $this->adminPath . '/fields/' . $new_instance->id();
$route_parameters = array(
$this->bundleEntityType => $this->bundle,
'field_instance' => $new_instance->id(),
);
$destinations[] = array('route_name' => 'field_ui.field_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
$destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
// Store new field information for any additional submit handlers.
$form_state['fields_added']['_add_new_field'] = $values['field_name'];
......@@ -464,7 +475,13 @@ public function submitForm(array &$form, array &$form_state) {
->setComponent($field_name)
->save();
$destinations[] = $this->adminPath . '/fields/' . $new_instance->id();
$destinations[] = array(
'route_name' => 'field_ui.instance_edit_' . $this->entity_type,
'route_parameters' => array(
$this->bundleEntityType => $this->bundle,
'field_instance' => $new_instance->id(),
),
);
// Store new field information for any additional submit handlers.
$form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
}
......@@ -477,11 +494,7 @@ public function submitForm(array &$form, array &$form_state) {
if ($destinations) {
$destination = drupal_get_destination();
$destinations[] = $destination['destination'];
$this->getRequest()->query->remove('destination');
$path = array_shift($destinations);
$options = drupal_parse_url($path);
$options['query']['destinations'] = $destinations;
$form_state['redirect'] = array($options['path'], $options);
$form_state['redirect_route'] = FieldUI::getNextDestination($destinations, $form_state);
}
else {
drupal_set_message($this->t('Your settings have been saved.'));
......
......@@ -7,26 +7,59 @@
namespace Drupal\field_ui;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Component\Utility\Url;
/**
* Static service container wrapper for Field UI.
*/
class FieldUI {
/**
* Returns the route info for the field overview of a given entity bundle.
*
* @param string $entity_type
* An entity type.
* @param string $bundle
* The entity bundle.
*
* @return array
* An associative array with the following keys:
* - route_name: The name of the route.
* - route_parameters: (optional) An associative array of parameter names
* and values.
* - options: (optional) An associative array of additional options. See
* \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* comprehensive documentation.
*/
public static function getOverviewRouteInfo($entity_type, $bundle) {
$entity_info = \Drupal::entityManager()->getDefinition($entity_type);
if ($entity_info->hasLinkTemplate('admin-form')) {
return array(
'route_name' => "field_ui.overview_$entity_type",
'route_parameters' => array(
$entity_info->getBundleEntityType() => $bundle,
),
'options' => array(),
);
}
}
/**
* Returns the next redirect path in a multipage sequence.
*
* @param array $destinations
* An array of destinations to redirect to.
*
* @return array
* An array of redirect paths.
* The next destination to redirect to.
*/
public static function getNextDestination(Request $request) {
$next_destination = array();
$destinations = $request->query->get('destinations');
if (!empty($destinations)) {
$request->query->remove('destinations');
$path = array_shift($destinations);
$options = drupal_parse_url($path);
public static function getNextDestination(array $destinations) {
$next_destination = array_shift($destinations);
if (is_array($next_destination)) {
$next_destination['options']['query']['destinations'] = $destinations;
}
else {
$options = Url::parse($next_destination);
if ($destinations) {
$options['query']['destinations'] = $destinations;
}
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -60,7 +61,7 @@ public function getConfirmText() {
* {@inheritdoc}
*/
public function getCancelRoute() {
return $this->entityManager->getAdminRouteInfo($this->entity->entity_type, $this->entity->bundle);
return FieldUI::getOverviewRouteInfo($this->entity->entity_type, $this->entity->bundle);
}
/**
......@@ -79,7 +80,7 @@ public function submit(array $form, array &$form_state) {
drupal_set_message($this->t('There was a problem removing the %field from the %type content type.', array('%field' => $this->entity->label(), '%type' => $bundle_label)), 'error');
}
$form_state['redirect_route'] = $this->entityManager->getAdminRouteInfo($this->entity->entity_type, $this->entity->bundle);
$form_state['redirect_route'] = FieldUI::getOverviewRouteInfo($this->entity->entity_type, $this->entity->bundle);
// Fields are purged on cron. However field module prevents disabling modules
// when field types they provided are used in a field until it is fully
......
......@@ -202,11 +202,18 @@ public function submitForm(array &$form, array &$form_state) {
try {
$field->save();
drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->instance->label())));
if ($next_destination = FieldUI::getNextDestination($this->getRequest())) {
$form_state['redirect'] = $next_destination;
$request = $this->getRequest();
if (($destinations = $request->query->get('destinations')) && $next_destination = FieldUI::getNextDestination($destinations)) {
$request->query->remove('destinations');
if (isset($next_destination['route_name'])) {
$form_state['redirect_route'] = $next_destination;
}
else {
$form_state['redirect'] = $next_destination;
}
}
else {
$form_state['redirect_route'] = $this->entityManager->getAdminRouteInfo($this->instance->entity_type, $this->instance->bundle);
$form_state['redirect_route'] = FieldUI::getOverviewRouteInfo($this->instance->entity_type, $this->instance->bundle);
}
}
catch (\Exception $e) {
......
......@@ -190,11 +190,18 @@ public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->instance->getLabel())));
if ($next_destination = FieldUI::getNextDestination($this->getRequest())) {
$form_state['redirect'] = $next_destination;
$request = $this->getRequest();
if (($destinations = $request->query->get('destinations')) && $next_destination = FieldUI::getNextDestination($destinations)) {
$request->query->remove('destinations');
if (isset($next_destination['route_name'])) {
$form_state['redirect_route'] = $next_destination;
}
else {
$form_state['redirect'] = $next_destination;
}
}
else {
$form_state['redirect_route'] = $this->entityManager->getAdminRouteInfo($this->instance->entity_type, $this->instance->bundle);
$form_state['redirect_route'] = FieldUI::getOverviewRouteInfo($this->instance->entity_type, $this->instance->bundle);
}
}
......
......@@ -135,8 +135,15 @@ protected function getTableHeader() {
/**
* {@inheritdoc}
*/
protected function getOverviewPath($mode) {
return $this->entityManager->getAdminPath($this->entity_type, $this->bundle) . "/form-display/$mode";
protected function getOverviewRoute($mode) {
return array(
'route_name' => 'field_ui.form_display_overview_form_mode_' . $this->entity_type,
'route_parameters' => array(
$this->bundleEntityType => $this->bundle,
'form_mode_name' => $mode,
),
'options' => array(),
);
}
/**
......
......@@ -31,18 +31,18 @@ abstract class OverviewBase extends FormBase {
protected $bundle = '';
/**
* The entity view or form mode.
* The entity type of the entity bundle.
*
* @var string
*/
protected $mode = '';
protected $bundleEntityType;
/**
* The admin path of the overview page.
* The entity view or form mode.
*
* @var string
*/
protected $adminPath = NULL;
protected $mode = '';
/**
* The entity manager.
......@@ -74,17 +74,17 @@ public static function create(ContainerInterface $container) {
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $entity_type = NULL, $bundle = NULL) {
$entity_info = $this->entityManager->getDefinition($entity_type);
$this->bundleEntityType = $entity_info->getBundleEntityType();
if (!isset($form_state['bundle'])) {
if (!$bundle) {
$entity_info = $this->entityManager->getDefinition($entity_type);
$bundle = $this->getRequest()->attributes->get('_raw_variables')->get($entity_info->getBundleEntityType());
$bundle = $this->getRequest()->attributes->get('_raw_variables')->get($this->bundleEntityType);
}
$form_state['bundle'] = $bundle;
}
$this->entity_type = $entity_type;
$this->bundle = $form_state['bundle'];
$this->adminPath = $this->entityManager->getAdminPath($this->entity_type, $this->bundle);
// When displaying the form, make sure the list of fields is up-to-date.
if (empty($form_state['post'])) {
......
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