Repository URL to install this package:
|
Version:
1.0.1 ▾
|
<?php
namespace Drupal\dds_dynamic_key_value;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Drupal\Core\Url;
/**
* Class DynamicKeyValueStorage.
*
* @package Drupal\dds_maps
*/
class DynamicKeyValueStorage {
/**
* Variable holding the key-value factory.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactory
*/
private $keyValue;
/**
* YamlConfigStorage constructor.
*
* @param \Drupal\Core\KeyValueStore\KeyValueFactory $keyValueFactory
* The key value factory.
*/
public function __construct(KeyValueFactory $keyValueFactory) {
$this->keyValue = $keyValueFactory;
}
/**
* Save the configuration into the key-value store.
*
* @param array $values
* The values to save.
*/
public function save(array $values): void {
foreach ($values as $key => $value) {
$this->keyValue->get('dynamic_key_value')->set($key, $value);
}
}
/**
* The the complete configuration for key.
*
* @param string $key
* The key to get the config for.
*
* @return array
* The complete configuration for the provided key.
*/
public function getCollection(string $key): array {
$collection = $this->keyValue->get('global_config')->get($key);
foreach ($collection as $key => $item) {
// if ($item['type'] === 'asset' && isset($item['uri'])) {
// $collection[$key]['url'] = file_create_url($item['uri']);
// kint($collection);
// }
}
return $collection;
}
/**
* Get a specific configuration.
*
* @param string $key
* The key to get the config from.
* @param string $name
* The name of the config to retrieve.
*
* @return mixed|null
* The value of the config.
*/
public function get(string $key, string $name) {
$configs = $this->getCollection($key);
if (isset($configs[$name])) {
return $configs[$name];
}
return NULL;
}
/**
* Get the names of the collections.
*
* @return array
* Array containing the names of all the collections.
*/
public function getCollectionLabels(): array {
$collections = $this->keyValue->get('dynamic_key_value')->getAll();
if (!empty($collections)) {
$keys = array_keys($collections);
return array_combine($keys, $keys);
}
return [];
}
/**
* Retrieve config as key-value for use in ex. select field.
*
* @param $key
* The key of the collection to retrieve.
*
* @return array
* Array containing the options names
*/
public function getOptions($key): array {
if (is_string($key)) {
$collection = $this->getCollection($key);
$config = [];
if (!empty($collection)) {
foreach ($collection as $collection_key => $value) {
$config[$collection_key] = $value['name'];
}
}
return $config;
}
return [];
}
/**
* Get config that needs to bee exposed through drupalSettings.
*
* @param $key
* The key of the collection to retrieve.
*
* @return array
* Array containing the settings that needs to be exposed to js.
*/
public function getJSOptions($key) {
if (is_string($key)) {
$collection = $this->getCollection($key);
$config = [];
if (!empty($collection)) {
foreach ($collection as $collection_key => $value) {
if(isset($value['js']) && $value['js'] === TRUE) {
unset($value['js']);
$config[$collection_key] = $value;
}
}
}
return $config;
}
return [];
}
}