Commit 324ddcdf authored by catch's avatar catch
Browse files

Issue #3236446 by paulocs, benjifisher: Avoid duplicated code for the Modules...

Issue #3236446 by paulocs, benjifisher: Avoid duplicated code for the Modules form and the Modules Confirmation form
parent 7cc466d2
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\system\Form;

use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
@@ -57,4 +58,29 @@ protected function modulesEnabledConfirmationMessage(array $modules): PluralTran
    );
  }

  /**
   * Provides a fail message after attempt to install a module.
   *
   * @param string[] $modules
   *   Enabled module names, keyed by machine names.
   * @param Drupal\Core\Config\PreExistingConfigException $exception
   *   Exception thrown if configuration with the same name already exists.
   *
   * @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
   *   A confirmation message. If any of the enabled modules have permissions
   *   that the current user can manage, then include a link to the permissions
   *   page for those modules.
   */
  protected function modulesFailToEnableMessage(array $modules, PreExistingConfigException $exception): PluralTranslatableMarkup {
    $config_objects = $exception->flattenConfigObjects($exception->getConfigObjects());
    return $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' => $modules['install'][$exception->getExtension()],
      ]);
  }

}
+1 −11
Original line number Diff line number Diff line
@@ -175,17 +175,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
        $this->moduleInstaller->install(array_keys($this->modules['install']));
      }
      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' => $this->modules['install'][$e->getExtension()],
            ])
        );
        $this->messenger()->addError($this->modulesFailToEnableMessage($this->modules, $e));
        return;
      }
      catch (UnmetDependenciesException $e) {
+1 −11
Original line number Diff line number Diff line
@@ -477,17 +477,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
          ->addStatus($this->modulesEnabledConfirmationMessage($modules['install']));
      }
      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' => $modules['install'][$e->getExtension()],
            ])
        );
        $this->messenger()->addError($this->modulesFailToEnableMessage($modules, $e));
        return;
      }
      catch (UnmetDependenciesException $e) {