We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43406
    • 15 Posts
    How to best achieve the below objective?

    I want the site editors to be able to tick a box on the template to indicate they want to make the current page a 'feature article'. What this does is will make it show up on the front page (and potentially other pages where applicable) feature banner.

    This banner is a rotating banner that shows various things including a banner image of any feature articles (set using the above tick box).

    I also want to have some way for the editor to indicate the period that this article should be shown on the feature banner (i.e only show during promotion period etc). Basically allow them to set a start and end date.

    I also want to have an image template variable which is the image that will be shown on the banner. User will be forced to choose an image if the "Show as Feature" box is selected.

    I plan to implement as per below but don't know how to make it so that when the editor tick the "Show as Feature" template variable the date & image then is compulsory and must be selected?

    My searching haven't turn up any way to force a user to enter data for those TV.

    Is there a better way to implement this?

    Also is implementing the "show as feature article" the way I've done it the best way. One must then loop through all pages to find out if it should be shown as a feature article (like wayfinder menu?). Will this make it slow if there are hundreds of pages to loop through. I want to make this as fast as possible especially for the front page to load quickly.

    Thank you in advance for any recommendation on how to do the above.
      • 3749
      • 24,544 Posts
      How about just having the image TV and showing any pages where it's set? You could still call the TV "Show as Feature" but all the user would have to do is select an image.

      If the default value of the image TV is empty, you can retrieve the non-empty ones fairly fast by doing a query on modTemplateVarResource.

      /* get only TVRs where the image TV has a value */
      $tvrs = $modx->getCollection('modTemplateVarResource, array('tmplvarid' => '12'));
      
      /* Get the image path and Resource info here */
        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
        • 43406
        • 15 Posts
        Thank you for the suggestion. That was I was thinking of doing (display image if one was selected).

        I'm guessing from the lack of comment that it is not possible to make sure the user select a date if they choose an image to display?
          • 4172
          • 5,888 Posts
          with a plugin for onBeforeDocFormSave you can check, if a date is selected and it should be possible to return a failure-message and stop saving, if not.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 43406
            • 15 Posts
            I'm no pro so please ignore my dumb question. But where do I find / put onBeforeDocFormSave?

            How would this work if I have different templates that have different requirement (i.e one template needs x,y,z to be selected if A is selected and another something totally different)
              • 3749
              • 24,544 Posts
              You check it on the System Events tab of the plugin. TBH, though, I have not had much luck with aborting a save in OnBeforeDocFormSave. It's clearly supposed to work, but when I've tried it, the save occurs anyway. I didn't spend much time looking into it, so my experience might not be typical or I might have been doing something wrong.
                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
                • 4172
                • 5,888 Posts
                something like that should work:

                <?php
                $image = $modx->getOption('tv10',$_REQUEST,'');
                $date = $modx->getOption('tv20',$_REQUEST,'');
                
                if ($resource->template == '15' && !empty($image)){
                    $valid = true;
                    //validate date here, if not valid -> $valid = false;
                
                    if (!$valid){
                        $modx->event->output('enter a valid date!');
                    }
                }	
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 43406
                  • 15 Posts
                  Bruno17, Thank you for that.
                    • 3749
                    • 24,544 Posts
                    @Bruno17: Shouldn't that have a return?

                    @orionweb: Let us know if it works.
                      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
                      • 43406
                      • 15 Posts
                      It's getting a little complex and messy if I have to do this for mulitple templates. I am going to have a think about it and might just let the user remember to put an image & date. If none supplied assume not a feature.

                      I forgot to ask, how do you tell if an image was selected (when using get resource to get list of page with image selected)?