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.

Handling BeforeEmbedDiscussion event

I am new to Vanilla and can't seem to figure out how to handle the BeforeEmbedDiscussion event. This is called when you first post to a discussion that doesn't exist via embedded comments.

The event is fired like this in PostController::Comment:

 $this->EventArguments['Discussion'] = $EmbeddedDiscussionData;
 $this->FireEvent('BeforeEmbedDiscussion');
 $DiscussionID = $this->DiscussionModel->SQL->Insert(
        'Discussion',
        $EmbeddedDiscussionData
 );

My question is, how to I change the data that is attached to the event? I want to update the text in the Body of $EmbeddedDiscussionData

Here is my method:

public function PostController_BeforeEmbedDiscussion_Handler($sender)
{
    $discussion = $sender->EventArguments['Discussion'];
    $body = $discussion['Body'];
    $attributes = unserialize($discussion['Attributes']);
    $productUrl = $attributes['ForeignUrl'];

    $newBody = str_replace('Read the full story here.', $productUrl, $body);
    $discussion['Body'] = $newBody;
    $sender->Discussion = $discussion;
}

Am I even close? The documentation is quite sparse for examples on this :\

Tagged:

Comments

  • hgtonighthgtonight Moderator

    You found a potential bug.

    Generally event arguments are assigned by reference if the intent is to potentially change the data in the event handler. This does not appear to be the case here. This may have been intentional.

    Changing line 444 of class.postcontroller.php to $this->EventArguments['Discussion'] =& $EmbeddedDiscussionData; would solve your issue.

    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.

    lfolco
  • So how could I update the embedded discussion body then? Even if the event handler data is updated, how does the value of $EmbeddedDiscussionData; get updated, that is being referenced in line 5 above? Does my event handler have to return something?

  • hgtonighthgtonight Moderator

    This will actually be fixed in the next version thanks to you :)

    By assigning the event argument by reference, we are not creating a copy, but a link to $EmbeddedDiscussionData. By changing $sender->EventArguments['Discussion'] you are effectively modifying $EmbeddedDiscussionData in the post controller.

    The last line of your event handler should be changed to: $sender->EventArguments['Discussion'] = $discussion;

    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.

    lfolcoBleistivtR_J
  • scratch that last comment, I had a typo. :)

  • it looks like this didn't make it into 2.1.9? i just downloaded it and the bug is still there :\

  • hgtonighthgtonight Moderator

    @lfolco said:
    it looks like this didn't make it into 2.1.9? i just downloaded it and the bug is still there :\

    It made it into the master branch, so I would expect to see it in 2.2.

    Until it gets released, you will have to re-apply the patch when updating.

    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.

Sign In or Register to comment.