"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/language/src/Element/LanguageConfiguration.php" (26 Nov 2020, 4525 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 "LanguageConfiguration.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\language\Element;
    4 
    5 use Drupal\Core\Url;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\Core\Language\LanguageInterface;
    8 use Drupal\Core\Render\Element\FormElement;
    9 
   10 /**
   11  * Defines an element for language configuration for a single field.
   12  *
   13  * @FormElement("language_configuration")
   14  */
   15 class LanguageConfiguration extends FormElement {
   16 
   17   /**
   18    * {@inheritdoc}
   19    */
   20   public function getInfo() {
   21     $class = get_class($this);
   22     return [
   23       '#input' => TRUE,
   24       '#tree' => TRUE,
   25       '#process' => [
   26         [$class, 'processLanguageConfiguration'],
   27       ],
   28     ];
   29   }
   30 
   31   /**
   32    * Process handler for the language_configuration form element.
   33    */
   34   public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) {
   35     $options = isset($element['#options']) ? $element['#options'] : [];
   36     // Avoid validation failure since we are moving the '#options' key in the
   37     // nested 'language' select element.
   38     unset($element['#options']);
   39     /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */
   40     $default_config = $element['#default_value'];
   41     $element['langcode'] = [
   42       '#type' => 'select',
   43       '#title' => t('Default language'),
   44       '#options' => $options + static::getDefaultOptions(),
   45       '#description' => t('Explanation of the language options is found on the <a href=":languages_list_page">languages list page</a>.', [':languages_list_page' => Url::fromRoute('entity.configurable_language.collection')->toString()]),
   46       '#default_value' => ($default_config != NULL) ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT,
   47     ];
   48 
   49     $element['language_alterable'] = [
   50       '#type' => 'checkbox',
   51       '#title' => t('Show language selector on create and edit pages'),
   52       '#default_value' => ($default_config != NULL) ? $default_config->isLanguageAlterable() : FALSE,
   53     ];
   54 
   55     // Add the entity type and bundle information to the form if they are set.
   56     // They will be used, in the submit handler, to generate the names of the
   57     // configuration entities that will store the settings and are a way to uniquely
   58     // identify the entity.
   59     $language = $form_state->get('language') ?: [];
   60     $language += [
   61       $element['#name'] => [
   62         'entity_type' => $element['#entity_information']['entity_type'],
   63         'bundle' => $element['#entity_information']['bundle'],
   64       ],
   65     ];
   66     $form_state->set('language', $language);
   67 
   68     // Do not add the submit callback for the language content settings page,
   69     // which is handled separately.
   70     if ($form['#form_id'] != 'language_content_settings_form') {
   71       // Determine where to attach the language_configuration element submit
   72       // handler.
   73       // @todo Form API: Allow form widgets/sections to declare #submit
   74       //   handlers.
   75       $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit';
   76       if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) {
   77         $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit';
   78       }
   79       elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
   80         $form['#submit'][] = 'language_configuration_element_submit';
   81       }
   82     }
   83     return $element;
   84   }
   85 
   86   /**
   87    * Returns the default options for the language configuration form element.
   88    *
   89    * @return array
   90    *   An array containing the default options.
   91    */
   92   protected static function getDefaultOptions() {
   93     $language_options = [
   94       LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", ['@language' => static::languageManager()->getDefaultLanguage()->getName()]),
   95       'current_interface' => t('Interface text language selected for page'),
   96       'authors_default' => t("Author's preferred language"),
   97     ];
   98 
   99     $languages = static::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
  100     foreach ($languages as $langcode => $language) {
  101       $language_options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
  102     }
  103 
  104     return $language_options;
  105   }
  106 
  107   /**
  108    * Wraps the language manager.
  109    *
  110    * @return \Drupal\Core\Language\LanguageManagerInterface
  111    */
  112   protected static function languageManager() {
  113     return \Drupal::languageManager();
  114   }
  115 
  116 }