Skip to content

Commit

Permalink
[photo] shortcode, fix order images
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Emmolo committed Jun 13, 2024
1 parent 271a6f6 commit eb56f46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion resources/views/new-gallery.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="w-full">
<div class="
grid grid-cols-4 grid-rows-2 not-prose sm:px-0">
@foreach (json_decode($images) as $image)
@foreach ($images as $image)
@if ($loop->iteration <= 5)
<a class="
Expand Down
20 changes: 13 additions & 7 deletions src/Shortcodes/PhotoShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PhotoShortcode
{
public function register($shortcode): string
{
if (! class_exists('\Outl1ne\NovaMediaHub\Models\Media')) {
if (!class_exists('\Outl1ne\NovaMediaHub\Models\Media')) {
return '';
}

Expand All @@ -16,8 +16,14 @@ public function register($shortcode): string
$ids = explode(',', $multipleIds);
if (count($ids) > 1) {
$images = \Outl1ne\NovaMediaHub\Models\Media::whereIn('id', $ids)->get();

//order images by shortcode order
$images = $images->sortBy(function ($image) use ($ids) {
return array_search($image->id, $ids);
});

foreach ($images as $key => $image) {
$images[$key]['src'] = $image->path.$image->file_name;
$images[$key]['src'] = $image->path . $image->file_name;
$images[$key]['title'] = $image['data']['title'][0] ?? null;
$images[$key]['alt'] = $image['data']['alt'][0] ?? null;
}
Expand All @@ -37,11 +43,11 @@ public function register($shortcode): string

// Single image
$media = \Outl1ne\NovaMediaHub\Models\Media::find($shortcode->id);
if (! $media) {
if (!$media) {
return '';
}

$path = $media->path.$media->file_name;
$path = $media->path . $media->file_name;
$align = $shortcode->align ?? null;
$link = $shortcode->link ? str_replace("'", '%27', $shortcode->link) : null;
$shape = $shortcode->shape ?? null;
Expand All @@ -58,7 +64,7 @@ public function register($shortcode): string
if (is_array($credits)) {
$credits = $credits[0];
}
$alt = $media->data['alt'] ?? 'Immagine id '.$shortcode->id;
$alt = $media->data['alt'] ?? 'Immagine id ' . $shortcode->id;
if (is_array($alt)) {
$alt = $alt[0];
}
Expand Down Expand Up @@ -87,10 +93,10 @@ public function register($shortcode): string
public static function getImageHeight(string $path, int $width = 0): float|int
{

$localPath = storage_path('app/public/'.$path);
$localPath = storage_path('app/public/' . $path);

// Check if file exists
if (! file_exists($localPath)) {
if (!file_exists($localPath)) {
return 0;
}

Expand Down

0 comments on commit eb56f46

Please sign in to comment.