You are here

public function TaxNumberItem::preSave in Commerce Core 8.2

Defines custom presave behavior for field values.

This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.

Overrides FieldItemBase::preSave

File

modules/tax/src/Plugin/Field/FieldType/TaxNumberItem.php, line 217

Class

TaxNumberItem
Plugin implementation of the 'commerce_tax_number' field type.

Namespace

Drupal\commerce_tax\Plugin\Field\FieldType

Code

public function preSave() {
  parent::preSave();
  if ($this
    ->isEmpty() || !empty($this->verification_state)) {
    return;
  }

  // The validator can't modify the field item in order to store the
  // verification result. This is why verification must be run again here.
  // TaxNumberTypeWithVerificationBase uses a memory cache to avoid making
  // multiple API requests for the same data.
  $type_plugin = $this
    ->getTypePlugin();
  if ($type_plugin instanceof SupportsVerificationInterface) {
    $result = $type_plugin
      ->verify($this->value);
    $this
      ->applyVerificationResult($result);
  }
}