public function CountryFlagFormatter::viewElements in Flags 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides CountryDefaultFormatter::viewElements
File
- flags_country/
src/ Plugin/ Field/ FieldFormatter/ CountryFlagFormatter.php, line 81
Class
- CountryFlagFormatter
- Plugin implementation of the 'country' formatter.
Namespace
Drupal\flags_country\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$countries = \Drupal::service('country.field.manager')
->getSelectableCountries($this->fieldDefinition);
$elements = parent::viewElements($items, $langcode);
$format = $this
->getSetting('flag_display');
$attributes = new Attribute(array(
'class' => array(
$format,
),
));
foreach ($items as $delta => $item) {
if (isset($countries[$item->value])) {
unset($elements[$delta]['#markup']);
if ('flag-instead' != $format) {
$elements[$delta]['country'] = array(
'#markup' => $countries[$item->value],
);
}
$elements[$delta]['flag'] = array(
'#theme' => 'flags',
'#code' => strtolower($item->value),
'#attributes' => clone $attributes,
'#source' => 'country',
);
}
$elements[$delta]['#prefix'] = '<div class="field__flags__item">';
$elements[$delta]['#suffix'] = '</div>';
}
return $elements;
}