enable/disable widget to show depending on certain conditions

Hi all.

There's a problem: I need to completely disable a widget to show it to a user viewing the profile page of another user (someone's else profile).
Is it possible, and if yes, how I can do this?

  • Some of my comments to "Delaying the system loading of widgets???" may be applicable.  Basically you need to undo the effect of an add_widget_type() call.

  • PLai, thank you for your answer.

    Unfortunately, the discussion your give me link for is inapplicable for me. I try to determine what the profile user request to view, and if it's someone's else profile - do not to call the add_widget_type() while 'init', 'system' event handler of the plugin that define this widget is executing, but widget is shown anyway. Also I trying to use the remove_widget_type() function, but there's no results too.

    If someone have any ideas of how to completely hide the widget if it's undesirable - I'll be very grateful.

  • I'm not sure how possible this is. I have played around with widgets and have managed to restrict their access to certain usertypes on my site. Here is what I'm using:

     

    if(isloggedin() && $_SESSION['user']->usertype == "Teacher"){

    add_widget_type('pages',elgg_echo('pages'),elgg_echo('pages:widget:description'));

    }

     

    If the user is not a teacher on my site then when they try to add widgets to their profile this widget doesn't appear. This code goes in the start.php of the plugin where the widget comes from. But I get the feeling this won't solve your problem, because what you want is to display a widget on everyone's profile, but only allow non-profile-owner users to see it. I have no idea how to actually do that. What's more you need to be aware that the profile-owner can remove the widget from their profile when going to the 'edit page' link if they wanted to.

  • Trajan, thank you, but this is not in my case. Every user must have this widget when he looking on his profile, however I need to hide the target widget when another user is viewing the profile of someone else.

  • Alex, you are right.  Avoiding add_widget_type() does not hide a widget.  My reading of the code was not complete.

    The solution may actually be much simpler.  You can just override the "widgets/wrapper" which is used to display a widget.  Just put a views/default/widgets/wrapper.php to a plugin of your own.  The following would hide the the blog widget:

    <?php
    if ($vars['entity']->handler == 'blog') { /* whatever test you want */
        echo '<div class="widget-hidden"></div>';
    }

    else {

        /* this is assuming that the default widget wrapper is used;
         *  i.e. no other plugin cares to override 'widgets/wrapper'
         */    
        global $CONFIG;
        include($CONFIG->path.'views/default/widgets/wrapper.php');
    }
    ?>

    Hope this helps.

  • Hi,
    I have tested it and it works. I guess if this is what Alex Shu wants then it will help. Maybe it is a nice idea to make a plugin.
    Regards.

  • Alex Shu Just a small issue, remember to disable and enable the plugin where you put the new view.
    Regards.

  • Thank you all who give me an answers!

    PLai - it's really great idea, this is the solution I'll use.

    gushbellino - do you mean that I should toggle the plugin OFF and then ON? but what exactly should happen by these actions? If you mean the troubles with simplecache - it's not a problem because the site is in DEV mode and simplecache now disabled. If there's some else - please describe in more detail, so I better understand the mechanism used for this.