View source
<?php
use Drupal\Core\Template\Attribute;
use Drupal\flags\Mapping\FlagMappingInterface;
function flags_theme($existing, $type, $theme, $path) {
return array(
'flags' => array(
'variables' => array(
'code' => null,
'source' => $type,
'tag' => 'span',
'attributes' => array(),
),
),
);
}
function template_preprocess_flags(&$variables) {
$flag = $variables['code'];
$serviceName = 'flags.mapping.' . $variables['source'];
if (\Drupal::hasService($serviceName)) {
$flag = \Drupal::service($serviceName)
->map($flag);
$extra = \Drupal::service($serviceName)
->getExtraClasses();
}
else {
throw new \InvalidArgumentException(sprintf('Service %s to map source "%s" is not defined.', $serviceName, $variables['source']));
}
$variables['flag'] = $flag;
if (empty($variables['attributes'])) {
$variables['attributes'] = new Attribute();
}
if (empty($variables['attributes']['class'])) {
$variables['attributes']['class'] = array();
}
$variables['attributes']['class'][] = 'flag';
$variables['attributes']['class'][] = 'flag-' . $flag;
if (!empty($extra)) {
$variables['attributes']
->addClass($extra);
}
unset($variables['source']);
}