You are here

private function SerialItem::getSerial in Serial Field 8

Gets the serial for this entity type, bundle, field instance.

Return value

int serial id

1 call to SerialItem::getSerial()
SerialItem::preSave in src/Plugin/Field/FieldType/SerialItem.php
Defines custom presave behavior for field values.

File

src/Plugin/Field/FieldType/SerialItem.php, line 193

Class

SerialItem
Plugin implementation of the 'serial' field type.

Namespace

Drupal\serial\Plugin\Field\FieldType

Code

private function getSerial() {
  $serial = NULL;
  $entity = $this
    ->getEntity();
  $newSerial = FALSE;

  // Does not apply if the node is not new or translated.
  if ($entity
    ->isNew()) {
    $newSerial = TRUE;
  }
  else {

    // Handle entity translation: fetch the same id or another one
    // depending of what is the design.
    // This should probably be solved by the end user decision
    // while setting the field translation.

    /** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
    $languageManager = \Drupal::getContainer()
      ->get('language_manager');

    // @todo isMultilingual is global, prefer local hasTranslation
    if ($languageManager
      ->isMultilingual() && $entity instanceof TranslatableInterface) {
      $newSerial = $entity
        ->isNewTranslation();
    }
  }
  if ($newSerial) {

    /** @var \Drupal\serial\SerialStorageInterface $serialStorage */
    $serialStorage = \Drupal::getContainer()
      ->get('serial.sql_storage');
    $serial = $serialStorage
      ->generateValue($this
      ->getFieldDefinition(), $this
      ->getEntity());

    // Get the starting value from the storage settings.
    $settings = $this
      ->getSettings();
    $startValue = isset($settings['start_value']) ? $settings['start_value'] : 1;

    // Subtract one as it is already added in code above.
    $serial = $serial + $startValue - 1;
  }
  return $serial;
}