"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/language/src/Form/LanguageAddForm.php" (26 Nov 2020, 6347 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "LanguageAddForm.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\language\Form;
    4 
    5 use Drupal\Core\Entity\EntityInterface;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\Core\Language\LanguageManager;
    8 use Drupal\Core\Url;
    9 use Drupal\language\Entity\ConfigurableLanguage;
   10 
   11 /**
   12  * Controller for language addition forms.
   13  *
   14  * @internal
   15  */
   16 class LanguageAddForm extends LanguageFormBase {
   17 
   18   /**
   19    * {@inheritdoc}
   20    */
   21   public function getFormId() {
   22     // @todo Remove in favor of base method.
   23     return 'language_admin_add_form';
   24   }
   25 
   26   /**
   27    * {@inheritdoc}
   28    */
   29   public function form(array $form, FormStateInterface $form_state) {
   30     $form['#title'] = $this->t('Add language');
   31 
   32     $predefined_languages = $this->languageManager->getStandardLanguageListWithoutConfigured();
   33 
   34     $predefined_languages['custom'] = $this->t('Custom language...');
   35     $predefined_default = $form_state->getValue('predefined_langcode', key($predefined_languages));
   36     $form['predefined_langcode'] = [
   37       '#type' => 'select',
   38       '#title' => $this->t('Language name'),
   39       '#default_value' => $predefined_default,
   40       '#options' => $predefined_languages,
   41     ];
   42     $form['predefined_submit'] = [
   43       '#type' => 'submit',
   44       '#value' => $this->t('Add language'),
   45       '#name' => 'add_language',
   46       '#limit_validation_errors' => [['predefined_langcode'], ['predefined_submit']],
   47       '#states' => [
   48         'invisible' => [
   49           'select#edit-predefined-langcode' => ['value' => 'custom'],
   50         ],
   51       ],
   52       '#validate' => ['::validatePredefined'],
   53       '#submit' => ['::submitForm', '::save'],
   54       '#button_type' => 'primary',
   55     ];
   56 
   57     $custom_language_states_conditions = [
   58       'select#edit-predefined-langcode' => ['value' => 'custom'],
   59     ];
   60     $form['custom_language'] = [
   61       '#type' => 'container',
   62       '#states' => [
   63         'visible' => $custom_language_states_conditions,
   64       ],
   65     ];
   66     $this->commonForm($form['custom_language']);
   67     $form['custom_language']['langcode']['#states'] = [
   68       'required' => $custom_language_states_conditions,
   69     ];
   70     $form['custom_language']['label']['#states'] = [
   71       'required' => $custom_language_states_conditions,
   72     ];
   73     $form['custom_language']['submit'] = [
   74       '#type' => 'submit',
   75       '#value' => $this->t('Add custom language'),
   76       '#name' => 'add_custom_language',
   77       '#validate' => ['::validateCustom'],
   78       '#submit' => ['::submitForm', '::save'],
   79     ];
   80 
   81     return $form;
   82   }
   83 
   84   /**
   85    * {@inheritdoc}
   86    */
   87   public function save(array $form, FormStateInterface $form_state) {
   88     parent::save($form, $form_state);
   89 
   90     $t_args = ['%language' => $this->entity->label(), '%langcode' => $this->entity->id()];
   91     $this->logger('language')->notice('The %language (%langcode) language has been created.', $t_args);
   92     $this->messenger()->addStatus($this->t('The language %language has been created and can now be used.', $t_args));
   93 
   94     if ($this->moduleHandler->moduleExists('block')) {
   95       // Tell the user they have the option to add a language switcher block
   96       // to their theme so they can switch between the languages.
   97       $this->messenger()->addStatus($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>.', [':block-admin' => Url::fromRoute('block.admin_display')->toString()]));
   98     }
   99     $form_state->setRedirectUrl($this->entity->toUrl('collection'));
  100   }
  101 
  102   /**
  103    * {@inheritdoc}
  104    */
  105   public function actions(array $form, FormStateInterface $form_state) {
  106     // No actions needed.
  107     return [];
  108   }
  109 
  110   /**
  111    * Validates the language addition form on custom language button.
  112    */
  113   public function validateCustom(array $form, FormStateInterface $form_state) {
  114     if ($form_state->getValue('predefined_langcode') == 'custom') {
  115       $langcode = $form_state->getValue('langcode');
  116       // Reuse the editing form validation routine if we add a custom language.
  117       $this->validateCommon($form['custom_language'], $form_state);
  118 
  119       if ($language = $this->languageManager->getLanguage($langcode)) {
  120         $form_state->setErrorByName('langcode', $this->t('The language %language (%langcode) already exists.', ['%language' => $language->getName(), '%langcode' => $langcode]));
  121       }
  122     }
  123     else {
  124       $form_state->setErrorByName('predefined_langcode', $this->t('Use the <em>Add language</em> button to save a predefined language.'));
  125     }
  126   }
  127 
  128   /**
  129    * Element specific validator for the Add language button.
  130    */
  131   public function validatePredefined($form, FormStateInterface $form_state) {
  132     $langcode = $form_state->getValue('predefined_langcode');
  133     if ($langcode == 'custom') {
  134       $form_state->setErrorByName('predefined_langcode', $this->t('Fill in the language details and save the language with <em>Add custom language</em>.'));
  135     }
  136     else {
  137       if ($language = $this->languageManager->getLanguage($langcode)) {
  138         $form_state->setErrorByName('predefined_langcode', $this->t('The language %language (%langcode) already exists.', ['%language' => $language->getName(), '%langcode' => $langcode]));
  139       }
  140     }
  141   }
  142 
  143   /**
  144    * {@inheritdoc}
  145    */
  146   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  147     $langcode = $form_state->getValue('predefined_langcode');
  148     if ($langcode == 'custom') {
  149       $langcode = $form_state->getValue('langcode');
  150       $label = $form_state->getValue('label');
  151       $direction = $form_state->getValue('direction');
  152     }
  153     else {
  154       $standard_languages = LanguageManager::getStandardLanguageList();
  155       $label = $standard_languages[$langcode][0];
  156       $direction = isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : ConfigurableLanguage::DIRECTION_LTR;
  157     }
  158     $entity->set('id', $langcode);
  159     $entity->set('label', $label);
  160     $entity->set('direction', $direction);
  161     // There is no weight on the edit form. Fetch all configurable languages
  162     // ordered by weight and set the new language to be placed after them.
  163     $languages = \Drupal::languageManager()->getLanguages(ConfigurableLanguage::STATE_CONFIGURABLE);
  164     $last_language = end($languages);
  165     $entity->setWeight($last_language->getWeight() + 1);
  166   }
  167 
  168 }