Created
September 20, 2023 08:25
-
-
Save andybroomfield/ddf9a8bb80974e7d4a3370331211e53f to your computer and use it in GitHub Desktop.
Eventsubscriber to sort text format filters alphapetically in config (still sorted by weight in the filter format itself)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
filter_format_config_sort.filter_subscriber: | |
class: Drupal\filter_format_config_sort\EventSubscriber\FilterConfigSubscriber | |
arguments: ['@config.storage', '@config.storage.sync'] | |
tags: | |
- { name: event_subscriber } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\filter_format_config_sort\EventSubscriber; | |
use Drupal\Core\Config\ConfigEvents; | |
use Drupal\Core\Config\StorageInterface; | |
use Drupal\Core\Config\StorageTransformEvent; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
/** | |
* Text format filter config event subscriber. | |
*/ | |
class FilterConfigSubscriber implements EventSubscriberInterface { | |
/** | |
* The active config storage. | |
* | |
* @var \Drupal\Core\Config\StorageInterface | |
*/ | |
protected $activeStorage; | |
/** | |
* The sync config storage. | |
* | |
* @var \Drupal\Core\Config\StorageInterface | |
*/ | |
protected $syncStorage; | |
/** | |
* DirectoriesConfigSubscriber constructor. | |
* | |
* @param \Drupal\Core\Config\StorageInterface $config_storage | |
* The config active storage. | |
* @param \Drupal\Core\Config\StorageInterface $sync_storage | |
* The sync config storage. | |
*/ | |
public function __construct(StorageInterface $config_storage, StorageInterface $sync_storage) { | |
$this->activeStorage = $config_storage; | |
$this->syncStorage = $sync_storage; | |
} | |
/** | |
* The storage is transformed for importing. | |
* | |
* @param \Drupal\Core\Config\StorageTransformEvent $event | |
* The config storage transform event. | |
*/ | |
public function onImportTransform(StorageTransformEvent $event) { | |
$transformation_storage = $event->getStorage(); | |
foreach ($transformation_storage->listAll() as $config_name) { | |
if (strpos($config_name, 'filter.format.') === 0) { | |
$config = $transformation_storage->read($config_name); | |
$new_config = $config; | |
ksort($new_config['filters']); | |
// Note: write direct to active storage instead of transform_storage. | |
// Not sure if best idea, but seems only way to get the order to stick. | |
$this->activeStorage->write($config_name, $new_config); | |
} | |
} | |
} | |
/** | |
* The storage is transformed for exporting. | |
* | |
* @param \Drupal\Core\Config\StorageTransformEvent $event | |
* The config storage transform event. | |
*/ | |
public function onExportTransform(StorageTransformEvent $event) { | |
$transformation_storage = $event->getStorage(); | |
foreach ($transformation_storage->listAll() as $config_name) { | |
if (strpos($config_name, 'filter.format.') === 0) { | |
$config = $transformation_storage->read($config_name); | |
$new_config = $config; | |
ksort($new_config['filters']); | |
$transformation_storage->write($config_name, $new_config); | |
} | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() { | |
$events[ConfigEvents::STORAGE_TRANSFORM_IMPORT][] = ['onImportTransform']; | |
$events[ConfigEvents::STORAGE_TRANSFORM_EXPORT][] = ['onExportTransform']; | |
return $events; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment