TestSplitFilter.php in Config Filter 8
File
tests/modules/config_filter_split_test/src/Plugin/ConfigFilter/TestSplitFilter.php
View source
<?php
namespace Drupal\config_filter_split_test\Plugin\ConfigFilter;
use Drupal\config_filter\Plugin\ConfigFilterBase;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TestSplitFilter extends ConfigFilterBase implements ContainerFactoryPluginInterface {
protected $storage;
protected $name;
public function __construct(StorageInterface $storage, $name) {
parent::__construct([], 'config_filter_split_test', []);
$this->storage = $storage;
$this->name = $name;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(new DatabaseStorage($container
->get('database'), 'config_filter_split_test'), 'core.');
}
protected function isSplitConfig($name) {
return strpos($name, $this->name) === 0;
}
public function filterRead($name, $data) {
if ($this
->isSplitConfig($name)) {
if ($this->storage
->exists($name)) {
$data = $this->storage
->read($name);
}
}
return $data;
}
public function filterExists($name, $exists) {
if ($this
->isSplitConfig($name) && !$exists) {
$exists = $this->storage
->exists($name);
}
return $exists;
}
public function filterReadMultiple(array $names, array $data) {
return array_merge($data, $this->storage
->readMultiple($names));
}
public function filterListAll($prefix, array $data) {
return array_unique(array_merge($data, $this->storage
->listAll($prefix)));
}
public function filterWrite($name, array $data) {
if ($this
->isSplitConfig($name)) {
$this->storage
->write($name, $data);
return NULL;
}
return $data;
}
public function filterWriteEmptyIsDelete($name) {
return $this
->isSplitConfig($name) ? TRUE : NULL;
}
public function filterDelete($name, $delete) {
if ($delete && $this->storage
->exists($name)) {
$this->storage
->delete($name);
}
return $delete;
}
public function filterDeleteAll($prefix, $delete) {
if ($delete && $this->storage) {
try {
$this->storage
->deleteAll($prefix);
} catch (\UnexpectedValueException $exception) {
}
}
return $delete;
}
public function filterCreateCollection($collection) {
return new static($this->storage
->createCollection($collection), $this->name);
}
}