Media folder broke my plugin

Hi all, I have a simple plugin to help me create a neat row of images on a page. Now that Kirby puts all images in the media folder, the URLs returned by my plugin result in 404s.

I’d really appreciate some help with fixing it to refer to the new media URLs.

This is how I call it:

(galleryblock: image1.jpg, image2.jpg, image3.jpg:three class: classname)

The images are uploaded in the panel as normal, the ‘three’ is to call the right css class and the class bit is for any twiddly decorative classes I want to add.

And this is my code. I was rusty at PHP when I wrote it and now I’m practically crumbling, which is why I need help. How do I make it return the right images? Thank you!

<?php

Kirby::plugin('aegir/galleryblock', [
    'tags' => [
        'galleryblock' => [
			'attr' => [
				'galleryblock'
			],
			'attr' => [
				'class'
			],
            'html' => function($tag) {

		    $class	= $tag->class;
		    $containers = explode('|', $tag->galleryblock);

		    $thisloc = $tag->parent()->url();

		    $html = '<div class="gallery-block ' . $class . '">';

		    foreach($containers as $container) {
		      // Remove space after the comma
		      $container = trim($container);
		      list($images, $size) = explode(':', $container);
		      //get the image array
		      $images = explode(',', $images);

		      foreach($images as $myimage) {
		        // Remove space after the comma
		        $myimage = trim($myimage);
		        $html .= '<figure class="gallery ' . $size . '"><img src="' . $thisloc . '/' . $myimage . '"></figure>';
		      }
			}

			return $html . '</div>';

            }
        ]
    ]
]);

Nothing to do with your issue, but to understand your code: Where does the pipe come from that you explode by?

I have to admit I can’t remember! I’ve just tried with just plain old explode like below and it makes no difference. I was probably copying some code from someone else’s plugin and it worked, so.

$containers = explode($tag->galleryblock);

You can get the images from the page, don’t use the page url:

<?php

Kirby::plugin('aegir/galleryblock', [
    'tags' => [
        'galleryblock' => [
            'attr' => [
                'class'
            ],

            'html' => function ($tag) {
                $class  = $tag->class;
                [$images, $size]  = explode(':', $tag->galleryblock);
                $imageNames = array_map('trim', explode(',', $images));
                $galleryImages = $tag->parent()->images()->filterBy('filename', 'in', $imageNames);
                $html = '';
                if ($galleryImages->isNotEmpty()) {
                    $html = '<div class="gallery-block ' . $class . '">';
                    foreach ($galleryImages as $image) {
                        $html .= '<figure class="gallery ' . $size . '"><img src="' . $image->url() . '"></figure>';
                    }
                    $html .= '</div>';
                }
                return $html;
            }
        ]
    ]
]);

If I were you, I’d use a separate attribute for the size, but I guess this is an existing project where the tags cannot easily be changed anymore?

That’s brilliant thank you, it works! Yes, I’ve got quite a few posts using this plugin and tag format so was hoping not to have to redo them all and you’ve saved me from having to do that.

Thanks again! I’ll have a look through what you’ve written and hopefully learn something, it looks a lot neater and shorter than what I had.

Don’t hesitate to ask if you have any specific questions.