"Listen" to (extension){"id":123} in content

  • Hello there,


    is there a documentation how to "listen" to entries like (extension){"id":123} in content and replace those entries? I already have an idea how I could possibly solve this, but this is not matching the ways bixie (https://github.com/Bixie/pagekit-formmaker) and Tobbe (https://github.com/tobbexiv/pagekit-widget-includer) are doing this.


    If I try to do it the way they do, the entries are not replaced with content - they are just replaced with an empty string.


    I'd love to include such a functionality in one of my extensions and I would like to do it correctly.


    If you could link me the corresponding part in the docs and/or explain it here, I would be very happy :)

  • Hi,


    I don't know if there is something in the docs, from what I remeber there was nothing. But let me explain it shortly here :)


    A plugin is identified by this coding:

    (<pluginName>){<pluginData>}

    <pluginName> is a Name you can choose freely (but everybody else can choose the same name, so be careful).

    <pluginData> is a JSON string with the data which is parsed and then delivered to your callback.


    The necessary coding is not that much (I chose to have my own file for the plugin for better clarity):

    1. You need to create a plugin: WidgetPlugin.php
      This plugin needs a subscribe method (forced by the event listener) which registers your plugin on the content.plugins event and gives the method that registers your plugin.
      Also you need an onContentPlugins method which tells the event handler the name of your plugin and which method to call as callback.
      The last thing in this class is your callback method (here applyPlugin) which returns a string. This string then is inserted at the same place where your previous plugin content was. It is not necessary to call other classes here, the external call within my method is only for simplification of my own coding.
    2. In step 1 you defined a plugin which is never subscribed as the application does not know to subscribe it. To do this you must subscribe your plugin class in the main method of the module: $app->subscribe(new WidgetPlugin()); In my case I only define a callback for the main in the index.php thus it is not called directly there,

    Did this explanation help you?


    Best regards,

    Tobbe

  • Hm, still did not get this working in my test-scenario...


    My plugin is called and I created this method:



    The method applyPlugin returns a string like that:



    Code
    1.     /**
    2. * Defines the plugins callback.
    3. *
    4. * @param array $options
    5. * @return string|null
    6. */
    7. public function applyPlugin(array $options)
    8. {
    9. return "I love Pagekit";
    10. }



    Now I insert (mytest){ } or (mytest){ "id":"1" } to a page and save this. But the page is rendered without echoing the string "I love Pagekit".


    I tested whether the onContentPlugins-method is called using:

    Code
    1.     /**
    2. * Content plugins callback.
    3. *
    4. * @param ContentEvent $event
    5. */
    6. public function onContentPlugins( ContentEvent $event )
    7. {
    8. $event->setContent("Test");
    9. }


    Now "Test" is echoed, so I assume the onContentPlugins-method is called and I did an error somewhere else.