Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

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

plugins

Started by Sean 2 years ago · 14 replies · 335 views
2 years ago

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?

PHP
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;
            }
        };
    }
2 years ago

How do I reference these in my code?

2 years ago

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

2 years ago

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

2 years ago

@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()?

2 years ago

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?

2 years ago

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

👍 1
2 years ago

💀 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.

last edited 03/19/24 by Sean
2 years ago

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 🤷‍♂️

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 😔

2 years ago

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.

last edited 03/19/24 by Sean
2 years ago

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?

2 years ago

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

2 years ago

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?

last edited 03/19/24 by Sean
2 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 44 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago