class LanguageConfiguration

Same name in other branches
  1. 9 core/modules/language/src/Element/LanguageConfiguration.php \Drupal\language\Element\LanguageConfiguration
  2. 8.9.x core/modules/language/src/Element/LanguageConfiguration.php \Drupal\language\Element\LanguageConfiguration
  3. 10 core/modules/language/src/Element/LanguageConfiguration.php \Drupal\language\Element\LanguageConfiguration

Defines an element for language configuration for a single field.

Hierarchy

Expanded class hierarchy of LanguageConfiguration

File

core/modules/language/src/Element/LanguageConfiguration.php, line 14

Namespace

Drupal\language\Element
View source
class LanguageConfiguration extends FormElementBase {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() {
        $class = static::class;
        return [
            '#input' => TRUE,
            '#tree' => TRUE,
            '#process' => [
                [
                    $class,
                    'processLanguageConfiguration',
                ],
            ],
        ];
    }
    
    /**
     * Process handler for the language_configuration form element.
     */
    public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) {
        $options = $element['#options'] ?? [];
        // Avoid validation failure since we are moving the '#options' key in the
        // nested 'language' select element.
        unset($element['#options']);
        
        /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */
        $default_config = $element['#default_value'];
        $element['langcode'] = [
            '#type' => 'select',
            '#title' => t('Default language'),
            '#options' => $options + static::getDefaultOptions(),
            '#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(),
            ]),
            '#default_value' => $default_config != NULL ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT,
        ];
        $element['language_alterable'] = [
            '#type' => 'checkbox',
            '#title' => t('Show language selector on create and edit pages'),
            '#default_value' => $default_config != NULL ? $default_config->isLanguageAlterable() : FALSE,
        ];
        // Add the entity type and bundle information to the form if they are set.
        // They will be used, in the submit handler, to generate the names of the
        // configuration entities that will store the settings and are a way to uniquely
        // identify the entity.
        $language = $form_state->get('language') ?: [];
        $language += [
            $element['#name'] => [
                'entity_type' => $element['#entity_information']['entity_type'],
                'bundle' => $element['#entity_information']['bundle'],
            ],
        ];
        $form_state->set('language', $language);
        // Do not add the submit callback for the language content settings page,
        // which is handled separately.
        if ($form['#form_id'] != 'language_content_settings_form') {
            // Determine where to attach the language_configuration element submit
            // handler.
            // @todo Form API: Allow form widgets/sections to declare #submit
            //   handlers.
            $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit';
            if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) {
                $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit';
            }
            elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
                $form['#submit'][] = 'language_configuration_element_submit';
            }
        }
        return $element;
    }
    
    /**
     * Returns the default options for the language configuration form element.
     *
     * @return array
     *   An array containing the default options.
     */
    protected static function getDefaultOptions() {
        $language_options = [
            LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", [
                '@language' => static::languageManager()->getDefaultLanguage()
                    ->getName(),
            ]),
            'current_interface' => t('Interface text language selected for page'),
            'authors_default' => t("Author's preferred language"),
        ];
        $languages = static::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
        foreach ($languages as $langcode => $language) {
            $language_options[$langcode] = $language->isLocked() ? t('- @name -', [
                '@name' => $language->getName(),
            ]) : $language->getName();
        }
        return $language_options;
    }
    
    /**
     * Wraps the language manager.
     *
     * @return \Drupal\Core\Language\LanguageManagerInterface
     */
    protected static function languageManager() {
        return \Drupal::languageManager();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FormElementBase::processAutocomplete public static function Adds autocomplete functionality to elements. 1
FormElementBase::processPattern public static function #process callback for #pattern form element property. 1
FormElementBase::validatePattern public static function #element_validate callback for #pattern form element property. 1
FormElementBase::valueCallback public static function Determines how user input is mapped to an element&#039;s #value property. Overrides FormElementInterface::valueCallback 17
LanguageConfiguration::getDefaultOptions protected static function Returns the default options for the language configuration form element.
LanguageConfiguration::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
LanguageConfiguration::languageManager protected static function Wraps the language manager.
LanguageConfiguration::processLanguageConfiguration public static function Process handler for the language_configuration form element.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::setAttributes public static function Sets a form element&#039;s class attribute. Overrides ElementInterface::setAttributes 2

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