Skip to content

Commit

Permalink
Refactor background image with studio
Browse files Browse the repository at this point in the history
  • Loading branch information
rabauss committed May 10, 2023
1 parent 07b915d commit 6ed97c0
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/Controller/ContentElement/ColumnStartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

use Contao\BackendTemplate;
use Contao\ContentModel;
use Contao\Controller;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Image\Studio\Studio;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\File;
use Contao\FilesModel;
use Contao\StringUtil;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -31,11 +29,13 @@
class ColumnStartController extends AbstractContentElementController
{
private ScopeMatcher $scopeMatcher;
private Studio $studio;

public function __construct(ScopeMatcher $scopeMatcher)
public function __construct(ScopeMatcher $scopeMatcher, Studio $studio)
{
$this->scopeMatcher = $scopeMatcher;
}
$this->studio = $studio;
}

protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
Expand Down Expand Up @@ -79,18 +79,15 @@ protected function getResponse(Template $template, ContentModel $model, Request
}

if (trim($model->rs_column_background_image)) {
$image = FilesModel::findByPk($model->rs_column_background_image);
if (null !== $image) {
$file = new File($image->path);
$imageObject = new \stdClass();
Controller::addImageToTemplate($imageObject, array(
'id' => $image->id,
'uuid' => $image->uuid ?? null,
'name' => $file->basename,
'singleSRC' => $image->path,
'size' => $model->rs_column_background_image_size,
));
$styles[] = 'background-image: url("' . $imageObject->src . '");';
$figure = $this->studio
->createFigureBuilder()
->fromUuid($model->rs_column_background_image ?: '')
->setSize($model->rs_column_background_image_size)
->enableLightbox(false)
->buildIfResourceExists()
;
if (null !== $figure) {
$styles[] = 'background-image: url("' . $figure->getImage()->getImageSrc(true) . '");';
}
}

Expand Down

0 comments on commit 6ed97c0

Please sign in to comment.