View source
<?php
namespace Drupal\contact_storage\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\options\Plugin\Field\FieldType\ListItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Session\AccountInterface;
class OptionsEmailItem extends ListItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Text value'))
->addConstraint('Length', [
'max' => 255,
])
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
protected function allowedValuesDescription() {
$description = '<p>' . $this
->t('The possible values this field can contain. Enter one value per line, in the format key|label|emails.');
$description .= '<br/>' . $this
->t('"key" is the message that is added to the body of the message.');
$description .= '<br/>' . $this
->t('"label" is the value displayed in the dropdown menu on the contact form.');
$description .= '<br/>' . $this
->t('"emails" are the email addresses to add to the recipients list (each separated by a comma).');
$description .= '</p>';
$description .= '<p>' . $this
->t('Allowed HTML tags in labels: @tags', [
'@tags' => FieldFilteredMarkup::displayAllowedTags(),
]) . '</p>';
return $description;
}
protected static function extractAllowedValues($string, $has_data) {
$values = [];
$list = explode("\n", $string);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
foreach ($list as $text) {
$elements = explode('|', $text);
if (count($elements) == 3) {
if (\Drupal::service('email.validator')
->isValid($elements[2])) {
$values[$elements[0]] = [
'value' => $elements[1],
'emails' => $elements[2],
];
continue;
}
}
return;
}
if (empty($values)) {
return;
}
return $values;
}
protected static function simplifyAllowedValues(array $structured_values) {
$values = [];
foreach ($structured_values as $value) {
$values[$value['key']] = [
'value' => $value['value'],
'emails' => $value['emails'],
];
}
return $values;
}
protected static function structureAllowedValues(array $values) {
$structured_values = [];
foreach ($values as $key => $value) {
$structured_values[] = [
'key' => $key,
'value' => $value['value'],
'emails' => $value['emails'],
];
}
return $structured_values;
}
public function getSettableOptions(AccountInterface $account = NULL) {
$allowed_options_keys = [];
$allowed_options = $this
->getOptionsAllowedValues();
foreach ($allowed_options as $key => $option) {
$allowed_options_keys[$key] = $key;
}
return $allowed_options_keys;
}
protected function getOptionsAllowedValues() {
return options_allowed_values($this
->getFieldDefinition()
->getFieldStorageDefinition(), $this
->getEntity());
}
protected function allowedValuesString($values) {
$lines = [];
foreach ($values as $key => $value) {
$lines[] = $key . '|' . $value['value'] . '|' . $value['emails'];
}
return implode("\n", $lines);
}
}