class Boolean
Same name and namespace in other branches
- 11.x core/modules/views/src/Plugin/views/field/Boolean.php \Drupal\views\Plugin\views\field\Boolean
A handler to provide proper displays for booleans.
Allows for display of true/false, yes/no, on/off, enabled/disabled.
Definition terms:
- output formats: An array where the first entry is displayed on boolean true and the second is displayed on boolean false. An example for sticky is:
'output formats' => array(
'sticky' => array(t('Sticky'), ''),
),
Plugin annotation
@ViewsField("boolean");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\views\Plugin\views\HandlerBase extends \Drupal\views\Plugin\views\ViewsHandlerInterface implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\field\FieldPluginBase extends \Drupal\views\Plugin\views\field\FieldHandlerInterface implements \Drupal\views\Plugin\views\HandlerBase
- class \Drupal\views\Plugin\views\field\Boolean implements \Drupal\views\Plugin\views\field\FieldPluginBase
- class \Drupal\views\Plugin\views\field\FieldPluginBase extends \Drupal\views\Plugin\views\field\FieldHandlerInterface implements \Drupal\views\Plugin\views\HandlerBase
- class \Drupal\views\Plugin\views\HandlerBase extends \Drupal\views\Plugin\views\ViewsHandlerInterface implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of Boolean
Related topics
229 string references to 'Boolean'
- aggregator.schema.yml in core/
modules/ aggregator/ config/ schema/ aggregator.schema.yml - core/modules/aggregator/config/schema/aggregator.schema.yml
- aggregator.views.schema.yml in core/
modules/ aggregator/ config/ schema/ aggregator.views.schema.yml - core/modules/aggregator/config/schema/aggregator.views.schema.yml
- BaseFieldOverrideResourceTestBase::getExpectedNormalizedEntity in core/
tests/ Drupal/ FunctionalTests/ Rest/ BaseFieldOverrideResourceTestBase.php - BaseFieldOverrideTest::getExpectedDocument in core/
modules/ jsonapi/ tests/ src/ Functional/ BaseFieldOverrideTest.php - Returns the expected JSON:API document for the entity.
- BlockContent::getFilters in core/
modules/ block_content/ src/ Plugin/ views/ wizard/ BlockContent.php
File
-
core/
modules/ views/ src/ Plugin/ views/ field/ Boolean.php, line 30
Namespace
Drupal\views\Plugin\views\fieldView source
class Boolean extends FieldPluginBase {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['type'] = [
'default' => 'yes-no',
];
$options['type_custom_true'] = [
'default' => '',
];
$options['type_custom_false'] = [
'default' => '',
];
$options['not'] = [
'default' => FALSE,
];
return $options;
}
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$default_formats = [
'yes-no' => [
$this->t('Yes'),
$this->t('No'),
],
'true-false' => [
$this->t('True'),
$this->t('False'),
],
'on-off' => [
$this->t('On'),
$this->t('Off'),
],
'enabled-disabled' => [
$this->t('Enabled'),
$this->t('Disabled'),
],
'boolean' => [
1,
0,
],
'unicode-yes-no' => [
'✔',
'✖',
],
];
$output_formats = $this->definition['output formats'] ?? [];
$custom_format = [
'custom' => [
$this->t('Custom'),
],
];
$this->formats = array_merge($default_formats, $output_formats, $custom_format);
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
foreach ($this->formats as $key => $item) {
$options[$key] = implode('/', $item);
}
$form['type'] = [
'#type' => 'select',
'#title' => $this->t('Output format'),
'#options' => $options,
'#default_value' => $this->options['type'],
];
$form['type_custom_true'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom output for TRUE'),
'#default_value' => $this->options['type_custom_true'],
'#states' => [
'visible' => [
'select[name="options[type]"]' => [
'value' => 'custom',
],
],
],
];
$form['type_custom_false'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom output for FALSE'),
'#default_value' => $this->options['type_custom_false'],
'#states' => [
'visible' => [
'select[name="options[type]"]' => [
'value' => 'custom',
],
],
],
];
$form['not'] = [
'#type' => 'checkbox',
'#title' => $this->t('Reverse'),
'#description' => $this->t('If checked, true will be displayed as false.'),
'#default_value' => $this->options['not'],
];
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
if (!empty($this->options['not'])) {
$value = !$value;
}
if ($this->options['type'] == 'custom') {
$custom_value = $value ? $this->options['type_custom_true'] : $this->options['type_custom_false'];
return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($custom_value));
}
elseif (isset($this->formats[$this->options['type']])) {
return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
}
else {
return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.