summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/src/Form/ModulesEnabledTrait.php
blob: d7d17ee1d3ae8a9e961b6ea1d2cba422d182953b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Drupal\system\Form;

use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;

/**
 * Provides helpers for enabling modules.
 *
 * @internal
 */
trait ModulesEnabledTrait {

  use StringTranslationTrait;

  /**
   * Gets the current user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The current user.
   */
  abstract protected function currentUser();

  /**
   * Provides a confirmation message after modules have been enabled.
   *
   * @param string[] $modules
   *   Enabled module names, keyed by machine names.
   *
   * @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 modulesEnabledConfirmationMessage(array $modules): PluralTranslatableMarkup {
    $machine_names = implode(',', array_keys($modules));
    $url = Url::fromRoute('user.admin_permissions.module', ['modules' => $machine_names]);
    $module_names = implode(', ', array_values($modules));
    $t_args = ['%name' => $module_names, '%names' => $module_names];

    if ($url->access($this->currentUser())) {
      return $this->formatPlural(
        count($modules),
        'Module %name has been installed. Configure <a href=":link">related permissions</a>.',
        '@count modules have been installed: %names. Configure <a href=":link">related permissions</a>.',
        $t_args + [':link' => $url->toString()]
      );
    }

    return $this->formatPlural(
      count($modules),
      'Module %name has been installed.',
      '@count modules have been installed: %names.',
      $t_args
    );
  }

  /**
   * 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()],
      ]);
  }

}