File tree Expand file tree Collapse file tree 4 files changed +59
-2
lines changed
Expand file tree Collapse file tree 4 files changed +59
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments