This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

billyZduke
billyZduke

I am developing a site in October that has a local development instance and a remote server instance, fairly standard practice. I've been trying to set it up so that I have as little code as possible to change when copying files between the two instances, to minimize possible breakages.

To that end, I have the local instance externally connecting to the same database that the remote instance uses, but now I need to generate absolute URLs for uploaded media images. I figured I would try extending Twig's |media filter with my own |mediator, which would prepend the necessary additional info to the image path.

Perhaps there is an easier way to do this. I'm kind of hoping for that, because I can't figure out from your 2 filter and 2 function extension examples what I should be doing. If I need to call an existing filter or function from within my plugin's extension, how do I do that? And how do I get the value that |media is attempting to filter in the first place?

DMeganoski
DMeganoski

I have also been working with Twig lately, so think I can point you in the right direction, at least.

If you take a look at Cms\Twig\Extension you can see where the original 'media' filter is defined.

When the extension class is created, the controller instance is passed to it and assigned to a variable ($this->controller). The media filter calls a simple function on the controller

$this->controller->mediaUrl($file)

This controller function also contains a simple code snippet.

return MediaLibrary::url($file);

MediaLibrary also belongs to the Cms/Classes namespace.

So just add the extension in your plugin, create a function that will call the previous mentioned url function, and then you should be able to append it from there.

something like this in your Plugin.php


 public function registerMarkupTags() {
        return [
            'filters' => [
                // A global function, i.e str_plural()
                'mediator' => [$this, 'absoluteMediaUrl'],
            ],
        ];
    }

    public function absoluteMediaUrl($file) {
        $original = \Cms\Classes\MediaLibrary::url($file);

        // modify here and return
    }
billyZduke
billyZduke

Thank you, that worked perfectly!

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.