function LanguageAddForm::form

Same name in other branches
  1. 9 core/modules/language/src/Form/LanguageAddForm.php \Drupal\language\Form\LanguageAddForm::form()
  2. 8.9.x core/modules/language/src/Form/LanguageAddForm.php \Drupal\language\Form\LanguageAddForm::form()
  3. 10 core/modules/language/src/Form/LanguageAddForm.php \Drupal\language\Form\LanguageAddForm::form()

Overrides EntityForm::form

File

core/modules/language/src/Form/LanguageAddForm.php, line 29

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'] = [
        '#type' => 'select',
        '#title' => $this->t('Language name'),
        '#default_value' => $predefined_default,
        '#options' => $predefined_languages,
    ];
    $form['predefined_submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Add language'),
        '#name' => 'add_language',
        '#limit_validation_errors' => [
            [
                'predefined_langcode',
            ],
            [
                'predefined_submit',
            ],
        ],
        '#states' => [
            'invisible' => [
                'select#edit-predefined-langcode' => [
                    'value' => 'custom',
                ],
            ],
        ],
        '#validate' => [
            '::validatePredefined',
        ],
        '#submit' => [
            '::submitForm',
            '::save',
        ],
        '#button_type' => 'primary',
    ];
    $custom_language_states_conditions = [
        'select#edit-predefined-langcode' => [
            'value' => 'custom',
        ],
    ];
    $form['custom_language'] = [
        '#type' => 'container',
        '#states' => [
            'visible' => $custom_language_states_conditions,
        ],
    ];
    $this->commonForm($form['custom_language']);
    $form['custom_language']['langcode']['#states'] = [
        'required' => $custom_language_states_conditions,
    ];
    $form['custom_language']['label']['#states'] = [
        'required' => $custom_language_states_conditions,
    ];
    $form['custom_language']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Add custom language'),
        '#name' => 'add_custom_language',
        '#validate' => [
            '::validateCustom',
        ],
        '#submit' => [
            '::submitForm',
            '::save',
        ],
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.