Show Panel When User Logs In

gourmet fare

Enable Content Block On Login

You want a popup or panel to show when a user logs in.

modxnooby asked...

I have a website where there are many slide out panels that can be popped into view by clicking on tabs and i want one that is on the bottom to appear only when the user is logged in. So I did some digging and I thought why not use some kind of event?

The Solutions

sottwell says...

I have a plugin using OnWebLogin

  $_SESSION['loginmessage'] = 'true';

Then a snippet (the login message is itself a resource for easier editing by the client since they change it a lot)

// only do all this if the $_SESSION value is set
if(isset($_SESSION['loginmessage']) && $_SESSION['loginmessage'] == 'true') {

  // the JQuery code for popup position and behavior
  $js = '
  <script type="text/javascript">
      $(function() {
          $( "#popupMessage" ).dialog({
               position: { my: "right bottom", at: "right bottom", of: window },
               hide: {effect: "fade", duration: 6000}
          setTimeout(function(){ $("#popupMessage").dialog("close"); }, 8000);
  // insert the javascript at the bottom of the page
  // use a resource for easy editing of the popup's content
  $document = $modx->getObject('modResource', 58);
  // clear the variable so this won't show on subsequent pages
  $_SESSION['loginmessage'] = '';
  // load the content for the popup
  return $document->get('content');

// don't return anything if the $_SESSION value isn't set

Kristoffer says...

If you want to show something only for logged in users, I would suggest the Personalize snippet. See for more details.

What's the Story?

The two solutions provide different functionality, depending on what you want.

Alternate OutputThe Personalize snippet will allow you to display the content block wherever you want, for as long as the user is logged in. It is an excellent solution for providing a small login form if the user isn't logged in, and a "Welcome, username" block if he is logged in. It's also good for providing panels or other menu-type links for logged-in users - you can use an empty chunk as the tpl where you don't want anything to show. Simply adding the Personalize snippet tags to your template will insert the appropriate content on every page using the template.

But in the case where you want the content to appear only once, when the user logs in, the plugin solution will be a better choice.

One way to do such a thing would be to develop your content block as a snippet, having it show at all times, so that you know it works. Then create a plugin to set a $_SESSION variable on login, and modify your snippet just a little, surrounding it with an "if" conditional, so that it only returns the content when that $_SESSION variable is set. Unset the variable after returning the content, and it won't be displayed on subsequent pages. If you want the content displayed to the logged-in user all the time, don't unset the $_SESSION variable.

Using a $_SESSION variable and a snippet will allow you to only display the content on pages where you want it, whereas having all of the code in the plugin would display it on all pages. The snippet will need to be called uncached, so that its output (or lack of output) isn't cached as part of the pages.