View source
<?php
namespace Drupal\serial\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\TranslatableInterface;
class SerialItem extends FieldItemBase {
public static function schema(FieldStorageDefinitionInterface $field) {
return [
'columns' => [
'value' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'sortable' => TRUE,
'views' => TRUE,
'index' => TRUE,
],
],
];
}
public static function defaultStorageSettings() {
return [
'start_value' => 1,
'init_existing_entities' => 0,
] + parent::defaultStorageSettings();
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$element['start_value'] = [
'#type' => 'number',
'#title' => $this
->t('Starting value'),
'#default_value' => $this
->getSetting('start_value'),
'#min' => 1,
'#required' => TRUE,
'#disabled' => $has_data,
];
$element['init_existing_entities'] = [
'#type' => 'radios',
'#title' => $this
->t('Start on existing entities'),
'#description' => $this
->t('When this field is created for a bundle that already have entities.'),
'#options' => [
0 => $this
->t('No'),
1 => $this
->t('Yes'),
],
'#default_value' => $this
->getSetting('init_existing_entities'),
'#required' => TRUE,
'#disabled' => $has_data,
];
if (!$has_data) {
$form['#validate'][] = [
$this,
'initializeEntitiesCallback',
];
}
return $element;
}
public function initializeEntitiesCallback(array &$form, FormStateInterface $form_state) {
$settings = $form_state
->getValue('settings');
if ((int) $settings['init_existing_entities'] === 1) {
$startValue = (int) $settings['start_value'];
$fieldConfig = $this
->getFieldDefinition();
$entityTypeId = $fieldConfig
->getTargetEntityTypeId();
$storage = \Drupal::entityTypeManager()
->getStorage($entityTypeId);
$bundleKey = $storage
->getEntityType()
->getKey('bundle');
$bundle = $fieldConfig
->getTargetBundle();
$query = \Drupal::entityQuery($entityTypeId);
$query
->condition($bundleKey, $bundle);
$ids = $query
->execute();
if (count($ids) > 0) {
$serialStorage = \Drupal::getContainer()
->get('serial.sql_storage');
$oldCount = $serialStorage
->initOldEntries($entityTypeId, $bundle, $fieldConfig
->getFieldStorageDefinition()
->getName(), $startValue);
if ($oldCount > 0) {
\Drupal::messenger()
->addMessage(t('Serial values have been automatically set for %count existing entities, starting from %start_value.', [
'%count' => $oldCount,
'%start_value' => $startValue,
]));
}
}
else {
\Drupal::messenger()
->addWarning(t('No entities to initialize, the next entity to be created will start from %start_value.', [
'%start_value' => $startValue,
]));
}
}
}
public function getValue() {
foreach ($this->properties as $name => $property) {
$value = $property
->getValue();
if (isset($this->values) || isset($value)) {
$this->values[$name] = $value;
}
}
return $this->values;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('Serial'))
->setComputed(TRUE)
->setInternal(FALSE)
->setRequired(TRUE);
return $properties;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
$empty = $value === NULL || !is_numeric($value);
return $empty;
}
public function preSave() {
$value = $this
->getSerial();
if (isset($value)) {
$this
->setValue($value);
}
}
private function getSerial() {
$serial = NULL;
$entity = $this
->getEntity();
$newSerial = FALSE;
if ($entity
->isNew()) {
$newSerial = TRUE;
}
else {
$languageManager = \Drupal::getContainer()
->get('language_manager');
if ($languageManager
->isMultilingual() && $entity instanceof TranslatableInterface) {
$newSerial = $entity
->isNewTranslation();
}
}
if ($newSerial) {
$serialStorage = \Drupal::getContainer()
->get('serial.sql_storage');
$serial = $serialStorage
->generateValue($this
->getFieldDefinition(), $this
->getEntity());
$settings = $this
->getSettings();
$startValue = isset($settings['start_value']) ? $settings['start_value'] : 1;
$serial = $serial + $startValue - 1;
}
return $serial;
}
}