You are here

public function Dimension::preSave in Dimension 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/Field/FieldType/Dimension.php \Drupal\dimension\Plugin\Field\FieldType\Dimension::preSave()
  2. 2.0.x src/Plugin/Field/FieldType/Dimension.php \Drupal\dimension\Plugin\Field\FieldType\Dimension::preSave()

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 DecimalItem::preSave

File

src/Plugin/Field/FieldType/Dimension.php, line 267

Class

Dimension

Namespace

Drupal\dimension\Plugin\Field\FieldType

Code

public function preSave() {
  $values = array();

  /** @noinspection StaticInvocationViaThisInspection */
  foreach ($this
    ->fields() as $key => $label) {
    $values[$key] = $this->{$key};
    $storage_settings = $this
      ->getSetting('storage_' . $key);
    $this->{$key} = round($this->{$key}, $storage_settings['scale']);
  }
  $this->value = $this
    ->calculate($values);
}