LanguageFlagFormatter.php in Flags 8
File
flags_language/src/Plugin/Field/FieldFormatter/LanguageFlagFormatter.php
View source
<?php
namespace Drupal\flags_language\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter as BaseFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
class LanguageFlagFormatter extends BaseFormatter {
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['flag_display'] = 'flag-before';
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['flag_display'] = array(
'#type' => 'select',
'#title' => $this
->t('Output format'),
'#default_value' => $this
->getSetting('flag_display'),
'#options' => $this
->getOutputFormats(),
);
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$format = $this
->getSetting('flag_display');
$formats = $this
->getOutputFormats();
$summary[] = $formats[$format];
return $summary;
}
protected function getOutputFormats() {
return array(
'flag-before' => $this
->t('Flag before language name'),
'flag-after' => $this
->t('Flag after language name'),
'flag-instead' => $this
->t('Replace language name with flag'),
);
}
protected function viewValue(FieldItemInterface $item) {
$view = parent::viewValue($item);
$value = $view['#plain_text'];
$format = $this
->getSetting('flag_display');
$attributes = new Attribute(array(
'class' => array(
$format,
),
));
$view = array();
if ('flag-instead' != $format) {
$view['language'] = array(
'#plain_text' => $value,
);
}
$view['flag'] = array(
'#theme' => 'flags',
'#code' => strtolower($item->value),
'#attributes' => $attributes,
'#source' => 'language',
);
$view['#prefix'] = '<div class="field__flags__item">';
$view['#suffix'] = '</div>';
return $view;
}
}