You are here

public static function MachineName::processMachineName in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/MachineName.php \Drupal\Core\Render\Element\MachineName::processMachineName()

Processes a machine-readable name form element.

Parameters

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.

Return value

array The processed element.

1 call to MachineName::processMachineName()
MachineNameTest::testProcessMachineName in core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php
@covers ::processMachineName

File

core/lib/Drupal/Core/Render/Element/MachineName.php, line 127

Class

MachineName
Provides a machine name render element.

Namespace

Drupal\Core\Render\Element

Code

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 += [
    '#title' => t('Machine-readable name'),
    '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
    '#machine_name' => [],
    '#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'] += [
    'source' => [
      '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'],
  ];

  // Store the initial value in form state. The machine name needs this to
  // ensure that the exists function is not called for existing values when
  // editing them.
  $initial_values = $form_state
    ->get('machine_name.initial_values') ?: [];

  // Store the initial values in an array so we can differentiate between a
  // NULL default value and a new machine name element.
  if (!array_key_exists($element['#name'], $initial_values)) {
    $initial_values[$element['#name']] = $element['#default_value'];
    $form_state
      ->set('machine_name.initial_values', $initial_values);
  }

  // By default, machine names are restricted to Latin alphanumeric characters.
  // So, default to LTR directionality.
  if (!isset($element['#attributes'])) {
    $element['#attributes'] = [];
  }
  $element['#attributes'] += [
    '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 . '">&nbsp;</small>';
  }
  else {

    // Append a field suffix to the source form element, which will contain
    // the live preview of the machine name.
    $source += [
      '#field_suffix' => '',
    ];
    $source['#field_suffix'] = $source['#field_suffix'] . ' <small id="' . $suffix_id . '">&nbsp;</small>';
    $parents = array_merge($element['#machine_name']['source'], [
      '#field_suffix',
    ]);
    NestedArray::setValue($form_state
      ->getCompleteForm(), $parents, $source['#field_suffix']);
  }
  $element['#attached']['library'][] = 'core/drupal.machine-name';
  $options = [
    'replace_pattern',
    'replace_token',
    'replace',
    'maxlength',
    'target',
    'label',
    'field_prefix',
    'field_suffix',
    'suffix',
  ];

  /** @var \Drupal\Core\Access\CsrfTokenGenerator $token_generator */
  $token_generator = \Drupal::service('csrf_token');
  $element['#machine_name']['replace_token'] = $token_generator
    ->get($element['#machine_name']['replace_pattern']);
  $element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = array_intersect_key($element['#machine_name'], array_flip($options));
  $element['#attached']['drupalSettings']['langcode'] = $language
    ->getId();
  return $element;
}