Add 'Like' Button to Widgets on Profile Page

Hello -

Can anyone please assist me in explaining how to add the like button to widgets on profile pages?

I'm currently using the core like plugin. From looking at the code, I think that the plugin automatically disables the like button if its in the widget context.

It seems like it'd be easier if I could modify the core plugin, but maybe I should create a new plugin fresh?

For either route, an explanation of how to go about this would be very helpful -- I am a little unfamiliar still with some of the Elgg methods and terminology.

Thanks all.

  • Create a plugin skeleton with only the required files: http://docs.elgg.org/wiki/Plugin_skeleton#Required_Files

    Register a plugin hook handler for the hook 'register', 'menu:entity' using these instructions: http://docs.elgg.org/wiki/PluginHooks

    Inside your new handler register the Like button the same way as it is registered in the likes plugin in the function likes_entity_menu_setup(). Just leave out the part where the handler returns true in case user is in the "widgets" context.

  • Great - thanks for your help - I will give that a try.

  • I've tried to make these revisions but it doesnt add the like button to the widgets.

    I created the manifest and the start file. In the start file, I registered the button the same as in the core Likes plugin, without the return true in the widgets context.

    I'm not sure if there's something wrong with this code -- or if there's something else that is missing that needs to be added?


    elgg_register_plugin_hook_handler('register', 'menu:entity', 'widget_likes_entity_menu_setup');


    /**
     * Add likes to entity menu at end of the menu
     */
    function widget_likes_entity_menu_setup($hook, $type, $return, $params) {

        $entity = $params['entity'];

        // likes button
        $options = array(
            'name' => 'likes',
            'text' => elgg_view('likes/button', array('entity' => $entity)),
            'href' => false,
            'priority' => 1000,
        );
        $return[] = ElggMenuItem::factory($options);

        // likes count
        $count = elgg_view('likes/count', array('entity' => $entity));
        if ($count) {
            $options = array(
                'name' => 'likes_count',
                'text' => $count,
                'href' => false,
                'priority' => 1001,
            );
            $return[] = ElggMenuItem::factory($options);
        }

        return $return;
    }

     

  • Try printing out something inside start.php and inside your handler. That way you can see it they get called in the first place.

  • Ah - I see, I didn't realize I had to register the initialization event... So I can now print out within the init function but when I print out within the handler, it doesn't appear -- although I'm still not clear on certain things about the views, so I'm not sure if a simple echo even should work within this function...? Thanks so much for your help.

     

    <?php
    /**
     * Widget Likes plugin
     *
     */

    elgg_register_event_handler('init', 'system', 'widget_likes_init');

    function widget_likes_init() {
        elgg_register_plugin_hook_handler('register', 'menu:entity', 'widget_likes_entity_menu_setup');
    }


    /**
     * Add likes to entity menu at end of the menu in widgets
     */
    function widget_likes_entity_menu_setup($hook, $type, $return, $params) {

        echo "<h1>test</h1>";

        $entity = $params['entity'];

        // likes button
        $options = array(
            'name' => 'likes',
            'text' => elgg_view('likes/button', array('entity' => $entity)),
            'href' => false,
            'priority' => 1000,
        );
        $return[] = ElggMenuItem::factory($options);

        // likes count
        $count = elgg_view('likes/count', array('entity' => $entity));
        if ($count) {
            $options = array(
                'name' => 'likes_count',
                'text' => $count,
                'href' => false,
                'priority' => 1001,
            );
            $return[] = ElggMenuItem::factory($options);

        }

        return $return;
    }

     

     

  • Echo works inside the hook handler but the message will propably appear outside the elgg basic layout. Most likely in the top left corner of the browser window.

    Your plugin is using views from the likes plugin: 'likes/button' and 'likes/count'. Do you have the original likes plugin enabled? You either have to enable it or copy those views from likes plugin to your own.

    (Btw, might be good to use http://pastebin.com/ or similar service when pasting a lot of code. Makes it easier to read.)

  • Yes, the original likes plugin is enabled. This new 'widget likes' plugin is also enabled, at the bottom of the plugin list.

    When I went to check about the plugins being enabled, I went to the admin dashboard and discovered that the test echo statement is being printed -- but only on the widgets within the admin dashboard. The test print statement appears above the username in the online users and new users widgets.

    I'm guessing the next step will be to specify that the like button should display in the context of the profile. I'm not sure how to do that though -- if you could help point me in the right direction. Thanks so much for helping to figure this out (and the reminder about pastebin).

  • the next step will be to specify that the like button should display in the context of the profile

    Well do just that. ;)

    if (elgg_in_context('profile')) { ...

  • I set the context to profile and that stopped the test text from appearing in the admin widgets. Thanks for your help with that. The like button still isnt appearing in the profile widgets though.

    I found that both the likes mod and the navigation file in the core engine library had this:

    // comments and non-objects cannot be commented on or liked
    if (!elgg_in_context('widgets') && $item->annotation_id == 0) {

    I tried deleteing the 'not in widgets context' part. But that didnt solve it either.

    So maybe the button needs to be explicitly added to the profile widgets as a view? I'm still a little unfamiliar with the differences between entities, objects, types and subtypes -- and what can be annotated vs what can't be annotated.

    It looks like the core likes plugin registers the hook explicitly for the river menu ('register', 'menu:river', 'likes_river_menu_setup') and then for the entity menu ('register', 'menu:entity', 'likes_entity_menu_setup'), but in the core navigation file there seems to be a seperate setup for the widget menu (function elgg_widget_menu_setup).

    So maybe there should instead be a separate hook that adds the button to the widget menu (even though it won't really appear in the widget 'menu' which is the icons for widget settings, delete widget, etc)?

  • Never edit any of the core files!

    Maybe your plugin hook handler is loaded too late. It adds the likes icon but then some other handler removes it afterwards.

    Try adding a priority parameter to your handler like this:

    elgg_register_plugin_hook_handler('register', 'menu:entity', 'widget_likes_entity_menu_setup', 999);

    (Default priority is 500 and likes is using 400 so 999 should be more than enough.)