new TranslatableMarkup('On'), 'off_label' => new TranslatableMarkup('Off'), ] + parent::defaultFieldSettings(); } /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('boolean') ->setLabel(t('Boolean value')) ->setRequired(TRUE); return $properties; } /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return [ 'columns' => [ 'value' => [ 'type' => 'int', 'size' => 'tiny', ], ], ]; } /** * {@inheritdoc} */ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $element = []; $element['on_label'] = [ '#type' => 'textfield', '#title' => $this->t('"On" label'), '#default_value' => $this->getSetting('on_label'), '#required' => TRUE, ]; $element['off_label'] = [ '#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 [0, 1]; } /** * {@inheritdoc} */ public function getPossibleOptions(AccountInterface $account = NULL) { return [ 0 => $this->getSetting('off_label'), 1 => $this->getSetting('on_label'), ]; } /** * {@inheritdoc} */ public function getSettableValues(AccountInterface $account = NULL) { return [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; } }