4 * Attach the machine-readable name form element behavior.
6 Drupal
.behaviors
.machineName
= {
8 * Attaches the behavior.
10 * @param settings.machineName
11 * A list of elements to process, keyed by the HTML ID of the form element
12 * containing the human-readable value. Each element is an object defining
13 * the following properties:
14 * - target: The HTML ID of the machine name form element.
15 * - suffix: The HTML ID of a container to show the machine name preview in
16 * (usually a field suffix after the human-readable name form element).
17 * - label: The label to show for the machine name preview.
18 * - replace_pattern: A regular expression (without modifiers) matching
19 * disallowed characters in the machine name; e.g., '[^a-z0-9]+'.
20 * - replace: A character to replace disallowed characters with; e.g., '_'
22 * - standalone: Whether the preview should stay in its own element rather
23 * than the suffix of the source element.
24 * - field_prefix: The #field_prefix of the form element.
25 * - field_suffix: The #field_suffix of the form element.
27 attach: function (context
, settings
) {
29 $.each(settings
.machineName
, function (source_id
, options
) {
30 var $source
= $(source_id
, context
).addClass('machine-name-source');
31 var $target
= $(options
.target
, context
).addClass('machine-name-target');
32 var $suffix
= $(options
.suffix
, context
);
33 var $wrapper
= $target
.closest('.form-item');
34 // All elements have to exist.
35 if (!$source
.length
|| !$target
.length
|| !$suffix
.length
|| !$wrapper
.length
) {
38 // Skip processing upon a form validation error on the machine name.
39 if ($target
.hasClass('error')) {
42 // Figure out the maximum length for the machine name.
43 options
.maxlength
= $target
.attr('maxlength');
44 // Hide the form item container of the machine name form element.
46 // Determine the initial machine name value. Unless the machine name form
47 // element is disabled or not empty, the initial default value is based on
48 // the human-readable form element value.
49 if ($target
.is(':disabled') || $target
.val() != '') {
50 var machine
= $target
.val();
53 var machine
= self
.transliterate($source
.val(), options
);
55 // Append the machine name preview to the source field.
56 var $preview
= $('<span class="machine-name-value">' + options
.field_prefix
+ Drupal
.checkPlain(machine
) + options
.field_suffix
+ '</span>');
59 $suffix
.append(' ').append('<span class="machine-name-label">' + options
.label
+ ':</span>');
61 $suffix
.append(' ').append($preview
);
63 // If the machine name cannot be edited, stop further processing.
64 if ($target
.is(':disabled')) {
68 // If it is editable, append an edit link.
69 var $link
= $('<span class="admin-link"><a href="#">' + Drupal
.t('Edit') + '</a></span>')
74 $source
.unbind('.machineName');
77 $suffix
.append(' ').append($link
);
79 // Preview the machine name in realtime when the human-readable name
80 // changes, but only if there is no machine name yet; i.e., only upon
81 // initial creation, not when editing.
82 if ($target
.val() == '') {
83 $source
.bind('keyup.machineName change.machineName input.machineName', function () {
84 machine
= self
.transliterate($(this).val(), options
);
85 // Set the machine name to the transliterated value.
87 if (machine
!= options
.replace
) {
89 $preview
.html(options
.field_prefix
+ Drupal
.checkPlain(machine
) + options
.field_suffix
);
99 // Initialize machine name preview.
106 * Transliterate a human-readable name to a machine name.
109 * A string to transliterate.
111 * The machine name settings for the corresponding field, containing:
112 * - replace_pattern: A regular expression (without modifiers) matching
113 * disallowed characters in the machine name; e.g., '[^a-z0-9]+'.
114 * - replace: A character to replace disallowed characters with; e.g., '_'
116 * - maxlength: The maximum length of the machine name.
119 * The transliterated source string.
121 transliterate: function (source
, settings
) {
122 var rx
= new RegExp(settings
.replace_pattern
, 'g');
123 return source
.toLowerCase().replace(rx
, settings
.replace
).substr(0, settings
.maxlength
);