Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Comment-rendering events for embedded content

MadebyfenMadebyfen New
edited December 2015 in Vanilla 2.0 - 2.8

Hi, I'm writing my first Vanilla plugin for embedding some content in discussions/comments. I don't want to save any extra information with the posts (dirty markup), and I want to let the 'front end' do the lifting, so I'm approaching it like this:

  • Hook into comment-rendering events and search for content URLs using regex.
  • Wrap URL in a tag to mark it for embedded content (a span tag works well here as it's 'safe' HTML)
  • Use JS to do the embedding (find the span, get the URL, magicmagicmagic)

This works well using DiscussionController_AfterCommentFormat_Handler, but of course only works on page render. I'm looking to cover all bases, so that the rendering can happen after a user posts, or after editing a comment. These events appear to be more JS-related, as DiscussionController_AfterCommentFormat_Handler doesn't seem to hook into these actions, and I can't find any docs on Vanilla's JS.

I guess my main questions are: am I approaching this correctly? Would those of you with more insight approach this differently? If the approach seems sane, what events (PHP or JS) should I be hooking into so that I can do the rendering on posting/editing a comment? The Eventi plugin appears to have fallen behind the times and isn't a lot of help!

Cheers :)

Comments

  • Yes, this is a good approach. Use format_links_handler

  • Are you trying to automatically create an embed if a certain link is found?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Correct. As mentioned, it works well when rendering a whole page, but I'm just really looking for an event I can hook into when posting/editing, too.

    Thanks @Bleistivt -- I'll take a look at format_links_handler :)

  • Yep, Format_Links_Handler was just what I was after, thank you @Bleistivt

  • MadebyfenMadebyfen New
    edited December 2015

    Having said that; how do I get hold of the comment body from Format_Links_Handler? I can get hold of a Gdn_PluginManager object as a parameter to the callback, but this isn't too useful here.

    DiscussionController_AfterCommentFormat_Handler provides a DiscussionController instance when rendering a page, and a PostController instance when posting, but nothing when editing (or rather, that event isn't fired when editing)...

  • The post controller also handles editing of comments and discussions. Check the event arguments for data.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • After some code-tracing, I now realise that FireEvent provides a second parameter to its associated callback containing event arguments, so I'm doing something like this:

    public function Format_Links_Handler($Sender, $Args) {
        $this->plugin->markEmbeds($Args['Mixed']); // 'Mixed' is the comment body
    }
    

    Thanks for the help.

Sign In or Register to comment.