abstract class BaseMapping in Flags 8
Provides generic mapping service to map values to flags using config entities.
Class BaseMapping
Hierarchy
- class \Drupal\flags\Mapping\BaseMapping implements FlagMappingInterface
Expanded class hierarchy of BaseMapping
1 file declares its use of BaseMapping
- Language.php in src/
Mapping/ Language.php
File
- src/
Mapping/ BaseMapping.php, line 14
Namespace
Drupal\flags\MappingView source
abstract class BaseMapping implements FlagMappingInterface {
/**
* @var ImmutableConfig[]
*/
protected $config;
/**
* @var string[]
*/
protected $extraClasses = [];
/**
* Gets config key that holds list of mapping entities.
*
* @return string
*/
protected abstract function getConfigKey();
/**
* Creates new instance of BaseMapping class.
*
* @param ConfigFactoryInterface $config
*/
public function __construct(ConfigFactoryInterface $config) {
$ids = $config
->listAll($this
->getConfigKey());
$this->config = $config
->loadMultiple($ids);
}
/**
* {@inheritdoc}
*/
function map($value) {
// Unify input data
$code = trim(strtolower($value));
$key = $this
->getConfigKey() . '.' . $code;
if (isset($this->config[$key])) {
// We make sure that flag is lowercase to match our CSS.
return strtolower($this->config[$key]
->get('flag'));
}
return $code;
}
/**
* {@inheritDoc}
*/
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;
}
/**
* {@inheritDoc}
*/
public function getExtraClasses() {
return $this->extraClasses;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseMapping:: |
protected | property | ||
BaseMapping:: |
protected | property | 2 | |
BaseMapping:: |
abstract protected | function | Gets config key that holds list of mapping entities. | 2 |
BaseMapping:: |
public | function |
Returns array of extra classes for specific mapper. Overrides FlagMappingInterface:: |
|
BaseMapping:: |
public | function |
Gets array with attributes for each option element. Overrides FlagMappingInterface:: |
|
BaseMapping:: |
function |
Maps provided string to a flag code.
Returned string should be lower case flag code. Overrides FlagMappingInterface:: |
||
BaseMapping:: |
public | function | Creates new instance of BaseMapping class. |