Processes a machine-readable name form element.
$element: The form element to process. Properties used:
function form_process_machine_name($element, &$form_state) {
// Apply default form element properties.
$element += array(
'#title' => t('Machine-readable name'),
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#machine_name' => array(),
'#field_prefix' => '',
'#field_suffix' => '',
'#suffix' => '',
);
// A form element that only wants to set one #machine_name property (usually
// 'source' only) would leave all other properties undefined, if the defaults
// were defined in hook_element_info(). Therefore, we apply the defaults here.
$element['#machine_name'] += array(
'source' => array(
'name',
),
'target' => '#' . $element['#id'],
'label' => t('Machine name'),
'replace_pattern' => '[^a-z0-9_]+',
'replace' => '_',
'standalone' => FALSE,
'field_prefix' => $element['#field_prefix'],
'field_suffix' => $element['#field_suffix'],
);
// By default, machine names are restricted to Latin alphanumeric characters.
// So, default to LTR directionality.
if (!isset($element['#attributes'])) {
$element['#attributes'] = array();
}
$element['#attributes'] += array(
'dir' => 'ltr',
);
// The source element defaults to array('name'), but may have been overidden.
if (empty($element['#machine_name']['source'])) {
return $element;
}
// Retrieve the form element containing the human-readable name from the
// complete form in $form_state. By reference, because we may need to append
// a #field_suffix that will hold the live preview.
$key_exists = NULL;
$source = drupal_array_get_nested_value($form_state['complete form'], $element['#machine_name']['source'], $key_exists);
if (!$key_exists) {
return $element;
}
$suffix_id = $source['#id'] . '-machine-name-suffix';
$element['#machine_name']['suffix'] = '#' . $suffix_id;
if ($element['#machine_name']['standalone']) {
$element['#suffix'] .= ' <small id="' . $suffix_id . '"> </small>';
}
else {
// Append a field suffix to the source form element, which will contain
// the live preview of the machine name.
$source += array(
'#field_suffix' => '',
);
$source['#field_suffix'] .= ' <small id="' . $suffix_id . '"> </small>';
$parents = array_merge($element['#machine_name']['source'], array(
'#field_suffix',
));
drupal_array_set_nested_value($form_state['complete form'], $parents, $source['#field_suffix']);
}
$js_settings = array(
'type' => 'setting',
'data' => array(
'machineName' => array(
'#' . $source['#id'] => $element['#machine_name'],
),
),
);
$element['#attached']['js'][] = 'misc/machine-name.js';
$element['#attached']['js'][] = $js_settings;
return $element;
}