neutral_paths.module in Neutral paths 7
Same filename and directory in other branches
Neutral paths.
A module solving a frequent task of small non-english sites to have language neutral path aliases.
File
neutral_paths.moduleView source
<?php
/**
* @file
* Neutral paths.
*
* A module solving a frequent task of small non-english sites to have
* language neutral path aliases.
*/
/**
* Implements hook_menu().
*/
function neutral_paths_menu() {
$items = array();
$items['admin/config/search/path/language_settings'] = array(
'title' => 'Language neutrality',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'neutral_paths_admin',
),
'access arguments' => array(
'administer url aliases',
),
'type' => MENU_LOCAL_TASK,
'weight' => 60,
);
return $items;
}
/***********************************************
* Module settings and administrative operations
*/
/**
* Admin form.
*/
function neutral_paths_admin() {
$form = array();
$form['neutral_paths_fix_new'] = array(
'#type' => 'checkboxes',
'#options' => array(
'node' => t('Nodes'),
'taxonomy' => t('Taxonomy'),
'user' => t('Users'),
),
'#title' => t('Set all newly created path aliases to be language neutral'),
'#default_value' => variable_get('neutral_paths_fix_new', array()),
);
$form['np_save'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
'#submit' => array(
'neutral_paths_admin_submit',
),
);
$form['np_actions'] = array(
'#type' => 'fieldset',
'#title' => t('Immediate actions'),
);
$form['np_actions']['np_bulk_neutral_node'] = _neutral_paths_admin_form_part('node', t('Language neutral node path aliases'));
$form['np_actions']['np_bulk_neutral_taxonomy'] = _neutral_paths_admin_form_part('taxonomy', t('Language neutral taxonomy path aliases'));
$form['np_actions']['np_bulk_neutral_user'] = _neutral_paths_admin_form_part('user', t('Language neutral user path aliases'));
return $form;
}
/**
* Returns a form part for a particular entity type.
*/
function _neutral_paths_admin_form_part($type, $caption) {
$form = array();
$num_aliases = db_select('url_alias')
->condition('source', $type . '/%', 'LIKE')
->countQuery()
->execute()
->fetchCol();
$num_aliases = $num_aliases[0];
$num_neutral_aliases = db_select('url_alias')
->condition('language', LANGUAGE_NONE)
->condition('source', $type . '/%', 'LIKE')
->countQuery()
->execute()
->fetchCol();
$num_neutral_aliases = $num_neutral_aliases[0];
$form['alias_statistics_' . $type] = array(
'#type' => 'markup',
'#markup' => '<p>' . $caption . ': ' . $num_neutral_aliases . ' / ' . $num_aliases . '</p>',
);
// Path alias of any entity type can be made language neutral.
$form['np_bulk_neutral_reset' . $type] = array(
'#type' => 'submit',
'#value' => t('Reset to neutral'),
'#name' => 'submit_reset' . $type,
'#attributes' => $num_aliases == $num_neutral_aliases ? array(
'disabled' => 'disabled',
) : array(),
'#submit' => array(
'neutral_paths_admin_submit_reset_' . $type,
),
);
// FIXME:
// for now only node-related aliases can be switched to a certain language.
if ($type == 'node') {
$form['np_bulk_neutral_localize_' . $type] = array(
'#type' => 'submit',
'#value' => t('Reset to localized'),
'#name' => 'submit_localize_node',
'#attributes' => $num_neutral_aliases == 0 ? array(
'disabled' => 'disabled',
) : array(),
'#submit' => array(
'neutral_paths_admin_submit_localize_nodes',
),
);
}
return $form;
}
/**
* Responds to a command to reset node paths language.
*/
function neutral_paths_admin_submit_reset_node($form, &$form_state) {
_neutral_paths_set_all_to_neutral('node');
}
/**
* Responds to a command to set node paths language.
*
* Path alias language is set to match that of corresponding nodes.
*/
function neutral_paths_admin_submit_localize_nodes($form, &$form_state) {
_neutral_paths_set_all_nodes_to_localized();
}
/**
* Responds to a command to reset taxonomy terms paths language.
*/
function neutral_paths_admin_submit_reset_taxonomy($form, &$form_state) {
_neutral_paths_set_all_to_neutral('taxonomy');
}
/**
* Responds to a command to reset user paths language.
*/
function neutral_paths_admin_submit_reset_user($form, &$form_state) {
_neutral_paths_set_all_to_neutral('user');
}
/**
* Handler for saving settings.
*/
function neutral_paths_admin_submit($form, &$form_state) {
$values =& $form_state['values'];
variable_set('neutral_paths_fix_new', array_keys(array_filter($values['neutral_paths_fix_new'])));
drupal_set_message(t('The configuration options have been saved.'));
}
/*********************************************
* Resetting newly created and updated aliases
*/
/**
* Fixes new aliases.
*/
function neutral_paths_path_insert($path) {
if (_neutral_paths_should_fix($path['source'])) {
_neutral_paths_fix($path['pid']);
}
}
/**
* Fixes updated aliases.
*/
function neutral_paths_path_update($path) {
if (_neutral_paths_should_fix($path['source'])) {
_neutral_paths_fix($path['pid']);
}
}
/**
* Tests if we should make an alias referencing a given source language neutral.
*/
function _neutral_paths_should_fix($source) {
$types = variable_get("neutral_paths_fix_new", array());
$type = explode('/', $source);
$type = $type[0];
return in_array($type, $types);
}
/**
* Fixing a given path id.
*/
function _neutral_paths_fix($pid) {
db_update('url_alias')
->fields(array(
'language' => LANGUAGE_NONE,
))
->condition('pid', $pid)
->execute();
}
/***********************
* Bulk alias operations
*/
/**
* Resets entity type language.
*
* Resets path aliases referring to particular entity type
* to be language neutral.
*/
function _neutral_paths_set_all_to_neutral($type) {
$num_updated = db_update('url_alias')
->fields(array(
'language' => LANGUAGE_NONE,
))
->condition('language', LANGUAGE_NONE, '!=')
->condition('source', $type . '/%', 'LIKE')
->execute();
if ($num_updated > 0) {
drupal_set_message(t('@num aliases were reset to language neutral', array(
'@num' => $num_updated,
)));
}
else {
drupal_set_message(t('No aliases were updated.') . $type);
}
}
/**
* Sets path alias language to match node language.
*/
function _neutral_paths_set_all_nodes_to_localized() {
$nodes = db_select('node', 'n')
->fields('n', array(
'nid',
'language',
))
->condition('language', LANGUAGE_NONE, '!=')
->execute();
$total_updated = 0;
$total_nodes = 0;
while ($node = $nodes
->fetchAssoc()) {
$num_updated = db_update('url_alias')
->fields(array(
'language' => $node['language'],
))
->condition('language', LANGUAGE_NONE)
->condition('source', 'node/' . $node['nid'])
->execute();
$total_updated += $num_updated;
if ($num_updated > 0) {
$total_nodes++;
}
}
if ($total_updated > 0) {
drupal_set_message(t('@num_aliases aliases referred to @num_nodes nodes were set according to node language.', array(
'@num_aliases' => $total_updated,
'@num_nodes' => $total_nodes,
)));
}
else {
drupal_set_message(t('No aliases were updated.'));
}
}
Functions
Name | Description |
---|---|
neutral_paths_admin | Admin form. |
neutral_paths_admin_submit | Handler for saving settings. |
neutral_paths_admin_submit_localize_nodes | Responds to a command to set node paths language. |
neutral_paths_admin_submit_reset_node | Responds to a command to reset node paths language. |
neutral_paths_admin_submit_reset_taxonomy | Responds to a command to reset taxonomy terms paths language. |
neutral_paths_admin_submit_reset_user | Responds to a command to reset user paths language. |
neutral_paths_menu | Implements hook_menu(). |
neutral_paths_path_insert | Fixes new aliases. |
neutral_paths_path_update | Fixes updated aliases. |
_neutral_paths_admin_form_part | Returns a form part for a particular entity type. |
_neutral_paths_fix | Fixing a given path id. |
_neutral_paths_set_all_nodes_to_localized | Sets path alias language to match node language. |
_neutral_paths_set_all_to_neutral | Resets entity type language. |
_neutral_paths_should_fix | Tests if we should make an alias referencing a given source language neutral. |