| 1 |
<?php |
<?php |
| 2 |
// $Id: pathauto.module,v 1.118.2.4 2009/04/20 22:43:44 greggles Exp $ |
// $Id: pathauto.module,v 1.118.2.5 2009/09/25 16:06:23 greggles Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @defgroup pathauto Pathauto: Automatically generates aliases for content |
* @defgroup pathauto Pathauto: Automatically generates aliases for content |
| 263 |
* into the path module's fieldset in the node form. |
* into the path module's fieldset in the node form. |
| 264 |
*/ |
*/ |
| 265 |
function pathauto_form_alter(&$form, $form_state, $form_id) { |
function pathauto_form_alter(&$form, $form_state, $form_id) { |
| 266 |
// Only do this for node forms |
// Process only node forms. |
| 267 |
if (isset($form['#id']) && ($form['#id'] == 'node-form') && arg(0) == 'node') { |
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { |
| 268 |
$node = $form['#node']; |
$node = $form['#node']; |
| 269 |
|
$pattern = FALSE; |
| 270 |
|
|
| 271 |
// See if there is a pathauto pattern or default applicable |
// Find if there is an automatic alias pattern for this node type. |
| 272 |
if (isset($form['language'])) { |
if (isset($form['language'])) { |
| 273 |
$language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value']; |
$language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value']; |
| 274 |
$pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_'. $language .'_pattern', '')); |
$pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_'. $language .'_pattern', '')); |
| 275 |
} |
} |
| 276 |
if (empty($pattern)) { |
if (!$pattern) { |
| 277 |
$pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_pattern', '')); |
$pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_pattern', '')); |
| 278 |
if (empty($pattern)) { |
if (!$pattern) { |
| 279 |
$pattern = trim(variable_get('pathauto_node_pattern', '')); |
$pattern = trim(variable_get('pathauto_node_pattern', '')); |
| 280 |
} |
} |
| 281 |
} |
} |
| 282 |
// If there is a pattern AND the user is allowed to create aliases AND the path textbox is present on this form |
|
| 283 |
if (!empty($pattern) && user_access('create url aliases') && isset($form['path']['path'])) { |
// If there is a pattern, show the automatic alias checkbox. |
| 284 |
$output = t('An alias will be generated for you. If you wish to create your own alias below, untick this option.'); |
if ($pattern) { |
| 285 |
if (user_access('administer pathauto')) { |
if (!isset($node->pathauto_perform_alias)) { |
| 286 |
$output .= t(' To control the format of the generated aliases, see the <a href="@pathauto">Pathauto settings</a>.', array('@pathauto' => url('admin/build/path/pathauto'))); |
if (isset($node->nid)) { |
| 287 |
|
// If this is not a new node, compare it's current alias to the |
| 288 |
|
// alias that would be genereted by pathauto. If they are the same, |
| 289 |
|
// then keep the automatic alias enabled. |
| 290 |
|
_pathauto_include(); |
| 291 |
|
$placeholders = pathauto_get_placeholders('node', $node); |
| 292 |
|
$pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language); |
| 293 |
|
$node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias; |
| 294 |
|
} |
| 295 |
|
else { |
| 296 |
|
// If this is a new node, enable the automatic alias. |
| 297 |
|
$node->pathauto_perform_alias = TRUE; |
| 298 |
|
} |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
|
// Add JavaScript that will disable the path textfield when the automatic |
| 302 |
|
// alias checkbox is checked. |
| 303 |
drupal_add_js(drupal_get_path('module', 'pathauto') .'/pathauto.js'); |
drupal_add_js(drupal_get_path('module', 'pathauto') .'/pathauto.js'); |
|
$form['path']['#collapsed'] = FALSE; |
|
| 304 |
|
|
| 305 |
$form['path']['pathauto_perform_alias'] = array( |
$form['path']['pathauto_perform_alias'] = array( |
| 306 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 307 |
'#title' => t('Automatic alias'), |
'#title' => t('Automatic alias'), |
| 308 |
'#default_value' => isset($node->pathauto_perform_alias) ? $node->pathauto_perform_alias : TRUE, |
'#default_value' => $node->pathauto_perform_alias, |
| 309 |
'#description' => $output, |
'#description' => t('An alias will be generated for you. If you wish to create your own alias below, uncheck this option.'), |
| 310 |
'#weight' => -1, |
'#weight' => -1, |
| 311 |
); |
); |
| 312 |
|
|
| 313 |
if (!empty($node->pathauto_perform_alias) && !empty($node->old_alias) && $node->path == '') { |
if (user_access('administer pathauto')) { |
| 314 |
|
$form['path']['pathauto_perform_alias']['#description'] .= ' '. t('To control the format of the generated aliases, see the <a href="@pathauto">automated alias settings</a>.', array('@pathauto' => url('admin/build/path/pathauto'))); |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) { |
| 318 |
$form['path']['path']['#default_value'] = $node->old_alias; |
$form['path']['path']['#default_value'] = $node->old_alias; |
| 319 |
$node->path = $node->old_alias; |
$node->path = $node->old_alias; |
| 320 |
} |
} |
| 321 |
|
|
| 322 |
//For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it |
// For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it |
| 323 |
if (isset($node->path)) { |
if (isset($node->path)) { |
| 324 |
$form['path']['old_alias'] = array('#type' => 'value', '#value' => $node->path); |
$form['path']['old_alias'] = array( |
| 325 |
|
'#type' => 'value', |
| 326 |
|
'#value' => $node->path, |
| 327 |
|
); |
| 328 |
} |
} |
| 329 |
} |
} |
| 330 |
} |
} |