2007-07-22 20:21:02 +00:00
< ? php
/**
* @ file
2007-07-30 18:23:25 +00:00
* Administrative page callbacks for the path module .
2007-07-22 20:21:02 +00:00
*/
2013-05-25 20:12:45 +00:00
use Drupal\Core\Language\Language ;
2013-10-25 19:40:11 +00:00
use Drupal\Component\Utility\String ;
2013-05-25 20:12:45 +00:00
2007-07-22 20:21:02 +00:00
/**
2012-03-28 18:02:04 +00:00
* Returns a listing of all defined URL aliases .
2009-10-20 01:24:34 +00:00
*
2007-07-22 20:21:02 +00:00
* When filter key passed , perform a standard search on the given key ,
* and return the list of matching URL aliases .
2013-09-18 18:30:30 +00:00
*
* @ deprecated Use \Drupal\path\Controller\PathController :: adminOverview ()
2007-07-22 20:21:02 +00:00
*/
function path_admin_overview ( $keys = NULL ) {
// Add the filter form above the overview table.
2009-07-29 06:39:35 +00:00
$build [ 'path_admin_filter_form' ] = drupal_get_form ( 'path_admin_filter_form' , $keys );
2012-02-02 16:08:57 +00:00
// Enable language column if language.module is enabled or if we have any
// alias with a language.
2013-05-25 20:12:45 +00:00
$alias_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {url_alias} WHERE langcode <> :langcode' , 0 , 1 , array ( ':langcode' => Language :: LANGCODE_NOT_SPECIFIED )) -> fetchField ();
2013-10-16 12:17:55 +00:00
$multilanguage = ( \Drupal :: moduleHandler () -> moduleExists ( 'language' ) || $alias_exists );
2007-07-22 20:21:02 +00:00
2010-05-14 04:57:59 +00:00
$header = array ();
$header [] = array ( 'data' => t ( 'Alias' ), 'field' => 'alias' , 'sort' => 'asc' );
$header [] = array ( 'data' => t ( 'System' ), 'field' => 'source' );
2007-07-22 20:21:02 +00:00
if ( $multilanguage ) {
2012-01-23 02:14:12 +00:00
$header [] = array ( 'data' => t ( 'Language' ), 'field' => 'langcode' );
2007-07-22 20:21:02 +00:00
}
2012-10-09 19:49:07 +00:00
$header [] = t ( 'Operations' );
2009-05-10 16:50:19 +00:00
2012-06-05 05:42:19 +00:00
$query = db_select ( 'url_alias' )
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2009-05-10 16:50:19 +00:00
if ( $keys ) {
// Replace wildcards with PDO wildcards.
2009-10-15 17:53:34 +00:00
$query -> condition ( 'alias' , '%' . preg_replace ( '!\*+!' , '%' , $keys ) . '%' , 'LIKE' );
2009-05-10 16:50:19 +00:00
}
$result = $query
-> fields ( 'url_alias' )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header )
2009-05-10 16:50:19 +00:00
-> limit ( 50 )
-> execute ();
2007-07-22 20:21:02 +00:00
$rows = array ();
$destination = drupal_get_destination ();
2009-05-10 16:50:19 +00:00
foreach ( $result as $data ) {
2010-05-14 04:57:59 +00:00
$row = array ();
2012-05-08 05:03:01 +00:00
$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 ),
));
2010-05-14 04:57:59 +00:00
if ( $multilanguage ) {
2012-02-02 16:08:57 +00:00
$row [ 'data' ][ 'language_name' ] = language_name ( $data -> langcode );
2010-05-14 04:57:59 +00:00
}
$operations = array ();
$operations [ 'edit' ] = array (
'title' => t ( 'edit' ),
'href' => " admin/config/search/path/edit/ $data->pid " ,
'query' => $destination ,
);
$operations [ 'delete' ] = array (
'title' => t ( 'delete' ),
'href' => " admin/config/search/path/delete/ $data->pid " ,
'query' => $destination ,
);
$row [ 'data' ][ 'operations' ] = array (
2008-12-05 12:50:28 +00:00
'data' => array (
2012-10-09 19:49:07 +00:00
'#type' => 'operations' ,
2010-05-14 04:57:59 +00:00
'#links' => $operations ,
2008-12-05 12:50:28 +00:00
),
);
2010-05-14 04:57:59 +00:00
2009-08-22 14:34:23 +00:00
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
2013-09-16 03:58:06 +00:00
if ( $data -> alias != \Drupal :: service ( 'path.alias_manager' ) -> getPathAlias ( $data -> source , $data -> langcode )) {
2009-08-22 14:34:23 +00:00
$row [ 'class' ] = array ( 'warning' );
}
2010-05-14 04:57:59 +00:00
2007-07-22 20:21:02 +00:00
$rows [] = $row ;
}
2009-07-29 06:39:35 +00:00
$build [ 'path_table' ] = array (
2009-10-10 20:46:17 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-12-02 14:56:32 +00:00
'#rows' => $rows ,
2010-05-14 04:57:59 +00:00
'#empty' => t ( 'No URL aliases available. <a href="@link">Add URL alias</a>.' , array ( '@link' => url ( 'admin/config/search/path/add' ))),
2009-07-29 06:39:35 +00:00
);
$build [ 'path_pager' ] = array ( '#theme' => 'pager' );
2007-07-22 20:21:02 +00:00
2009-07-29 06:39:35 +00:00
return $build ;
2007-07-22 20:21:02 +00:00
}
/**
2012-03-28 18:02:04 +00:00
* Page callback : Returns a form creating or editing a path alias .
*
* @ param $path
* An array containing the path ID , source , alias , and language code .
*
* @ return
* A form for adding or editing a URL alias .
*
* @ see path_menu ()
2013-09-18 18:30:30 +00:00
*
* @ deprecated Use \Drupal\path\Controller\PathController :: adminEdit () or
* \Drupal\path\Controller\PathController :: adminEdit ()
2007-07-22 20:21:02 +00:00
*/
2009-10-15 17:53:34 +00:00
function path_admin_edit ( $path = array ()) {
if ( $path ) {
$output = drupal_get_form ( 'path_admin_form' , $path );
2013-10-25 19:40:11 +00:00
$output [ '#title' ] = String :: checkPlain ( $path [ 'alias' ]);
2007-07-22 20:21:02 +00:00
}
else {
2007-08-12 16:34:56 +00:00
$output = drupal_get_form ( 'path_admin_form' );
2007-07-22 20:21:02 +00:00
}
return $output ;
}
/**
2012-03-28 18:02:04 +00:00
* Form constructor for the path administration form .
*
* @ param $path
* An array containing the path ID , source , alias , and language code .
2007-07-22 20:21:02 +00:00
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see path_admin_form_validate ()
* @ see path_admin_form_submit ()
2012-03-28 18:02:04 +00:00
* @ see path_admin_form_delete_submit ()
2007-07-22 20:21:02 +00:00
*/
2013-05-25 20:12:45 +00:00
function path_admin_form ( $form , & $form_state , $path = array ( 'source' => '' , 'alias' => '' , 'langcode' => Language :: LANGCODE_NOT_SPECIFIED , 'pid' => NULL )) {
2009-10-15 17:53:34 +00:00
$form [ 'source' ] = array (
2007-07-22 20:21:02 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Existing system path' ),
2009-10-15 17:53:34 +00:00
'#default_value' => $path [ 'source' ],
2009-06-11 04:59:26 +00:00
'#maxlength' => 255 ,
2007-07-22 20:21:02 +00:00
'#size' => 45 ,
2009-10-24 05:13:44 +00:00
'#description' => t ( 'Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.' ),
2012-04-29 15:16:27 +00:00
'#field_prefix' => url ( NULL , array ( 'absolute' => TRUE )),
2007-11-10 12:08:22 +00:00
'#required' => TRUE ,
2007-07-22 20:21:02 +00:00
);
2009-10-15 17:53:34 +00:00
$form [ 'alias' ] = array (
2007-07-22 20:21:02 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Path alias' ),
2009-10-15 17:53:34 +00:00
'#default_value' => $path [ 'alias' ],
2009-06-11 04:59:26 +00:00
'#maxlength' => 255 ,
2007-07-22 20:21:02 +00:00
'#size' => 45 ,
'#description' => 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.' ),
2012-04-29 15:16:27 +00:00
'#field_prefix' => url ( NULL , array ( 'absolute' => TRUE )),
2007-11-10 12:08:22 +00:00
'#required' => TRUE ,
2007-07-22 20:21:02 +00:00
);
2009-10-15 17:53:34 +00:00
2012-02-02 16:08:57 +00:00
// A hidden value unless language.module is enabled.
2013-10-16 12:17:55 +00:00
if ( \Drupal :: moduleHandler () -> moduleExists ( 'language' )) {
2012-04-25 23:44:20 +00:00
$languages = language_list ();
2012-01-23 15:46:29 +00:00
foreach ( $languages as $langcode => $language ) {
$language_options [ $langcode ] = $language -> name ;
}
2012-01-23 02:14:12 +00:00
$form [ 'langcode' ] = array (
2011-09-21 12:44:37 +00:00
'#type' => 'select' ,
'#title' => t ( 'Language' ),
2012-01-23 15:46:29 +00:00
'#options' => $language_options ,
2013-05-25 20:12:45 +00:00
'#empty_value' => Language :: LANGCODE_NOT_SPECIFIED ,
2012-02-02 16:08:57 +00:00
'#empty_option' => t ( '- None -' ),
2012-01-23 02:14:12 +00:00
'#default_value' => $path [ 'langcode' ],
2011-09-21 12:44:37 +00:00
'#weight' => - 10 ,
2012-02-02 16:08:57 +00:00
'#description' => 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>.' ),
2011-09-21 12:44:37 +00:00
);
}
else {
2012-01-23 02:14:12 +00:00
$form [ 'langcode' ] = array (
2011-09-21 12:44:37 +00:00
'#type' => 'value' ,
2012-01-23 02:14:12 +00:00
'#value' => $path [ 'langcode' ]
2011-09-21 12:44:37 +00:00
);
}
2010-01-21 04:20:08 +00:00
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-21 04:20:08 +00:00
$form [ 'actions' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Save' ),
);
2009-10-15 17:53:34 +00:00
if ( $path [ 'pid' ]) {
$form [ 'pid' ] = array (
'#type' => 'hidden' ,
'#value' => $path [ 'pid' ],
);
2010-01-21 04:20:08 +00:00
$form [ 'actions' ][ 'delete' ] = array (
2009-10-15 17:53:34 +00:00
'#type' => 'submit' ,
2010-01-21 04:20:08 +00:00
'#value' => t ( 'Delete' ),
'#submit' => array ( 'path_admin_form_delete_submit' ),
2009-10-15 17:53:34 +00:00
);
2007-07-22 20:21:02 +00:00
}
return $form ;
}
2010-01-21 04:20:08 +00:00
/**
2012-03-28 18:02:04 +00:00
* Form submission handler for the 'Delete' button on path_admin_form () .
*
* @ see path_admin_form_validate ()
* @ see path_admin_form_submit ()
2010-01-21 04:20:08 +00:00
*/
function path_admin_form_delete_submit ( $form , & $form_state ) {
$destination = array ();
2013-09-16 03:58:06 +00:00
$query = \Drupal :: request () -> query ;
2013-06-14 22:45:49 +00:00
if ( $query -> has ( 'destination' )) {
2010-01-21 04:20:08 +00:00
$destination = drupal_get_destination ();
2013-06-14 22:45:49 +00:00
$query -> remove ( 'destination' );
2010-01-21 04:20:08 +00:00
}
$form_state [ 'redirect' ] = array ( 'admin/config/search/path/delete/' . $form_state [ 'values' ][ 'pid' ], array ( 'query' => $destination ));
}
2007-07-22 20:21:02 +00:00
/**
2012-03-28 18:02:04 +00:00
* Form validation handler for path_admin_form () .
*
* @ see path_admin_form_submit ()
* @ see path_admin_form_delete_submit ()
2007-07-22 20:21:02 +00:00
*/
function path_admin_form_validate ( $form , & $form_state ) {
2009-10-24 05:13:44 +00:00
$source = & $form_state [ 'values' ][ 'source' ];
2013-09-16 03:58:06 +00:00
$source = \Drupal :: service ( 'path.alias_manager' ) -> getSystemPath ( $source );
2009-10-15 17:53:34 +00:00
$alias = $form_state [ 'values' ][ 'alias' ];
2007-07-22 20:21:02 +00:00
$pid = isset ( $form_state [ 'values' ][ 'pid' ]) ? $form_state [ 'values' ][ 'pid' ] : 0 ;
2012-02-02 16:08:57 +00:00
// Language is only set if language.module is enabled, otherwise save for all
// languages.
2013-05-25 20:12:45 +00:00
$langcode = isset ( $form_state [ 'values' ][ 'langcode' ]) ? $form_state [ 'values' ][ 'langcode' ] : Language :: LANGCODE_NOT_SPECIFIED ;
2007-07-22 20:21:02 +00:00
2012-01-23 02:14:12 +00:00
$has_alias = db_query ( " SELECT COUNT(alias) FROM { url_alias} WHERE pid <> :pid AND alias = :alias AND langcode = :langcode " , array (
2009-10-20 01:24:34 +00:00
':pid' => $pid ,
':alias' => $alias ,
2012-01-23 02:14:12 +00:00
':langcode' => $langcode ,
2009-10-20 01:24:34 +00:00
))
-> fetchField ();
2009-05-10 16:50:19 +00:00
if ( $has_alias ) {
2013-11-23 20:57:04 +00:00
form_set_error ( 'alias' , $form_state , t ( 'The alias %alias is already in use in this language.' , array ( '%alias' => $alias )));
2007-07-22 20:21:02 +00:00
}
2009-12-17 13:10:19 +00:00
if ( ! drupal_valid_path ( $source )) {
2013-11-23 20:57:04 +00:00
form_set_error ( 'source' , $form_state , t ( " The path '@link_path' is either invalid or you do not have access to it. " , array ( '@link_path' => $source )));
2007-11-10 12:08:22 +00:00
}
2007-07-22 20:21:02 +00:00
}
/**
2012-03-28 18:02:04 +00:00
* Form submission handler for path_admin_form () .
*
* @ see path_admin_form_validate ()
* @ see path_admin_form_delete_submit ()
2007-07-22 20:21:02 +00:00
*/
function path_admin_form_submit ( $form , & $form_state ) {
2009-10-20 01:24:34 +00:00
// Remove unnecessary values.
form_state_values_clean ( $form_state );
2012-11-21 15:29:04 +00:00
$pid = isset ( $form_state [ 'values' ][ 'pid' ]) ? $form_state [ 'values' ][ 'pid' ] : 0 ;
$source = & $form_state [ 'values' ][ 'source' ];
2013-09-16 03:58:06 +00:00
$source = \Drupal :: service ( 'path.alias_manager' ) -> getSystemPath ( $source );
2012-11-21 15:29:04 +00:00
$alias = $form_state [ 'values' ][ 'alias' ];
// Language is only set if language.module is enabled, otherwise save for all
// languages.
2013-05-25 20:12:45 +00:00
$langcode = isset ( $form_state [ 'values' ][ 'langcode' ]) ? $form_state [ 'values' ][ 'langcode' ] : Language :: LANGCODE_NOT_SPECIFIED ;
2012-11-21 15:29:04 +00:00
2013-09-16 03:58:06 +00:00
\Drupal :: service ( 'path.crud' ) -> save ( $source , $alias , $langcode , $pid );
2007-07-22 20:21:02 +00:00
drupal_set_message ( t ( 'The alias has been saved.' ));
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path' ;
2007-07-22 20:21:02 +00:00
}
/**
2012-03-28 18:02:04 +00:00
* Form constructor for the path admin overview filter form .
2007-07-22 20:21:02 +00:00
*
* @ ingroup forms
2012-03-28 18:02:04 +00:00
* @ see path_admin_filter_form_submit_filter ()
* @ see path_admin_filter_form_submit_reset ()
2007-07-22 20:21:02 +00:00
*/
2009-09-18 00:12:48 +00:00
function path_admin_filter_form ( $form , & $form_state , $keys = '' ) {
2009-08-22 14:34:23 +00:00
$form [ '#attributes' ] = array ( 'class' => array ( 'search-form' ));
2012-11-27 07:06:47 +00:00
$form [ 'basic' ] = array ( '#type' => 'details' ,
2010-03-03 19:46:26 +00:00
'#title' => t ( 'Filter aliases' ),
'#attributes' => array ( 'class' => array ( 'container-inline' )),
2007-07-22 20:21:02 +00:00
);
2010-03-03 19:46:26 +00:00
$form [ 'basic' ][ 'filter' ] = array (
2012-03-27 06:17:38 +00:00
'#type' => 'search' ,
2010-09-11 03:04:43 +00:00
'#title' => 'Path alias' ,
'#title_display' => 'invisible' ,
2007-07-22 20:21:02 +00:00
'#default_value' => $keys ,
2008-11-22 10:49:01 +00:00
'#maxlength' => 128 ,
2007-07-22 20:21:02 +00:00
'#size' => 25 ,
);
2010-03-03 19:46:26 +00:00
$form [ 'basic' ][ 'submit' ] = array (
2007-11-10 12:08:22 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Filter' ),
'#submit' => array ( 'path_admin_filter_form_submit_filter' ),
);
if ( $keys ) {
2010-03-03 19:46:26 +00:00
$form [ 'basic' ][ 'reset' ] = array (
2007-11-10 12:08:22 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Reset' ),
'#submit' => array ( 'path_admin_filter_form_submit_reset' ),
);
}
2007-07-22 20:21:02 +00:00
return $form ;
}
/**
2012-03-28 18:02:04 +00:00
* Form submission handler for the path_admin_filter_form () Filter button .
*
* @ see path_admin_filter_form_submit_reset ()
2007-07-22 20:21:02 +00:00
*/
2007-11-10 12:08:22 +00:00
function path_admin_filter_form_submit_filter ( $form , & $form_state ) {
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path/list/' . trim ( $form_state [ 'values' ][ 'filter' ]);
2007-07-22 20:21:02 +00:00
}
2007-11-10 12:08:22 +00:00
/**
2012-03-28 18:02:04 +00:00
* Form submission handler for the path_admin_filter_form () Reset button .
*
* @ see path_admin_filter_form_submit_filter ()
2007-11-10 12:08:22 +00:00
*/
function path_admin_filter_form_submit_reset ( $form , & $form_state ) {
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path/list' ;
2007-11-10 12:08:22 +00:00
}