View source
<?php
require_once dirname(__FILE__) . '/taxonomy_machine_name.api.inc';
require_once dirname(__FILE__) . '/taxonomy_machine_name.views.inc';
require_once dirname(__FILE__) . '/taxonomy_machine_name.token.inc';
if (module_exists('migrate')) {
require_once dirname(__FILE__) . '/taxonomy_machine_name.migrate.inc';
}
function taxonomy_machine_name_views_api() {
return array(
'api' => 3,
);
}
function taxonomy_machine_name_migrate_api() {
return array(
'api' => 2,
'destination handlers' => array(
'MigrateTaxonomyTermMachineNameHandler',
),
'field handlers' => array(
'MigrateTaxonomyTermReferenceMachineNameFieldHandler',
),
);
}
function taxonomy_machine_name_entity_property_info_alter(&$infos) {
$infos['taxonomy_term']['properties']['machine_name'] = array(
'label' => t('Machine name'),
'type' => 'text',
'required' => FALSE,
'description' => 'The taxonomy term machine name.',
'schema field' => 'machine_name',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
'entity views field' => TRUE,
'entity token' => TRUE,
);
}
function taxonomy_term_machine_name_load($machine_name, $vocabulary) {
$conditions = array(
'machine_name' => trim($machine_name),
);
$args = func_get_args();
if (isset($args[2]['term']) && is_object($args[2]['term'])) {
$vocabulary = $args[2]['term']->vocabulary_machine_name;
}
if (is_string($vocabulary)) {
$vocabularies =& drupal_static(__FUNCTION__);
if (NULL === $vocabularies) {
$vocabularies = taxonomy_vocabulary_get_names();
}
$vocabulary = $vocabularies[$vocabulary];
}
if (is_object($vocabulary)) {
$conditions['vid'] = $vocabulary->vid;
}
elseif (is_numeric($vocabulary)) {
$conditions['vid'] = (int) $vocabulary;
}
else {
return FALSE;
}
$term = taxonomy_term_load_multiple(array(), $conditions);
return $term ? reset($term) : FALSE;
}
function taxonomy_machine_name_taxonomy_term_presave($term) {
if (empty($term->machine_name)) {
$term->machine_name = taxonomy_machine_name_clean_name($term->name);
watchdog('taxonomy', 'Add default machine name for term: %name => %machine_name', array(
'%name' => $term->name,
'%machine_name' => $term->machine_name,
), WATCHDOG_DEBUG);
}
else {
$term->machine_name = taxonomy_machine_name_clean_name($term->machine_name);
}
$original_machine_name = $machine_name = $term->machine_name;
taxonomy_machine_name_uniquify($machine_name, $term);
if ($original_machine_name != $machine_name) {
$term->machine_name = $machine_name;
watchdog('taxonomy', 'Duplicate machine name for term: %original_machine_name => %machine_name', array(
'%original_machine_name' => $original_machine_name,
'%machine_name' => $term->machine_name,
), WATCHDOG_DEBUG);
}
}
function taxonomy_machine_name_clean_name($name, $force = FALSE) {
if (!preg_match('/^[a-z0-9\\_]+$/i', $name) || $force) {
if (module_exists('pathauto')) {
module_load_include('inc', 'pathauto');
$machine_name = pathauto_cleanstring(str_replace('_', variable_get('pathauto_separator', '-'), $name));
$machine_name = str_replace(variable_get('pathauto_separator', '-'), '_', $machine_name);
}
elseif (module_exists('token')) {
module_load_include('module', 'token');
$machine_name = token_clean_token_name($name);
$machine_name = str_replace('-', '_', drupal_strtolower($machine_name));
}
else {
$machine_name = drupal_strtolower(str_replace('_', '-', $name));
}
$machine_name = preg_replace('/[^a-z0-9\\_]/iu', '_', $machine_name);
$machine_name = trim($machine_name, '_');
}
else {
$machine_name = trim(drupal_strtolower($name));
}
drupal_alter('taxonomy_machine_name_clean_name', $machine_name, $name, $force);
return $machine_name;
}
function taxonomy_machine_name_uniquify(&$machine_name, $term) {
if (!taxonomy_machine_name_exists($machine_name, $term)) {
return;
}
$maxlength = _taxonomy_machine_name_get_schema_machine_name_maxlength();
$separator = '_';
$original_machine_name = $machine_name;
$i = 0;
do {
$unique_suffix = $separator . $i;
$machine_name = truncate_utf8($original_machine_name, $maxlength - drupal_strlen($unique_suffix, TRUE)) . $unique_suffix;
$i++;
} while (taxonomy_machine_name_exists($machine_name, $term));
}
function taxonomy_machine_name_exists($machine_name, $term) {
$pid = db_query_range("SELECT tid\n FROM {taxonomy_term_data}\n WHERE vid = :vid\n AND machine_name = :machine_name\n AND tid <> :tid", 0, 1, array(
':vid' => $term->vid,
':machine_name' => $machine_name,
':tid' => isset($term->tid) ? $term->tid : 0,
))
->fetchField();
return !empty($pid);
}
function taxonomy_machine_name_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'taxonomy_overview_terms') {
foreach (element_children($form) as $key) {
if (isset($form[$key]['#term'])) {
$term =& $form[$key];
$term_object = isset($term['tid']['#value']) ? taxonomy_term_load($term['tid']['#value']) : taxonomy_term_load($term['#term']['tid']);
$term['view']['#title'] .= ' (' . $term_object->machine_name . ')';
}
}
}
elseif ($form_id == 'taxonomy_form_term') {
$op = isset($form_state['input']['op']) ? $form_state['input']['op'] : '';
if ($op != t('Delete')) {
$term = $form_state['term'];
$form['machine_name'] = array(
'#type' => 'machine_name',
'#default_value' => '',
'#maxlength' => 255,
'#machine_name' => array(
'exists' => 'taxonomy_term_machine_name_load',
),
'#weight' => -4,
);
$form['old_machine_name'] = array(
'#type' => 'value',
'#value' => '',
);
$form['#validate'][] = 'taxonomy_machine_name_form_vocabulary_validate';
if (NULL !== $term->tid) {
$form['machine_name']['#default_value'] = $term->machine_name;
$form['old_machine_name']['#default_value'] = $term->machine_name;
}
}
}
}
function taxonomy_machine_name_form_vocabulary_validate($form, &$form_state) {
if (isset($form_state['values']['machine_name'])) {
$machine_name = $form_state['values']['machine_name'];
$disallowed = array(
'add',
'list',
);
if (in_array($machine_name, $disallowed)) {
form_set_error('machine_name', t('The machine-readable name cannot be "add" or "list".'));
}
}
}
function _taxonomy_machine_name_get_schema_machine_name_maxlength() {
$maxlength =& drupal_static(__FUNCTION__);
if (!isset($maxlength)) {
$schema = drupal_get_schema('taxonomy_term_data');
$maxlength = $schema['fields']['machine_name']['length'];
}
return $maxlength;
}