Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldType\BooleanItem.
Dries Buytaert
committed
*/
Alex Pott
committed
namespace Drupal\Core\Field\Plugin\Field\FieldType;
Dries Buytaert
committed
use Drupal\Core\Field\FieldDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FieldItemBase;
Alex Pott
committed
use Drupal\Core\Field\FieldStorageDefinitionInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
catch
committed
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\DataDefinition;
Dries Buytaert
committed
/**
Alex Pott
committed
* Defines the 'boolean' entity field type.
Alex Pott
committed
*
Alex Pott
committed
* @FieldType(
* id = "boolean",
* label = @Translation("Boolean"),
Alex Pott
committed
* description = @Translation("An entity field containing a boolean value."),
catch
committed
* default_widget = "boolean_checkbox",
* default_formatter = "boolean",
Alex Pott
committed
* )
Dries Buytaert
committed
*/
class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
catch
committed
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
catch
committed
return array(
'on_label' => t('On'),
'off_label' => t('Off'),
) + parent::defaultFieldSettings();
catch
committed
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('boolean')
->setLabel(t('Boolean value'))
->setRequired(TRUE);
Dries Buytaert
committed
return $properties;
Dries Buytaert
committed
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function schema(FieldStorageDefinitionInterface $field_definition) {
Alex Pott
committed
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'size' => 'tiny',
),
),
);
}
catch
committed
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = array();
catch
committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
$element['on_label'] = array(
'#type' => 'textfield',
'#title' => $this->t('"On" label'),
'#default_value' => $this->getSetting('on_label'),
'#required' => TRUE,
);
$element['off_label'] = array(
'#type' => 'textfield',
'#title' => $this->t('"Off" label'),
'#default_value' => $this->getSetting('off_label'),
'#required' => TRUE,
);
return $element;
}
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
return array(0, 1);
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
return array(
0 => $this->getSetting('off_label'),
1 => $this->getSetting('on_label'),
);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
return array(0, 1);
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
return $this->getPossibleOptions($account);
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['value'] = mt_rand(0, 1);
return $values;
}
Dries Buytaert
committed
}