LanguageAddForm.php 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

/**
 * @file
 * Contains \Drupal\language\Form\LanguageAddForm.
 */

namespace Drupal\language\Form;

use Drupal\language\Form\LanguageFormBase;
use Drupal\Core\Language\Language;

/**
 * Controller for language addition forms.
 */
class LanguageAddForm extends LanguageFormBase {

  /**
   * {@inheritdoc}
   */
21
  public function getFormId() {
22 23 24 25 26 27 28 29 30 31
    // @todo Remove in favour of base method.
    return 'language_admin_add_form';
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, array &$form_state) {
    $form['#title'] = $this->t('Add language');

32
    $predefined_languages = $this->languageManager->getUnusedPredefinedList();
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    $predefined_languages['custom'] = $this->t('Custom language...');
    $predefined_default = !empty($form_state['values']['predefined_langcode']) ? $form_state['values']['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'),
      '#limit_validation_errors' => array(array('predefined_langcode'), array('predefined_submit')),
      '#states' => array(
        'invisible' => array(
          'select#edit-predefined-langcode' => array('value' => 'custom'),
        ),
      ),
      '#validate' => array(array($this, 'validatePredefined')),
      '#submit' => array(array($this, 'submitForm')),
    );

55 56 57
    $custom_language_states_conditions = array(
      'select#edit-predefined-langcode' => array('value' => 'custom'),
    );
58 59 60
    $form['custom_language'] = array(
      '#type' => 'container',
      '#states' => array(
61
        'visible' => $custom_language_states_conditions,
62 63 64
      ),
    );
    $this->commonForm($form['custom_language']);
65 66 67 68 69 70
    $form['custom_language']['langcode']['#states'] = array(
      'required' => $custom_language_states_conditions,
    );
    $form['custom_language']['name']['#states'] = array(
      'required' => $custom_language_states_conditions,
    );
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    $form['custom_language']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Add custom language'),
      '#validate' => array(array($this, 'validateCustom')),
      '#submit' => array(array($this, 'submitForm')),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $langcode = $form_state['values']['predefined_langcode'];
    if ($langcode == 'custom') {
      $langcode = $form_state['values']['langcode'];
      // Custom language form.
      $language = new Language(array(
        'id' => $langcode,
        'name' => $form_state['values']['name'],
        'direction' => $form_state['values']['direction'],
      ));
    }
    else {
      $language = new Language(array('id' => $langcode));
    }
    // Save the language and inform the user that it happened.
    $language = language_save($language);
100

101 102 103 104 105
    drupal_set_message($this->t('The language %language has been created and can now be used.', array('%language' => $language->name)));

    // Tell the user they have the option to add a language switcher block
    // to their theme so they can switch between the languages.
    drupal_set_message($this->t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href="@block-admin">block administration page</a>.', array('@block-admin' => url('admin/structure/block'))));
106
    $form_state['redirect_route']['route_name'] = 'language.admin_overview';
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }

  /**
   * {@inheritdoc}
   */
  public function actions(array $form, array &$form_state) {
    // No actions needed.
    return array();
  }

  /**
   * Validates the language addition form on custom language button.
   */
  public function validateCustom(array $form, array &$form_state) {
    if ($form_state['values']['predefined_langcode'] == 'custom') {
      $langcode = $form_state['values']['langcode'];
      // Reuse the editing form validation routine if we add a custom language.
      $this->validateCommon($form['custom_language'], $form_state);

      if ($language = language_load($langcode)) {
127
        $this->setFormError('langcode', $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
128 129 130
      }
    }
    else {
131
      $this->setFormError('predefined_langcode', $form_state, $this->t('Use the <em>Add language</em> button to save a predefined language.'));
132 133 134 135 136 137 138 139 140
    }
  }

  /**
   * Element specific validator for the Add language button.
   */
  public function validatePredefined($form, &$form_state) {
    $langcode = $form_state['values']['predefined_langcode'];
    if ($langcode == 'custom') {
141
      $this->setFormError('predefined_langcode', $form_state, $this->t('Fill in the language details and save the language with <em>Add custom language</em>.'));
142 143 144
    }
    else {
      if ($language = language_load($langcode)) {
145
        $this->setFormError('predefined_langcode', $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
146 147 148 149 150
      }
    }
  }

}