function ImageItem::preSave

Same name and namespace in other branches
  1. 9 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. 11.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 312

Class

ImageItem
Plugin implementation of the 'image' field type.

Namespace

Drupal\image\Plugin\Field\FieldType

Code

public function preSave() {
    parent::preSave();
    $width = $this->width;
    $height = $this->height;
    // Determine the dimensions if necessary.
    if ($this->entity && $this->entity instanceof EntityInterface) {
        if (empty($width) || empty($height)) {
            $image = \Drupal::service('image.factory')->get($this->entity
                ->getFileUri());
            if ($image->isValid()) {
                $this->width = $image->getWidth();
                $this->height = $image->getHeight();
            }
        }
    }
    else {
        trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING);
    }
}

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