View source
<?php
namespace Drupal\options\Plugin\Field\FieldType;
use Drupal\Core\Field\AllowedTagsXssTrait;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
abstract class ListItemBase extends FieldItemBase implements OptionsProviderInterface {
use AllowedTagsXssTrait;
public static function defaultStorageSettings() {
return [
'allowed_values' => [],
'allowed_values_function' => '',
] + parent::defaultStorageSettings();
}
public function getPossibleValues(AccountInterface $account = NULL) {
$flatten_options = OptGroup::flattenOptions($this
->getPossibleOptions($account));
return array_keys($flatten_options);
}
public function getPossibleOptions(AccountInterface $account = NULL) {
return $this
->getSettableOptions($account);
}
public function getSettableValues(AccountInterface $account = NULL) {
$flatten_options = OptGroup::flattenOptions($this
->getSettableOptions($account));
return array_keys($flatten_options);
}
public function getSettableOptions(AccountInterface $account = NULL) {
$allowed_options = options_allowed_values($this
->getFieldDefinition()
->getFieldStorageDefinition(), $this
->getEntity());
return $allowed_options;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$allowed_options = options_allowed_values($field_definition
->getFieldStorageDefinition());
$values['value'] = array_rand($allowed_options);
return $values;
}
public function isEmpty() {
return empty($this->value) && (string) $this->value !== '0';
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$allowed_values = $this
->getSetting('allowed_values');
$allowed_values_function = $this
->getSetting('allowed_values_function');
$element['allowed_values'] = [
'#type' => 'textarea',
'#title' => t('Allowed values list'),
'#default_value' => $this
->allowedValuesString($allowed_values),
'#rows' => 10,
'#access' => empty($allowed_values_function),
'#element_validate' => [
[
get_class($this),
'validateAllowedValues',
],
],
'#field_has_data' => $has_data,
'#field_name' => $this
->getFieldDefinition()
->getName(),
'#entity_type' => $this
->getEntity()
->getEntityTypeId(),
'#allowed_values' => $allowed_values,
];
$element['allowed_values']['#description'] = $this
->allowedValuesDescription();
$element['allowed_values_function'] = [
'#type' => 'item',
'#title' => t('Allowed values list'),
'#markup' => t('The value of this field is being determined by the %function function and may not be changed.', [
'%function' => $allowed_values_function,
]),
'#access' => !empty($allowed_values_function),
'#value' => $allowed_values_function,
];
return $element;
}
protected abstract function allowedValuesDescription();
public static function validateAllowedValues($element, FormStateInterface $form_state) {
$values = static::extractAllowedValues($element['#value'], $element['#field_has_data']);
if (!is_array($values)) {
$form_state
->setError($element, t('Allowed values list: invalid input.'));
}
else {
foreach ($values as $key => $value) {
if ($error = static::validateAllowedValue($key)) {
$form_state
->setError($element, $error);
break;
}
}
if ($element['#field_has_data']) {
$lost_keys = array_keys(array_diff_key($element['#allowed_values'], $values));
if (_options_values_in_use($element['#entity_type'], $element['#field_name'], $lost_keys)) {
$form_state
->setError($element, t('Allowed values list: some values are being removed while currently in use.'));
}
}
$form_state
->setValueForElement($element, $values);
}
}
protected static function extractAllowedValues($string, $has_data) {
$values = [];
$list = explode("\n", $string);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
$generated_keys = $explicit_keys = FALSE;
foreach ($list as $position => $text) {
$matches = [];
if (preg_match('/(.*)\\|(.*)/', $text, $matches)) {
$key = trim($matches[1]);
$value = trim($matches[2]);
$explicit_keys = TRUE;
}
elseif (!static::validateAllowedValue($text)) {
$key = $value = $text;
$explicit_keys = TRUE;
}
elseif (!$has_data) {
$key = (string) $position;
$value = $text;
$generated_keys = TRUE;
}
else {
return;
}
$values[$key] = $value;
}
if ($explicit_keys && $generated_keys) {
return;
}
return $values;
}
protected static function validateAllowedValue($option) {
}
protected function allowedValuesString($values) {
$lines = [];
foreach ($values as $key => $value) {
$lines[] = "{$key}|{$value}";
}
return implode("\n", $lines);
}
public static function storageSettingsToConfigData(array $settings) {
if (isset($settings['allowed_values'])) {
$settings['allowed_values'] = static::structureAllowedValues($settings['allowed_values']);
}
return $settings;
}
public static function storageSettingsFromConfigData(array $settings) {
if (isset($settings['allowed_values'])) {
$settings['allowed_values'] = static::simplifyAllowedValues($settings['allowed_values']);
}
return $settings;
}
protected static function simplifyAllowedValues(array $structured_values) {
$values = [];
foreach ($structured_values as $item) {
if (is_array($item['label'])) {
$item['label'] = static::simplifyAllowedValues($item['label']);
}
$values[$item['value']] = $item['label'];
}
return $values;
}
protected static function structureAllowedValues(array $values) {
$structured_values = [];
foreach ($values as $value => $label) {
if (is_array($label)) {
$label = static::structureAllowedValues($label);
}
$structured_values[] = [
'value' => static::castAllowedValue($value),
'label' => $label,
];
}
return $structured_values;
}
protected static function castAllowedValue($value) {
return $value;
}
}