You are here

class CustomFilterValidator in Custom filter 2.0.x

Prevents uninstallation of modules providing used filter plugins.

Hierarchy

Expanded class hierarchy of CustomFilterValidator

1 file declares its use of CustomFilterValidator
CustomFilterDeleteForm.php in src/Form/CustomFilterDeleteForm.php
1 string reference to 'CustomFilterValidator'
customfilter.services.yml in ./customfilter.services.yml
customfilter.services.yml
1 service uses CustomFilterValidator
customfilter.validator in ./customfilter.services.yml
Drupal\customfilter\CustomFilterValidator

File

src/CustomFilterValidator.php, line 11

Namespace

Drupal\customfilter
View source
class CustomFilterValidator extends FilterUninstallValidator {

  /**
   * Determines the reasons a filter can not be uninstalled.
   *
   * @param \Drupal\customfilter\Entity\CustomFilter $filter
   *   A custom filter.
   *
   * @return string|null
   *   The reason the filter can not be uninstalled, NULL if it can.
   */
  public function validateFilter(CustomFilter $filter) {
    $needle = 'customfilter_' . $filter
      ->id();
    $used_in = [];

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

  /**
   * Clears all text format from using the given filter.
   *
   * @param \Drupal\customfilter\Entity\CustomFilter[] $customfilters
   *   A custom filter array.
   *
   * @return \Drupal\filter\Entity\FilterFormat[]
   *   The filter formats the custom filter as been removed from.
   */
  public function clearFilters(array $customfilters) {
    $used_in = [];

    // Find out if any filter formats have the plugin enabled.
    foreach ($this
      ->getEnabledFilterFormats() as $filter_format) {
      $filters = $filter_format
        ->filters();
      foreach ($customfilters as $customfilter) {
        $needle = 'customfilter_' . $customfilter
          ->id();
        if ($filters
          ->has($needle) && $filters
          ->get($needle)->status) {
          $used_in[] = $filter_format;
          $filter_format
            ->removeFilter($needle);
          $filter_format
            ->save();
        }
      }
    }
    return $used_in;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomFilterValidator::clearFilters public function Clears all text format from using the given filter.
CustomFilterValidator::validateFilter public function Determines the reasons a filter can not be uninstalled.
FilterUninstallValidator::$filterManager protected property The filter plugin manager.
FilterUninstallValidator::$filterStorage protected property The filter entity storage.
FilterUninstallValidator::getEnabledFilterFormats protected function Returns all enabled filter formats.
FilterUninstallValidator::getFilterDefinitionsByProvider protected function Returns all filter definitions that are provided by the specified provider.
FilterUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
FilterUninstallValidator::__construct public function Constructs a new FilterUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.