verbb / shortcodes

Craft CMS 3 shortcode plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question/Feature request: How to add parameters like context to the shortcode filter?

Friksel opened this issue · comments

Hi, first of all thanks for this plugin. It's pretty easy to setup and use. I normally try to keep away from shortcodes, but for a project I need to have floating images inside texts and I want Craft to keep track of them, so I can't use the build in Redactor image inserts for example.

This is what I'm after: I'm having a matrix block with a redactor field and an assets field. Inside the redactor field people can bind images from the assets field by image-key in a shortcode. so for example by entering [image]pencils[/image].
But the problem I am facing now is that inside the shortcode there seem to be no way to know on which matrix block or even redactor-field the shortcode filter was called. But I might be missing something?

So what I am looking for is a way to pass params to the shortcode filter method, for example like this:
{{ rich_content|shortcodes([ { context: content_block }, ]) }}

I've been looking in the readme and the TemplateHandler code, but this doesn't seem to be possible at the moment.

Could this functionality be added? It would solve a lot of problems using the plugin if we could add in our own parameters!

Thanks in advance!

Context would be a great enhancement. I’ll see if I can add it this weekend.

@samhernandez Thanks a lot for your quick response! That would be really awesome!

Quick update: I wrestled with PhpStorm this weekend :)

I'll see if I can't get it in this week but as a stopgap, you might try this plugin: https://plugins.craftcms.com/transient

You could set some variables in the template that calls the shortcode, then retrieve them in the template that handles the shortcode.

@samhernandez Thanks for the update and helpful suggestion. No worries. Take a look at VS Code, highly recommended!

I'd like to keep the amount of plugins as minimal as possible, but that transient plugin looks interesting. Worth taking a look at for another thing I might need it for.

I also found another interesting thing that could help: https://docs.craftcms.com/v3/reference-tags.html . With Crafts build in reference tags I can reach the entry and assets from within the redactor-field, but so far couldn't manage to reach the actual matrix block the redactor field is in. If I could do that I could get to the asset field which is a sibling of the rich text field.

That would help getting a dynamic link to an asset to put in the shortcode for example. Which is dynamic and an object, so all dynamic data is there. But not sure if reference tags supply the matrix block.

Anyway, thanks again and take it easy.

@Friksel I tagged an alpha release with the new functionality if you'd like to take it for a test drive.

You probably know how to do all of this, but just in case not...

Add to /composer.json:

{
  ...
  "minimum-stability": "alpha",
  "prefer-stable": true,
  ...
}

Then in terminal:

composer require samhernandez/craft-shortcodes:1.1.0-alpha.1
# Install from the CP or from the command line like so:
./craft install/plugin craft-shortcodes

My full test site composer.json looks like this:

{
  "require": {
    "craftcms/cms": "^3.4.0",
    "craftcms/redactor": "^2.6",
    "elivz/craft-transient": "^1.0",
    "mildlygeeky/craft-kint": "^1.0",
    "samhernandez/craft-shortcodes": "1.1.0-alpha.1",
    "vlucas/phpdotenv": "^3.4.0"
  },
  "require-dev": {
    "yiisoft/yii2-shell": "^2.0.3"
  },
  "minimum-stability": "alpha",
  "prefer-stable": true,
  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },
  "config": {
    "sort-packages": true,
    "optimize-autoloader": true,
    "platform": {
      "php": "7.0"
    }
  },
  "scripts": {
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ]
  }
}

My testing template looks like this:

    {#
       `entry: entry` is not necessary because the plugin adds
       it by default already. It's here just to show that you can override it.
    #}
    {% for block in entry.contentModules.all() %}
        {{ block.text | shortcodes({
            context: {
                entry: entry,
                block: block,
                field: block.text
            }
        }) }}
    {% endfor %}

The Twig shortcodes filter accepts an options object and for now it supports a context parameter.

This works for me but I haven't tried to break it yet. I'll test it thoroughly in the evening but if you have time to give it a go, that would help a lot! We can get this update out the door this week.

That looks very promising! I give it a try

@samhernandez It's working fantastic and it fit the use case too, which is now finished with this: I'm having a matrix block with a Redactor field and an assets-field. The assets all have image-keys that can be used with a shortcode in the redactor-field. And now the image shortcode gets that key, finds the image from the assets field on key and inserts the image.

With this Craft always knows the usage of images which is not the case when just inserting images in redactor, because all assets loaded in the matrix block's assets field are all meant to go on the redactor field and are in use. And can add special layouts to them.

So with this change the target is checked! Nice to be able to set more than one context too in the shortcode filter.

Thanks a lot for this service and your quick responses and action! If this was the only thing for that alpha I bet it's good enough to release. Have a nice day!

Awesome, thanks for the QA work! I'll get a real release out soon so you can get your composer.json file back to where it should be.

Hi @samhernandez it's been around three weeks. Any chance you could find the time to release the new version? Or are you waiting / is there something that needs to be tested or done first? Thanks