You are here

public function LanguageAddForm::form in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/Form/LanguageAddForm.php \Drupal\language\Form\LanguageAddForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/language/src/Form/LanguageAddForm.php, line 31
Contains \Drupal\language\Form\LanguageAddForm.

Class

LanguageAddForm
Controller for language addition forms.

Namespace

Drupal\language\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form['#title'] = $this
    ->t('Add language');
  $predefined_languages = $this->languageManager
    ->getStandardLanguageListWithoutConfigured();
  $predefined_languages['custom'] = $this
    ->t('Custom language...');
  $predefined_default = $form_state
    ->getValue('predefined_langcode', key($predefined_languages));
  $form['predefined_langcode'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Language name'),
    '#default_value' => $predefined_default,
    '#options' => $predefined_languages,
  );
  $form['predefined_submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Add language'),
    '#name' => 'add_language',
    '#limit_validation_errors' => array(
      array(
        'predefined_langcode',
      ),
      array(
        'predefined_submit',
      ),
    ),
    '#states' => array(
      'invisible' => array(
        'select#edit-predefined-langcode' => array(
          'value' => 'custom',
        ),
      ),
    ),
    '#validate' => array(
      '::validatePredefined',
    ),
    '#submit' => array(
      '::submitForm',
      '::save',
    ),
    '#button_type' => 'primary',
  );
  $custom_language_states_conditions = array(
    'select#edit-predefined-langcode' => array(
      'value' => 'custom',
    ),
  );
  $form['custom_language'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => $custom_language_states_conditions,
    ),
  );
  $this
    ->commonForm($form['custom_language']);
  $form['custom_language']['langcode']['#states'] = array(
    'required' => $custom_language_states_conditions,
  );
  $form['custom_language']['label']['#states'] = array(
    'required' => $custom_language_states_conditions,
  );
  $form['custom_language']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Add custom language'),
    '#name' => 'add_custom_language',
    '#validate' => array(
      '::validateCustom',
    ),
    '#submit' => array(
      '::submitForm',
      '::save',
    ),
  );
  return $form;
}