Provides a machine name render element.
Provides a form element to enter a machine name, which is validated to ensure that the name is unique and does not contain disallowed characters.
The element may be automatically populated via JavaScript when used in conjunction with a separate "source" form element (typically specifying the human-readable name). As the user types text into the source element, the JavaScript converts all values to lower case, replaces any remaining disallowed characters with a replacement, and populates the associated machine name form element.
Properties:
-
#machine_name: An associative array containing:
-
exists: A callable to invoke for checking whether a submitted machine name value already exists. The arguments passed to the callback will be:
- The submitted value.
- The element array.
- The form state object.
In most cases, an existing API or menu argument loader function can be re-used. The callback is only invoked if the submitted value differs from the element's #default_value.
- source: (optional) The #array_parents of the form element containing the human-readable name (i.e., as contained in the $form structure) to use as source for the machine name. Defaults to array('label').
- label: (optional) Text to display as label for the machine name value after the human-readable name form element. Defaults to t('Machine name').
- replace_pattern: (optional) A regular expression (without delimiters) matching disallowed characters in the machine name. Defaults to '[^a-z0-9_]+'.
- replace: (optional) A character to replace disallowed characters in the machine name via JavaScript. Defaults to '_' (underscore). When using a different character, 'replace_pattern' needs to be set accordingly.
- error: (optional) A custom form error message string to show, if the machine name contains disallowed characters.
- standalone: (optional) Whether the live preview should stay in its own form element rather than in the suffix of the source element. Defaults to FALSE.
-
exists: A callable to invoke for checking whether a submitted machine name value already exists. The arguments passed to the callback will be:
- #maxlength: (optional) Maximum allowed length of the machine name. Defaults to 64.
- #disabled: (optional) Should be set to TRUE if an existing machine name must not be changed after initial creation.
Usage example:
$form['id'] = array( '#type' => 'machine_name', '#default_value' => $this->entity->id(), '#disabled' => !$this->entity->isNew(), '#maxlength' => 64, '#description' => $this->t('A unique name for this item. It must only contain lowercase letters, numbers, and underscores.'), '#machine_name' => array( 'exists' => array($this, 'exists'), ), );
Plugin annotation
@FormElement("machine_name")
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\Textfield
- class \Drupal\Core\Render\Element\MachineName
- class \Drupal\Core\Render\Element\Textfield
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait
See also
\Drupal\Core\Render\Element\Textfield
File
- core/lib/Drupal/Core/Render/Element/MachineName.php, line 71
Namespace
Drupal\Core\Render\Element
Members
Name | Modifiers | Type | Description |
---|---|---|---|
DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. |
DependencySerializationTrait::__sleep | public | function | |
DependencySerializationTrait::__wakeup | public | function | |
FormElement::processAutocomplete | public static | function | Adds autocomplete functionality to elements. |
FormElement::processPattern | public static | function | #process callback for #pattern form element property. |
FormElement::validatePattern | public static | function | #element_validate callback for #pattern form element property. |
MachineName::getInfo | public | function | Returns the element properties for this element. Overrides Textfield::getInfo |
MachineName::processMachineName | public static | function | Processes a machine-readable name form element. |
MachineName::validateMachineName | public static | function | Form element validation handler for machine_name elements. |
MachineName::valueCallback | public static | function | Determines how user input is mapped to an element's #value property. Overrides Textfield::valueCallback |
PluginBase::$configuration | protected | property | Configuration information passed into the plugin. |
PluginBase::$pluginDefinition | protected | property | The plugin implementation definition. |
PluginBase::$pluginId | protected | property | The plugin_id. |
PluginBase::DERIVATIVE_SEPARATOR | constant | A string which is used to separate base plugin IDs from the derivative ID. | |
PluginBase::getBaseId | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId |
PluginBase::getDerivativeId | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId |
PluginBase::getPluginDefinition | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition |
PluginBase::getPluginId | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId |
PluginBase::__construct | public | function | Constructs a Drupal\Component\Plugin\PluginBase object. |
RenderElement::preRenderAjaxForm | public static | function | Adds Ajax information about an element to communicate with JavaScript. |
RenderElement::preRenderGroup | public static | function | Adds members of this group as actual elements for rendering. |
RenderElement::processAjaxForm | public static | function | Form element processing handler for the #ajax form property. |
RenderElement::processGroup | public static | function | Arranges elements into groups. |
RenderElement::setAttributes | public static | function | Sets a form element's class attribute. Overrides ElementInterface::setAttributes |
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. |
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. |
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. |
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Textfield::preRenderTextfield | public static | function | Prepares a #type 'textfield' render element for input.html.twig. |
Please login to continue.