trait ModulesEnabledTrait

Same name in other branches
  1. 10 core/modules/system/src/Form/ModulesEnabledTrait.php \Drupal\system\Form\ModulesEnabledTrait
  2. 11.x core/modules/system/src/Form/ModulesEnabledTrait.php \Drupal\system\Form\ModulesEnabledTrait

Provides helpers for enabling modules.

@internal

Hierarchy

File

core/modules/system/src/Form/ModulesEnabledTrait.php, line 15

Namespace

Drupal\system\Form
View source
trait ModulesEnabledTrait {
    use StringTranslationTrait;
    
    /**
     * Gets the current user.
     *
     * @return \Drupal\Core\Session\AccountInterface
     *   The current user.
     */
    protected abstract 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 enabled. Configure <a href=":link">related permissions</a>.', '@count modules have been enabled: %names. Configure <a href=":link">related permissions</a>.', $t_args + [
                ':link' => $url->toString(),
            ]);
        }
        return $this->formatPlural(count($modules), 'Module %name has been enabled.', '@count modules have been enabled: %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()],
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ModulesEnabledTrait::currentUser abstract protected function Gets the current user.
ModulesEnabledTrait::modulesEnabledConfirmationMessage protected function Provides a confirmation message after modules have been enabled.
ModulesEnabledTrait::modulesFailToEnableMessage protected function Provides a fail message after attempt to install a module.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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