function ImageItem::preSave

Same name and namespace in other branches
  1. 11.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::preSave()
  2. 10 core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::preSave()
  3. 9 core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::preSave()
  4. 8.9.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::preSave()

Overrides EntityReferenceItem::preSave

File

core/modules/image/src/Plugin/Field/FieldType/ImageItem.php, line 325

Class

ImageItem
Plugin implementation of the 'image' field type.

Namespace

Drupal\image\Plugin\Field\FieldType

Code

public function preSave() {
  parent::preSave();
  $width = $this->get('width')
    ->getValue();
  $height = $this->get('height')
    ->getValue();
  // Determine the dimensions if necessary.
  if ($this->entity && $this->entity instanceof EntityInterface) {
    if ($width === NULL || $height === NULL) {
      $image = \Drupal::service('image.factory')->get($this->entity
        ->getFileUri());
      if ($image->isValid()) {
        $this->set('width', $image->getWidth());
        $this->set('height', $image->getHeight());
      }
    }
  }
  else {
    $this->getLogger('image')
      ->warning("Missing file with ID %id.", [
      '%id' => $this->target_id,
    ]);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.