public function getImage($fallbackToPlaceholder = true, $placeHolderPath = null)
{
$finder = $this->getImagesFinder();
$imageQuery = Image::find()->where($finder);
$imageQuery->orderBy(['isMain' => SORT_DESC, 'id' => SORT_ASC]);
$img = $imageQuery->one();
// No image model + fallback to placeholder or
// image model but image does not exist + fallback to placeholder
if (!$img && $fallbackToPlaceholder || $img !== null && !file_exists($img->getBaseUrl()) && $fallbackToPlaceholder) {
// Custom placeholder
if ($placeHolderPath) {
$placeHolder = new Image(['filePath' => basename(Yii::getAlias($placeHolderPath)), 'urlAlias' => basename($placeHolderPath)]);
return $placeHolder;
// Default placeholder
} else {
return new PlaceHolder();
}
}
return $img;
}