1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Having trouble with custom like handler...

Discussion in 'XenForo Development Discussions' started by Jaxel, Feb 25, 2011.

  1. Jaxel

    Jaxel Well-Known Member

    So I've made a custom like handler... you can see it here: (must be logged in to like)

    It works great at liking, it adds likes, subtracts likes, and works through ajax without any issues. The likes also appear properly on my "account/likes" page.

    Likes You ve Received   8WayRun.Com - Soulcalibur Community.png

    However, I have run into a few issues that I have no idea how to fix:
    1. Likes do not appear on "recent-activity"
    2. Likes do not appear on "news feeds"
    3. When someone likes a media, the alert system says I have an alert (red marker), however when I go the alert tab, it says I have no new alerts. The like alert also does not appear in my "latest alerts" page.
    This is the contents of my like handler:
    class EWRmedio_LikeHandler_Media extends XenForo_LikeHandler_Abstract
    	public function incrementLikeCounter($contentId, array $latestLikes, $adjustAmount = 1)
    		$dw = XenForo_DataWriter::create('EWRmedio_DataWriter_Media');
    		$dw->set('media_likes', $dw->get('media_likes') + $adjustAmount);
    		$dw->set('media_like_users', $latestLikes);
    	public function getContentData(array $contentIds, array $viewingUser)
    		$mediaModel = XenForo_Model::create('EWRmedio_Model_Media');
    		$medias = $mediaModel->getMediasByIDs($contentIds);
    		return $medias;
    	public function getListTemplateName()
    		return 'news_feed_item_media_like';
  2. Jaxel

    Jaxel Well-Known Member

    I really dont get what I'm supposed to do to get these alerts and news feeds working...
    When a like is added, it automatically adds entries to the `xf_news_feed` and `xf_user_alert` tables. I can look at the database and see that these entries are there; but in the actual news feed on the website, and my alerts panel, they are not.

    What am I missing? I have templates for alert_media_like and news_feed_item_media_like
  3. Bob

    Bob Well-Known Member

    maybe you need to extend the XenForo_AlertHandler_Abstract as well? did you look into the alert handlers? library\XenForo\AlertHandler
  4. Kier

    Kier XenForo Developer Staff Member

    You need to publish the story to the news feed and send alerts yourself - if alerts are being sent but are not visible, you may simply need to create the appropriate template.
  5. Jaxel

    Jaxel Well-Known Member

    I have the following file:
    class EWRmedio_AlertHandler_Media extends XenForo_AlertHandler_DiscussionMessage
    	public function getContentByIds(array $contentIds, $model, $userId, array $viewingUser)
    		return $model->getModelFromCache('EWRmedio_Model_Media')->getMediasByIDs($contentIds);
    Alerts don't work. I have not added any code event listeners...

    What templates are needed? From looking at the database, it looks like the LikeHandler publishes like alerts automatically. Since both database tables are filled with the media/like data.

    Content Type = media
    Action = like

    I have templates for "alert_media_like" and "news_feed_item_media_like"
    {xen:phrase x_liked_your_media_y,
        'name={xen:helper username, $user, 'subject'}',
        'link={xen:link media, $content}',
    <h3 class="description">
        <xen:if is="{$content.user_id} == {$visitor.user_id}">
            <xen:comment><!-- $user liked your status --></xen:comment>
            {xen:phrase x_liked_your_media_y,
                'name={xen:helper username, $user, 'primaryText'}',
                'link={xen:link media, $content}',
        <xen:else />
            <xen:comment><!-- $user liked $contentUser's media --></xen:comment>
            {xen:phrase x_liked_ys_media_z,
                'name={xen:helper username, $user, 'primaryText'}',
                'link={xen:link media, $content}',
    <p class="snippet">{xen:helper snippet, $content.media_description, 100, {xen:array 'stripQuote=1'}}</p>
  6. Kier

    Kier XenForo Developer Staff Member

    Refreshing my memory on XenForo_Model_Like, it looks like the system automatically sends alerts and publishes news feed items, so it should be the case that you just need to provide the appropriate templates to display the data.

    Those templates need to be:
    • news_feed_item_{contentType}_like
    • alert_{contentType}_like
    Have you created these? Can you verify that they are being called / used?
  7. Kier

    Kier XenForo Developer Staff Member

    Aah, looks like you already replied.

    Can you verify that the data is being inserted into the alert and news feed tables correctly?
  8. Jaxel

    Jaxel Well-Known Member

    Attached is a screenshot of the two tables... it looks correct to me...
  9. Jaxel

    Jaxel Well-Known Member

    I have not extended any alert classes... I have not added any code event listeners. Do I need to?

    I also have a related question... I've set up the alert preferences below. How do I implement them once I finally get the alerts working for these likes?
  10. Kier

    Kier XenForo Developer Staff Member

    You need to create an alert handler and a news feed handler so that the system knows how to fetch the data referred to in the news feed and alert tables.

    For alert preferences, you really just need to extend the account_alert_preferences template using the available template hooks and provided the form fields are named correctly, everything else should be handled for you.
  11. Jaxel

    Jaxel Well-Known Member

    What do I need to do to extend the alert and news feed handlers? Do I need to setup code event listeners?

    I have already extended account_alert_preferences... but I dont get how or where to use the settings; I've tried searching for variables based on the existing alert preferences, but there is nothing. The way handlers work in XenForo seem completely undocumented and are very hard to follow.
  12. Jaxel

    Jaxel Well-Known Member

    Nevermind, I got it... all I had to do to get alerts working was to create a content_type_field for it... trying to get the news feed content_type_field working now...

    I still dont understand how to use the data in account_alert_preferences though...

    You also said something earlier about manually publishing alerts and news feeds... how do I do this?
  13. Onimua

    Onimua Well-Known Member

    I believe that you use it to check whether or not the user has chosen to be alerted for that item via XenForo_Model_Alert::userReceivesAlert() which checks xf_user_alert_optout, at which point (if it returns true) you actually then send the alert to the user.

    Of course, I have been trying to figure this out myself as well... maybe Kier can make another video? :p

    EDIT: Okay it took me a while but I get this alert thing myself as well. :)
  14. Jaxel

    Jaxel Well-Known Member

    Onimua... I've figured it out... I would mention it, but its a lot to cover, adn I dont have the time right now.
    Bob likes this.
  15. Bob

    Bob Well-Known Member

    Sweet :D Its all in the latest release of the media addon? Mine is slightly different as I am firing alerts via cron based on certain date/time events within the addon. Think I almost got it tho...

    btw, keep up the great work on your Addons Bro.. they ROCK!
  16. PhpMember

    PhpMember Member

    My alert handler works fine but I am having issues with the news feed handlers, It has the content_type entry and field entry, and it seems it inserts the data into the news_feed table correctly, though it doesn't pull any of it when I try to pull from NewsFeed, I have also created templates like:

    news_feed_item_{content_type}_insert, but it is still not pulling anything, do you know what I could be doing wrong or left out?
  17. PhpMember

    PhpMember Member

    nvm got it working.. it took me like 5 hours to figure this out, ahhhh!. I was getting just one item with getContentByIds(), but I had to get it all.. that fixed the issue, and it is pulling the data now.

Share This Page