CustomFilterValidator.php in Custom filter 2.0.x
File
src/CustomFilterValidator.php
View source
<?php
namespace Drupal\customfilter;
use Drupal\customfilter\Entity\CustomFilter;
use Drupal\filter\FilterUninstallValidator;
class CustomFilterValidator extends FilterUninstallValidator {
public function validateFilter(CustomFilter $filter) {
$needle = 'customfilter_' . $filter
->id();
$used_in = [];
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;
}
public function clearFilters(array $customfilters) {
$used_in = [];
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;
}
}