Plugin: How to get url of media in current page's folder

Hello, I’m working on a plugin to allow the insertions of SVG files in markup, but I’m not sure how I can retrieve the URL of an svg contained within a page folder. Sure, I could use an absolute path in the markup, but for obvious reasons the isn’t a valid solution. How can I grab the URL of the page to point to an SVG in the page folder?

public function onMarkdownInitialized(Event $event)
        $markdown = $event['markdown'];

        // Add the custom block type for SVG insertion
        $markdown->addBlockType('$', 'Svg', true, false);

        // Define the block handler for SVG insertion
        $markdown->blockSvg = function ($line) {
            if (preg_match('/^\$\s*(.+)$/', $line['text'], $matches)) {
                $svgPath = $matches[1];
                $currentUrl = $svgPath . $page->url(true);

                // Load the SVG content
                $svgContent = file_get_contents($currentUrl);
                // Return the SVG content
                return $svgContent;

@seanparko, Have you tried Page::media() and Media::files()?

See Page and the MediaCollectionsInterface of Media

How do I reference these in my code?

@seanparko, Where do you get stuck? Any errors?

Yeah, I mean like how do I put these in my code? Like how do I call the interfaces from the API?

@seanparko, I see you already have a $page in your code. I presume it contains an instantiation of \Grav\Common\Page\Page. Have you tried calling $page->media()?

Tbh that was just generated by chat GPT. Monkey see, monkey do. So I put use in the top of my PHP code to use the class, but how do I actually access these functions? Like I can’t just put getPath() as its undefined. What do I use to define page or something so I can actually call the functions?

@seanparko, Sorry, if you make me spent time while you take the lazy route, I’m out…

1 Like

:skull: I’m new to GravCMS and php in general. Would you prefer me to ask you to write the whole thing for me? I tried to write it on my own, then I asked chat GPT how to get the URL, and then I went to the forums. Would you rather dumb questions from me and everyone else who’s new 24/7, or the occasional one when I can’t figure it out on my own? A part of learning is getting stuck in spot where you cannot figure it out on your own, so you have to ask for help :man_facepalming: . You seem to one man the forums, providing a great public service and know everything. I asked a pretty AI independent question of “How do I actually call upon the API in my plugin”, and you basically responded with nothing after I re-iterated my problem multiple times. Instead of recognising how AI could help you on the forums, and reduce the number of questions like this you get asked, you insulted me. Now, could you please outline how I’ve taken the lazy way out?

Like I actually did the closet thing to what a professional programmer would do in this instance, and I borrowed mostly from the markdown notices plugin beforehand for what I’m currently working on plugin-wise, I just got stuck trying to get the URL.

The point is, that if you can’t make Grav work for you by using available themes and plugins, you will need to have basic knowledge of PHP and/or Twig. Most likely you will also need to dive into Grav docs and go through a Cookbook at least :man_shrugging:

What you’re asking currently, is to write a full solution of that snippet. IMO @pamtbaau already pointed you exactly to what you need to check. But as I explained above, if you are completely new to Grav and even PHP, there’s a good chance you wouldn’t understand what you need to implement :pensive:

Like I can’t tell if I’m talking jibberish. Like I’m not having a go, does it even make sense what I’m asking? Like I know what part of the media interface I want to use, how do I call it? I know I’m a novice and have no clue what I’m doing, but thats apart of doing something for the first time. Does what I’m saying actually mean something? Google doesn’t provide any results.

And yeah I checked through the cookbook, none seemed to involve anything page grabbing a URL. There was one on actually sorting through the pages, but I want their URLs so I can grab an SVG file’s content.

Like I want to use getPath() from Interface: \Grav\Common\Media\Interfaces\MediaCollectionInterface. Can you just tell me if I’m making sense when I say that I want to implement the interface?

In your case I believe getting SVG path is no different than getting any other image path. You probably could check how Grav does it by default

I tried looking in the plugins folder, but there is no base plugin or anything like similar that I could see that appears to have the onMarkdownInitialized function in it. Do you know where-abouts in Grav the markdown compiler is kept? Or the onMarkdownInitialized prototype (if thats what its called) is kept?

It’s either somewhere on Grav core or look for Shortcode plugin