class CustomFilterValidator in Custom filter 2.0.x
Prevents uninstallation of modules providing used filter plugins.
Hierarchy
- class \Drupal\filter\FilterUninstallValidator implements ModuleUninstallValidatorInterface uses StringTranslationTrait
- class \Drupal\customfilter\CustomFilterValidator
Expanded class hierarchy of CustomFilterValidator
1 file declares its use of CustomFilterValidator
- CustomFilterDeleteForm.php in src/
Form/ CustomFilterDeleteForm.php
1 string reference to 'CustomFilterValidator'
1 service uses CustomFilterValidator
File
- src/
CustomFilterValidator.php, line 11
Namespace
Drupal\customfilterView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CustomFilterValidator:: |
public | function | Clears all text format from using the given filter. | |
CustomFilterValidator:: |
public | function | Determines the reasons a filter can not be uninstalled. | |
FilterUninstallValidator:: |
protected | property | The filter plugin manager. | |
FilterUninstallValidator:: |
protected | property | The filter entity storage. | |
FilterUninstallValidator:: |
protected | function | Returns all enabled filter formats. | |
FilterUninstallValidator:: |
protected | function | Returns all filter definitions that are provided by the specified provider. | |
FilterUninstallValidator:: |
public | function |
Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface:: |
|
FilterUninstallValidator:: |
public | function | Constructs a new FilterUninstallValidator. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |