ComputedStringItem.php in Computed Field 3.x
File
src/Plugin/Field/FieldType/ComputedStringItem.php
View source
<?php
namespace Drupal\computed_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
class ComputedStringItem extends ComputedStringItemBase {
use ComputedFieldStronglyTypedItemTrait;
public static function defaultStorageSettings() {
return [
'max_length' => 255,
'is_ascii' => FALSE,
] + parent::defaultStorageSettings();
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$schema = [
'columns' => [
'value' => [
'type' => $settings['is_ascii'] === TRUE ? 'varchar_ascii' : 'varchar',
'length' => (int) $settings['max_length'],
'binary' => $settings['case_sensitive'],
],
],
];
return $schema;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$settings = $this
->getSettings();
$element = [];
$element['max_length'] = [
'#type' => 'number',
'#title' => t('Maximum length'),
'#default_value' => $settings['max_length'],
'#required' => TRUE,
'#description' => t('The maximum length of the field in characters.'),
'#min' => 1,
'#disabled' => $has_data,
];
return $element;
}
public function executeCode() {
return mb_substr(parent::executeCode(), 0, $this
->getSettings()['max_length'] - 1);
}
}