public static MachineName::processMachineName(&$element, FormStateInterface $form_state, &$complete_form)
Processes a machine-readable name form element.
array $element: The form element to process. See main class documentation for properties.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
array The processed element.
public static function processMachineName(&$element, FormStateInterface $form_state, &$complete_form) { // We need to pass the langcode to the client. $language = \Drupal::languageManager()->getCurrentLanguage(); // 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 by an element plugin. Therefore, we apply the defaults here. $element['#machine_name'] += array( 'source' => array('label'), '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' => LanguageInterface::DIRECTION_LTR); // The source element defaults to array('name'), but may have been overridden. 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 = NestedArray::getValue($form_state->getCompleteForm(), $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'] = $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'] = $source['#field_suffix'] . ' <small id="' . $suffix_id . '"> </small>'; $parents = array_merge($element['#machine_name']['source'], array('#field_suffix')); NestedArray::setValue($form_state->getCompleteForm(), $parents, $source['#field_suffix']); } $element['#attached']['library'][] = 'core/drupal.machine-name'; $options = [ 'replace_pattern', 'replace', 'maxlength', 'target', 'label', 'field_prefix', 'field_suffix', 'suffix', ]; $element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = array_intersect_key($element['#machine_name'], array_flip($options)); $element['#attached']['drupalSettings']['langcode'] = $language->getId(); return $element; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!MachineName.php/function/MachineName::processMachineName/8.1.x