View source
<?php
namespace Drupal\boolean_formatter\Plugin\field\formatter;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Field\FieldInterface;
class BooleanYesNo extends FormatterBase {
public function settingsForm(array $form, array &$form_state) {
$field_name = $this->fieldDefinition
->getFieldName();
$form['format'] = array(
'#type' => 'select',
'#title' => t('Output format'),
'#options' => $this
->getFormatOptions(),
'#default_value' => $this
->getSetting('format'),
);
$form['custom_on'] = array(
'#type' => 'textfield',
'#title' => t('Custom output for On'),
'#default_value' => $this
->getSetting('custom_on'),
'#states' => array(
'visible' => array(
'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => array(
'value' => 'custom',
),
),
),
);
$form['custom_off'] = array(
'#type' => 'textfield',
'#title' => t('Custom output for Off'),
'#default_value' => $this
->getSetting('custom_off'),
'#states' => array(
'visible' => array(
'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => array(
'value' => 'custom',
),
),
),
);
$form['reverse'] = array(
'#type' => 'checkbox',
'#title' => t('Reverse'),
'#description' => t('If checked, true will be displayed as false.'),
'#default_value' => $this
->getSetting('reverse'),
);
return $form;
}
public function settingsSummary() {
$formats = $this
->getFormatOptions($this->settings);
$summary[] = t('Output format: %format', array(
'%format' => $formats[$this
->getSetting('format')],
));
$summary[] = t('Reversed: @reversed', array(
'@reversed' => $this
->displayValueWithFormat($this
->getSetting('reverse'), 'yes-no', array(
'reverse' => FALSE,
)),
));
return $summary;
}
public function viewElements(EntityInterface $entity, $langcode, FieldInterface $items) {
$elements = array();
$values = options_allowed_values($this->fieldDefinition, $entity);
foreach ($items as $delta => $item) {
$value = !empty($values[1]) ? $item->value == $values[1] : !empty($item->value);
$text = $this
->displayValueWithFormat($value, $this
->getSetting('format'), $this->settings);
$elements[$delta] = array(
'#markup' => field_filter_xss($text),
);
}
return $elements;
}
public function getFormats(array $options = array()) {
$formats = array(
'yes-no' => array(
t('Yes'),
t('No'),
),
'true-false' => array(
t('True'),
t('False'),
),
'on-off' => array(
t('On'),
t('Off'),
),
'enabled-disabled' => array(
t('Enabled'),
t('Disabled'),
),
'boolean' => array(
1,
0,
),
'unicode-yes-no' => array(
'✔',
'✖',
),
'custom' => array(
t('Custom'),
),
);
if (isset($options['custom_on']) && isset($options['custom_off'])) {
$formats['custom'] = array(
$options['custom_on'],
$options['custom_off'],
);
}
return $formats;
}
public function getFormatOptions(array $options = array()) {
$format_options = array();
foreach ($this
->getFormats($options) as $key => $format) {
$format_options[$key] = implode('/', $format);
}
return $format_options;
}
public function displayValueWithFormat($value, $format, array $options = array()) {
$formats = $this
->getFormats($options);
if (!isset($formats[$format])) {
reset($formats);
$format = key($formats);
}
if (!empty($options['reverse'])) {
$value = !(bool) $value;
}
return !empty($value) ? $formats[$format][0] : $formats[$format][1];
}
}