function ThemeExperimentalConfirmForm::submitForm

Same name in other branches
  1. 9 core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm::submitForm()
  2. 8.9.x core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm::submitForm()
  3. 11.x core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm::submitForm()

Overrides FormInterface::submitForm

File

core/modules/system/src/Form/ThemeExperimentalConfirmForm.php, line 135

Class

ThemeExperimentalConfirmForm
Builds a confirmation form for enabling experimental themes.

Namespace

Drupal\system\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $args = $form_state->getBuildInfo()['args'];
    $theme = $args[0] ?? NULL;
    $set_default = $args[1] ?? FALSE;
    $themes = $this->themeList
        ->getList();
    $config = $this->configFactory()
        ->getEditable('system.theme');
    try {
        if ($this->themeInstaller
            ->install([
            $theme,
        ])) {
            if ($set_default) {
                // Set the default theme.
                $config->set('default', $theme)
                    ->save();
                // The status message depends on whether an admin theme is currently
                // in use: an empty string means the admin theme is set to be the
                // default theme.
                $admin_theme = $config->get('admin');
                if (!empty($admin_theme) && $admin_theme !== $theme) {
                    $this->messenger()
                        ->addStatus($this->t('Note that the administration theme is still set to the %admin_theme theme; consequently, the theme on this page remains unchanged. All non-administrative sections of the site, however, will show the selected %selected_theme theme by default.', [
                        '%admin_theme' => $themes[$admin_theme]->info['name'],
                        '%selected_theme' => $themes[$theme]->info['name'],
                    ]));
                }
                else {
                    $this->messenger()
                        ->addStatus($this->t('%theme is now the default theme.', [
                        '%theme' => $themes[$theme]->info['name'],
                    ]));
                }
            }
            else {
                $this->messenger()
                    ->addStatus($this->t('The %theme theme has been installed.', [
                    '%theme' => $themes[$theme]->info['name'],
                ]));
            }
        }
        else {
            $this->messenger()
                ->addError($this->t('The %theme theme was not found.', [
                '%theme' => $theme,
            ]));
        }
    } catch (PreExistingConfigException $e) {
        $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
        $this->messenger()
            ->addError($this->formatPlural(count($config_objects), 'Unable to install @extension, %config_names already exists in active configuration.', 'Unable to install @extension, %config_names already exist in active configuration.', [
            '%config_names' => implode(', ', $config_objects),
            '@extension' => $theme,
        ]));
    } catch (UnmetDependenciesException $e) {
        $this->messenger()
            ->addError($e->getTranslatedMessage($this->getStringTranslation(), $theme));
    }
    $form_state->setRedirectUrl($this->getCancelUrl());
}

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