BaseMapping.php in Flags 8
File
src/Mapping/BaseMapping.php
View source
<?php
namespace Drupal\flags\Mapping;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Template\Attribute;
abstract class BaseMapping implements FlagMappingInterface {
protected $config;
protected $extraClasses = [];
protected abstract function getConfigKey();
public function __construct(ConfigFactoryInterface $config) {
$ids = $config
->listAll($this
->getConfigKey());
$this->config = $config
->loadMultiple($ids);
}
function map($value) {
$code = trim(strtolower($value));
$key = $this
->getConfigKey() . '.' . $code;
if (isset($this->config[$key])) {
return strtolower($this->config[$key]
->get('flag'));
}
return $code;
}
public function getOptionAttributes(array $options = []) {
$attributes = [];
foreach ($options as $key) {
$classes = [
'flag',
'flag-' . strtolower($this
->map($key)),
];
$classes = array_merge($this
->getExtraClasses(), $classes);
$attributes[$key] = new Attribute([
'data-class' => $classes,
]);
}
return $attributes;
}
public function getExtraClasses() {
return $this->extraClasses;
}
}
Classes
Name |
Description |
BaseMapping |
Provides generic mapping service to map values to flags using config entities. |