Expansion Shortcode Core Plugin

Hello everyone!
I want to expand Shortcode Core Plugin without creating a plugin.
Can you tell me how to access the images (theme://images)?

<?php
namespace Grav\Plugin\Shortcodes;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class IconShortcode extends Shortcode
{
    public function init()
    {
        $this->shortcode->getHandlers()->add('icon', function(ShortcodeInterface $sc) {
            $class = $sc->getParameter('class');
            $icon = $sc->getParameter('icon');
            $color = $sc->getParameter('color');
            $svg = $sc->getParameter('svg');
            $wh = $sc->getParameter('wh');

            $class_output = $class ? ' class="' . $class . '"' : '';
            $icon_output = $icon ? '<span class="uk-margin-small-right" uk-icon="icon: ' . $icon . '"></span>' : '';
            $svg_output = $svg ? '<img src="theme://images/svg/' . $svg . '.svg" width="' . $wh . '" height="' . $wh . '" uk-svg>' : '';

            $content = $sc->getContent();

            if ($svg_output) { 
              $content = $svg_output . $content;
            }
            if ($icon_output) {
              $content = $icon_output . $content;
            }
            return $content;
        });
    }
}

I’m not sure the docs make this very clear. I’m rusty, but I think you can only use stream URLs directly in page markdown, e.g. ![my image](theme://images/theme-image.jpg).

If you want to use them in Twig or PHP, they need to be wrapped in the url() and Utils::url() functions respectively.

And you probably realise this but I’ll say it: you’ll need use \Grav\Common\Utils at the top of your PHP file.

Let us know if something’s not clear or correct.

@g00dman,

Two alternatives:

  1. In PHP one can use:

    $locator = $this->grav['locator'];
    $path = $locator->findResource('theme://images');
    ==> '/www/grav/site-dev/user/themes/quark/images'
    
  2. In Markdown, you should also be able to use:

    [yourshortcode]
    ![](theme://images/myimage.png)
    [\yourshortcode]
    

    In your PHP shortcode definition, $content = $sc->getContent(); should now contain the HTML of the parsed image: <img src="path/to/image" ... >

Option 1 works, but the path turns out to be absolute. On the local server: /home/lex/www/grav/user/themes/hotelvial/images/svg/hair.svg (the image is not displayed). Need to get a relative link - /grav/user/themes/hotelvial/images/svg/hair.svg
we need a solution specifically for PHP
I’m sorry for my English ))

Hmm, I am surprised and also I thought one of the parameters could toggle this but it looks like it doesn’t. I think @pamtbaau’s suggestions will work though. @pamtbaau usually tests posted suggestions.

@g00dman,

When you hover in your editor over the findResource() method, you will see a boolean parameter $absolute = true. By default it returns an absolute path.

   RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator::findResource

   Find highest priority instance from a resource.

   <?php
   public function findResource($uri, $absolute = true, $first = false) { }

   @param string $uri — Input URI to be searched.
   @param bool $absolute — Whether to return absolute path.
   @param bool $first — Whether to return first path even if it doesn't exist.
   @return string|false

   @throws BadMethodCallException

Or visit the sourcecode UniformResourceLocator.php#L353-L363:

    public function findResource($uri, $absolute = true, $first = false)
    {
        if (!is_string($uri)) {
            throw new BadMethodCallException('Invalid parameter $uri.');
        }


        /** @var string|false $cached */
        $cached = $this->findCached($uri, false, $absolute, $first);


        return $cached;
    }