You are here

public static function MachineName::processMachineName in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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.

File

core/lib/Drupal/Core/Render/Element/MachineName.php, line 128
Contains \Drupal\Core\Render\Element\MachineName.

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 += 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 . '">&nbsp;</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 . '">&nbsp;</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';
  $element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = $element['#machine_name'];
  $element['#attached']['drupalSettings']['langcode'] = $language
    ->getId();
  return $element;
}