Skip to content

Commit 2be9f21

Browse files
committed
WIP
- Work in progress on cover image retrieval from blocks
1 parent 492e631 commit 2be9f21

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

site/blueprints/pages/article.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ fields:
2020
label: Subtitle
2121
type: text
2222
required: true
23+
24+
cover_image:
25+
label: Cover image
26+
type: files
27+
required: true
28+
multiple: false
29+
accept: image/*
2330

2431
blocks:
2532
type: blocks

site/models/article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function abstract(): string
1212

1313
return match(true) {
1414
!empty($abstract) => $abstract,
15-
$this->subtitle()->isNoEmpty() => $this->subtitle(),
15+
$this->subtitle()->isNotEmpty() => $this->subtitle(),
1616
default => $this->title(),
1717
};
1818
}

site/models/default.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Kirby\Cms\Block;
4+
use Kirby\Cms\Page;
5+
use Kirby\Toolkit\Str;
6+
7+
class defaultPage extends Page
8+
{
9+
public function coverImage(): string
10+
{
11+
$coverImage = $this->getCoverImage();
12+
13+
return match(true) {
14+
!empty($coverImage) => $coverImage, // Si se encontró una imagen, devuelve la URL
15+
default => '', // Si no se encontró imagen, devuelve un valor vacío
16+
};
17+
}
18+
19+
protected function getCoverImage(): ?string
20+
{
21+
// Verifica si la página tiene bloques
22+
if ($this->blocks()->isEmpty()) {
23+
return null;
24+
}
25+
26+
// Filtra los bloques para encontrar aquellos de tipo 'files'
27+
$image = $this
28+
->blocks()
29+
->toBlocks()
30+
->filter(fn($block) => $block->type() === 'files')
31+
->first(); // Obtiene el primer bloque de tipo 'file'
32+
33+
// Si no se encuentra ninguna imagen, retorna null
34+
if ($image === null) {
35+
return null;
36+
}
37+
38+
// Obtiene el primer archivo de imagen y retorna su URL
39+
$file = $image->toFile();
40+
41+
// Si el archivo existe y es una imagen, devuelve su URL
42+
if ($file && $file->isImage()) {
43+
return $file->url();
44+
}
45+
46+
// Si no es una imagen, retorna null
47+
return null;
48+
}
49+
50+
}

site/templates/home.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{ $article->abstract() }}
1616
</div>
1717
</div>
18-
<img src="{{ $article->image()->url() }}" alt="{{ $article->title() }}" class="w-full sm:w-32 sm:h-28 object-cover shadow-md sm:ml-6 mt-3 sm:mt-0">
18+
<img src="{{ $article->coverImage() }}">
1919
</a>
2020
<hr class="border-t border-gray-200">
2121
@endforeach

0 commit comments

Comments
 (0)