You are here

public function FilterUninstallValidator::validate in Drupal 9

Same name in this branch
  1. 9 core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator::validate()
  2. 9 core/modules/filter/src/ProxyClass/FilterUninstallValidator.php \Drupal\filter\ProxyClass\FilterUninstallValidator::validate()
Same name and namespace in other branches
  1. 8 core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator::validate()
  2. 10 core/modules/filter/src/FilterUninstallValidator.php \Drupal\filter\FilterUninstallValidator::validate()

Determines the reasons a module can not be uninstalled.

Parameters

string $module: A module name.

Return value

string[] An array of reasons the module can not be uninstalled, empty if it can. Each reason should not end with any punctuation since multiple reasons can be displayed together.

Overrides ModuleUninstallValidatorInterface::validate

See also

template_preprocess_system_modules_uninstall()

File

core/modules/filter/src/FilterUninstallValidator.php, line 51

Class

FilterUninstallValidator
Prevents uninstallation of modules providing used filter plugins.

Namespace

Drupal\filter

Code

public function validate($module) {
  $reasons = [];

  // Get filter plugins supplied by this module.
  if ($filter_plugins = $this
    ->getFilterDefinitionsByProvider($module)) {
    $used_in = [];

    // Find out if any filter formats have the plugin enabled.
    foreach ($this
      ->getEnabledFilterFormats() as $filter_format) {
      $filters = $filter_format
        ->filters();
      foreach ($filter_plugins as $filter_plugin) {
        if ($filters
          ->has($filter_plugin['id']) && $filters
          ->get($filter_plugin['id'])->status) {
          $used_in[] = $filter_format
            ->label();
          break;
        }
      }
    }
    if (!empty($used_in)) {
      $reasons[] = $this
        ->t('Provides a filter plugin that is in use in the following filter formats: %formats', [
        '%formats' => implode(', ', $used_in),
      ]);
    }
  }
  return $reasons;
}