We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7804
    • 80 Posts
    Hello All,

    I'm using 2.3.3-pl. I am not a very experienced MODx developer (yet) and therefore wanted to seek the community's wisdom on how best to accomplish this. I have a need for a configurable "site alert" that, when toggled on, shows some user-supplied content on every page of the site:

    • The site alert should be configurable (be able to be turned on / off) by a standard editor user all in one place within the manager
    • The actual alert content itself (usually just some text) should be specified here as well
    • The interface for configuring it should be usable by a basic editor with no HTML coding required
    • This should also be extensible to support additional properties, e.g. ability to specify an image (from a group of presets) to show with the alert, ability to specify an "unpublish" time / date for the alert, ability to set a "more info" link with the alert, etc.

    If you've ever visited cnn.com when they've had a "breaking news" notice in their site header, then this gives you a basic idea of what I'm trying to accomplish.

    I am looking for some guidance on how to best go about implementing this feature for my client. I'm not necessarily looking for a step-by-step tutorial, but just some help setting my feet in the right direction on this. The only solution I've arrived at thus far is to create a chunk with output modifier in the site template, and then have my site editor manually edit the chunk. But this implementation adds margin for (client) error and doesn't accommodate the level of customization I want to provide.

    This question has been answered by dubrod. See the first response.

      • 3749
      • 24,544 Posts
      I think you might be better off using a resource rather than a chunk for the content. You'd need a custom snippet to do the logic for you (rather than an output modifier). Resources can be set to be published and unpublished at certain times and your snippet could use the 'published' field of the Resource to decide whether to show it or not. You could use a drop-down-list TV for the images, with the TV value holding just the path to the image.

      The snippet (I called it ShowNews) would look something like this (I've called the TV 'NewsImage'):

      /* ShowNews snippet */
      $docId = 12; // ID of the resource holding the content and TV
      
      
      $doc = $modx->getObject('modResource', docId);
      
      if ($doc) {
         if ($doc->get('published')) {
             $modx->setPlaceholder('news_content', $doc->get('content'))
         }
         $tvValue = $doc->getTVValue('NewsImage');
         if (! empty($tvValue)) {
             $modx->setPlaceholder('news_image', $tvValue);
         }
      }
      
      return "";
      


      You'd create a snippet with the code above (fixing the first line), then put a tag for that snippet at the top of the body section of the template:

      <body>
      [[ShowNews]]

      Then, in the template where you want the content and image to appear, you'd put the two placeholders set in the snippet:

      <div class="news_content">
          [[+news_content]]
      </div>



      <img src="[[+news_image]]" alt="News Image">



      I've made the snippet tag cached since it will be much faster and the cache would be cleared when you save the resource with the content.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 7804
        • 80 Posts
        Thank you, Bob! I'll give this a shot and let you know how it goes.
        • discuss.answer
          • 47838
          • 6 Posts
          This is a good example of when to use custom Collections.
          Setup a Collection called Alerts and use the standard call features to render any alerts.

          http://rtfm.modx.com/extras/revo/collections

          It would be very similar to how I setup this slider demo - https://www.youtube.com/watch?v=rnA5NtaLaYY
            MODX Front End Dev
            • 7804
            • 80 Posts
            Thank you both again for your input. I wound up going with Bob's solution. Dubrod, I watched your demo vid several times and actually went through the process of setting up a collection, and while I can see how this would be useful in certain cases, it was not ideal here because I will only ever have a single alert shown at any given time. Collections were overkill for this use case, but I definitely see their value and will use them in the future when the need presents itself.

            Bob, your method sets placeholder values, and I'm having to use quite a few output modifiers to get everything to work properly on the front end. However, on your website you state:

            And don't get me started on conditional output modifiers. These were created to let people who were not able to use PHP code create conditions that determined what was shown on the page. They are notoriously slow, however. Unless your server is blindingly fast, you should either learn to write simple snippets to replace your conditional output modifiers or hire someone (like me, for instance) to replace them for you.

            Does this still apply to revo? If so, I'll convert my placeholder logic to a pure PHP implementation.
              • 3749
              • 24,544 Posts
              Yes, they are still slow, though less so when the page can be cached and the tag containing the modifiers is called uncached (no !). Still, the first person to visit the page after the cache is cleared can suffer.

              Another problem is that the output modifiers will often process everything in the tag, even though it won't be used. Sometimes you can prevent that by rewriting the tag, but not always.

              If you want to post your problem tag, we can suggest a more efficient snippet version.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 7804
                • 80 Posts
                Bob, thanks for the offer. I was able to implement a fairly basic PHP solution which does the job nicely.
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. It should be *way* faster than using conditional output modifiers.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting