Commit 93e77491 authored by alexpott's avatar alexpott

Issue #1987802 by disasm, pfrenssen, laurentchardin, tim.plunkett, fgm,...

Issue #1987802 by disasm, pfrenssen, laurentchardin, tim.plunkett, fgm, vijaycs85, plopesc, Xano, dawehner, h3rj4n: Convert path_admin_overview() to a new style controller.
parent 8a567823
......@@ -7,42 +7,125 @@
namespace Drupal\path\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Path\AliasStorageInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller routines for path routes.
*/
class PathController {
class PathController extends ControllerBase {
/**
* @todo Remove path_admin_overview().
* The path alias storage.
*
* @var \Drupal\Core\Path\AliasStorageInterface
*/
public function adminOverview() {
module_load_include('admin.inc', 'path');
return path_admin_overview();
}
protected $aliasStorage;
/**
* @todo Remove path_admin_overview().
* The path alias manager.
*
* @var \Drupal\Core\Path\AliasManagerInterface
*/
public function adminOverviewFiltered($keys) {
module_load_include('admin.inc', 'path');
return path_admin_overview($keys);
}
protected $aliasManager;
/**
* @todo Remove path_admin_edit().
* Constructs a new PathController.
*
* @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
* The path alias storage.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The path alias manager.
*/
public function adminEdit($path) {
$path = \Drupal::service('path.alias_storage')->load(array('pid' => $path));
module_load_include('admin.inc', 'path');
return path_admin_edit($path);
public function __construct(AliasStorageInterface $alias_storage, AliasManagerInterface $alias_manager) {
$this->aliasStorage = $alias_storage;
$this->aliasManager = $alias_manager;
}
/**
* @todo Remove path_admin_edit().
* {@inheritdoc}
*/
public function adminAdd() {
module_load_include('admin.inc', 'path');
return path_admin_edit();
public static function create(ContainerInterface $container) {
return new static(
$container->get('path.alias_storage'),
$container->get('path.alias_manager')
);
}
public function adminOverview($keys) {
// Add the filter form above the overview table.
$build['path_admin_filter_form'] = $this->formBuilder()->getForm('Drupal\path\Form\PathFilterForm', $keys);
// Enable language column if language.module is enabled or if we have any
// alias with a language.
$multilanguage = ($this->moduleHandler()->moduleExists('language') || $this->aliasStorage->languageAliasExists());
$header = array();
$header[] = array('data' => $this->t('Alias'), 'field' => 'alias', 'sort' => 'asc');
$header[] = array('data' => $this->t('System'), 'field' => 'source');
if ($multilanguage) {
$header[] = array('data' => $this->t('Language'), 'field' => 'langcode');
}
$header[] = $this->t('Operations');
$rows = array();
$destination = drupal_get_destination();
foreach ($this->aliasStorage->getAliasesForAdminListing($header, $keys) as $data) {
$row = array();
$row['data']['alias'] = l(truncate_utf8($data->alias, 50, FALSE, TRUE), $data->source, array(
'attributes' => array('title' => $data->alias),
));
$row['data']['source'] = l(truncate_utf8($data->source, 50, FALSE, TRUE), $data->source, array(
'alias' => TRUE,
'attributes' => array('title' => $data->source),
));
if ($multilanguage) {
$row['data']['language_name'] = $this->languageManager()->getLanguageName($data->langcode);
}
$operations = array();
$operations['edit'] = array(
'title' => $this->t('Edit'),
'route_name' => 'path.admin_edit',
'route_parameters' => array(
'pid' => $data->pid,
),
'query' => $destination,
);
$operations['delete'] = array(
'title' => $this->t('Delete'),
'route_name' => 'path.delete',
'route_parameters' => array(
'pid' => $data->pid,
),
'query' => $destination,
);
$row['data']['operations'] = array(
'data' => array(
'#type' => 'operations',
'#links' => $operations,
),
);
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
if ($data->alias != $this->aliasManager->getPathAlias($data->source, $data->langcode)) {
$row['class'] = array('warning');
}
$rows[] = $row;
}
$build['path_table'] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No URL aliases available. <a href="@link">Add URL alias</a>.', array('@link' => $this->url('path.admin_add'))),
);
$build['path_pager'] = array('#theme' => 'pager');
return $build;
}
}
<?php
/**
* @file
* Contains \Drupal\path\Form\AddForm.
*/
namespace Drupal\path\Form;
use Drupal\Core\Language\Language;
/**
* Provides the path add form.
*/
class AddForm extends PathFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'path_admin_add';
}
/**
* {@inheritdoc}
*/
protected function buildPath($pid) {
return array(
'source' => '',
'alias' => '',
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'pid' => NULL,
);
}
}
<?php
/**
* @file
* Contains \Drupal\path\Form\EditForm.
*/
namespace Drupal\path\Form;
use Drupal\Component\Utility\String;
use Drupal\Core\Url;
/**
* Provides the path edit form.
*/
class EditForm extends PathFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'path_admin_edit';
}
/**
* {@inheritdoc}
*/
protected function buildPath($pid) {
return $this->aliasStorage->load(array('pid' => $pid));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $pid = NULL) {
$form = parent::buildForm($form, $form_state, $pid);
$form['#title'] = String::checkPlain($this->path['alias']);
$form['pid'] = array(
'#type' => 'hidden',
'#value' => $this->path['pid'],
);
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => $this->t('Delete'),
'#submit' => array(array($this, 'deleteSubmit')),
);
return $form;
}
/**
* Submits the delete form.
*/
public function deleteSubmit(array &$form, array &$form_state) {
$form_state['redirect_route'] = new Url('path.delete', array(
'pid' => $form_state['values']['pid'],
));
if ($this->getRequest()->query->has('destination')) {
$form_state['redirect_route']->setOption('query', drupal_get_destination());
$this->getRequest()->query->remove('destination');
}
}
}
<?php
/**
* @file
* Contains \Drupal\path\Form\PathFilterForm.
*/
namespace Drupal\path\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
/**
* Provides the path admin overview filter form.
*/
class PathFilterForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'path_admin_filter_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $keys = NULL) {
$form['#attributes'] = array('class' => array('search-form'));
$form['basic'] = array(
'#type' => 'details',
'#title' => $this->t('Filter aliases'),
'#open' => TRUE,
'#attributes' => array('class' => array('container-inline')),
);
$form['basic']['filter'] = array(
'#type' => 'search',
'#title' => 'Path alias',
'#title_display' => 'invisible',
'#default_value' => $keys,
'#maxlength' => 128,
'#size' => 25,
);
$form['basic']['submit'] = array(
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this->t('Filter'),
);
if ($keys) {
$form['basic']['reset'] = array(
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#submit' => array(array($this, 'resetForm')),
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$form_state['redirect_route'] = new Url('path.admin_overview_filter', array(
'keys' => trim($form_state['values']['filter']),
));
}
/**
* Resets the filter selections.
*/
public function resetForm(array &$form, array &$form_state) {
$form_state['redirect_route'] = new Url('path.admin_overview');
}
}
<?php
/**
* @file
* Contains \Drupal\path\Form\PathFormBase.
*/
namespace Drupal\path\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\Language;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Path\AliasStorageInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a base class for path add/edit forms.
*/
abstract class PathFormBase extends FormBase {
/**
* An array containing the path ID, source, alias, and language code.
*
* @var array
*/
protected $path;
/**
* The path alias storage.
*
* @var \Drupal\Core\Path\AliasStorageInterface
*/
protected $aliasStorage;
/**
* The path alias manager.
*
* @var \Drupal\Core\Path\AliasManagerInterface
*/
protected $aliasManager;
/**
* Constructs a new PathController.
*
* @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
* The path alias storage.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The path alias manager.
*/
public function __construct(AliasStorageInterface $alias_storage, AliasManagerInterface $alias_manager) {
$this->aliasStorage = $alias_storage;
$this->aliasManager = $alias_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('path.alias_storage'),
$container->get('path.alias_manager')
);
}
/**
* Builds the path used by the form.
*
* @param int|null $pid
* Either the unique path ID, or NULL if a new one is being created.
*/
abstract protected function buildPath($pid);
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $pid = NULL) {
$this->path = $this->buildPath($pid);
$form['source'] = array(
'#type' => 'textfield',
'#title' => $this->t('Existing system path'),
'#default_value' => $this->path['source'],
'#maxlength' => 255,
'#size' => 45,
'#description' => $this->t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'),
'#field_prefix' => url(NULL, array('absolute' => TRUE)),
'#required' => TRUE,
);
$form['alias'] = array(
'#type' => 'textfield',
'#title' => $this->t('Path alias'),
'#default_value' => $this->path['alias'],
'#maxlength' => 255,
'#size' => 45,
'#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
'#field_prefix' => url(NULL, array('absolute' => TRUE)),
'#required' => TRUE,
);
// A hidden value unless language.module is enabled.
if (\Drupal::moduleHandler()->moduleExists('language')) {
$languages = \Drupal::languageManager()->getLanguages();
$language_options = array();
foreach ($languages as $langcode => $language) {
$language_options[$langcode] = $language->name;
}
$form['langcode'] = array(
'#type' => 'select',
'#title' => $this->t('Language'),
'#options' => $language_options,
'#empty_value' => Language::LANGCODE_NOT_SPECIFIED,
'#empty_option' => $this->t('- None -'),
'#default_value' => $this->path['langcode'],
'#weight' => -10,
'#description' => $this->t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set as <em>- None -</em>.'),
);
}
else {
$form['langcode'] = array(
'#type' => 'value',
'#value' => $this->path['langcode']
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$source = &$form_state['values']['source'];
$source = $this->aliasManager->getSystemPath($source);
$alias = $form_state['values']['alias'];
// Language is only set if language.module is enabled, otherwise save for all
// languages.
$langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED;
if ($this->aliasStorage->aliasExists($alias, $langcode, $source)) {
$this->setFormError('alias', $form_state, t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
}
if (!drupal_valid_path($source)) {
$this->setFormError('source', $form_state, t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
// Remove unnecessary values.
form_state_values_clean($form_state);
$pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
$source = &$form_state['values']['source'];
$source = $this->aliasManager->getSystemPath($source);
$alias = $form_state['values']['alias'];
// Language is only set if language.module is enabled, otherwise save for all
// languages.
$langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED;
$this->aliasStorage->save($source, $alias, $langcode, $pid);
drupal_set_message($this->t('The alias has been saved.'));
$form_state['redirect_route'] = new Url('path.admin_overview');
}
}
This diff is collapsed.
......@@ -11,6 +11,7 @@ path.admin_overview:
defaults:
_title: 'URL aliases'
_content: '\Drupal\path\Controller\PathController::adminOverview'
keys: NULL
requirements:
_permission: 'administer url aliases'
......@@ -18,7 +19,7 @@ path.admin_overview_filter:
path: '/admin/config/search/path/list/{keys}'
defaults:
_title: 'URL aliases'
_content: '\Drupal\path\Controller\PathController::adminOverviewFiltered'
_content: '\Drupal\path\Controller\PathController::adminOverview'
requirements:
_permission: 'administer url aliases'
......@@ -26,14 +27,14 @@ path.admin_add:
path: '/admin/config/search/path/add'
defaults:
_title: 'Add alias'
_content: '\Drupal\path\Controller\PathController::adminAdd'
_form: '\Drupal\path\Form\AddForm'
requirements:
_permission: 'administer url aliases'
path.admin_edit:
path: '/admin/config/search/path/edit/{path}'
path: '/admin/config/search/path/edit/{pid}'
defaults:
_title: 'Edit alias'
_content: '\Drupal\path\Controller\PathController::adminEdit'
_form: '\Drupal\path\Form\EditForm'
requirements:
_permission: 'administer url aliases'
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