We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5950
    • 44 Posts
    First off, Bob, bless you for creating NewsPublisher. It is exactly what I needed for my current project. Thank you also, for diligently labeling your templates and commenting your snippets!

    My project requires a new/enhanced feature, though (don't they all?).

    I'm working on a product review website, where authors fill in values for "Manufacturer" "ProductName" "ProductNumber" and "ProductLine". From that information, I want to automatically create the article's title upon submit.

    First I defined outertpl as a duplicate of npOuterTpl, which had a hidden field for pagetitle. The value was set to a phrase made up of dynamic calls for "npx.ProductLine npx.ProductName (npx.Manufacturer npx.ProductNo)". Not surprisingly, because the form can't know the values before they're submitted, the final pagetitle output was " ( )" - but I was, at least, encouraged that the pagetitle would be accepted via hidden input.

    Next, I created a copy of NewsPublisher called ReviewPublisher, and poked around for the right place to set a default for pagetitle. It is not, of course, a property that is actually set in NewsPublisher, so I am at the point where I think I must assign my string to the pagetitle during the validation stage.

    I am extremely rusty with php (thanks in part to everyone who already wrote snippets that do exactly what I need). So, before I start mucking about in ReviewPublisher, I wanted to throw my idea out here and see if anyone else had already done something like this or knew quickly how to approach it. I'll describe my hoped-for logic using English:

    What I know:
    If there is no field for pagetitle in the form, pagetitle doesn't get set. I know this to be true because, when I tried to leave pagetitle out of the form, I got an error message saying the field was required. This supports my idea of just checking that field for a value and, if it's null, telling the snippet what I want to do about it.

    In Case A (ProductReview):
    If the field for pagetitle is not included in the form, then we are in a Review post and pagetitle's value should be set to "ProductLine ProductName (Manufacturer ProductNumber)" (the code should check each one for content before including it and surrounding spaces in the string). If none are set, the user should receive an error about filling in some identifying product information.

    In Case B (Blog Post):
    If pagetitle is included in the form (in which case we are in a Blog post and the TVs mentioned above would NOT be in the form), the user will need to fill it in.

    I'm happy to do this using separate Snippets for each case, adding in hooks, other snippet calls, etc.

    Thanks so much in advance for taking the time to read this. I'll update as I try/fail/succeed.
      • 4172
      • 5,888 Posts
      sounds like you should think about using formit with hooks something like a custom formit2resource - hook and some custom validators.

      May be you should also think about storing the reviews into a custom-table.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 5950
        • 44 Posts
        Thanks for your response Bruno.

        I thought I had mentioned it in the original post (I must have over-edited before posting), but I am using the Articles package to hold the contributions from my authors. One reason that using NewsPublisher appealed to me was that Bob has already included Articles integration, and it works perfectly. This is just one little feature I thought might be simple enough to tack in, then I can hit the ground running.

        Knowing that I'm using Articles, would that change your suggested approach at all?

        Thanks again!
          • 3749
          • 24,544 Posts
          I can think of several things to try. First, try just leaving NewsPublisher alone and putting a snippet with this code above the NP snippet tag:

          <?php
          $ph = '';  /* your computed pagetitle here */
          $modx->setPlaceholder('np.pagetitle', $ph);
          


          If that doesn't work, I think what you're doing could work, but I think there's an easier way (see below). To do it your way, the part you're inserting for the pagetitle should look like a modified version of this (I would make it readonly rather than hidden -- at least during development):

          <div id="np-pagetitle-container" class="np-text">
                  [[+np.error_pagetitle]]]]
                  <label class="fieldlabel" for="np-pagetitle" title="[[+npx.help]]">[[+npx.caption]]: </label>
                  <input name="pagetitle" class="text" id="np-pagetitle" readonly="readonly" type="text"  value="[[+np.pagetitle]]" maxlength="[[+npx.maxlength]]" />
              </div>


          I'm guessing here, so some of the above may not be quite right, but then I would put a snippet above the NewsPublisher snippet to set the np.pagetitle placeholder.

          <?php
          $ph = '';  /* your computed pagetitle here */
          $modx->setPlaceholder('np.pagetitle', $ph);
          


          Another, possibly easier and more reliable method that wouldn't require you to customize NewsPublisher at all would be to create a plugin attached to OnDocFormSave (or possibly OnBeforeDocFormSave).

          <?php
          
          /* only execute on new docs */
          if ($mode != modSystemEvent::MODE_NEW) {
              return '';
          }
          
          /* need a test here to make sure it's a NewsPublisher doc, probably based on the parent */
          
          if ($resource->get('parent') != 12) {
             return '';
          }
          
          /* make sure pagetitle was empty */
          if ($resource->get('pagetitle') != $modx->lexicon('resource_untitled') {
               return '';;
          }
          
          $pt = ''; /* your computed pagetitle here using $resource->getTVValue('ProductLine') etc. */
          
          $resource->set('pagetitle', $pt);
          $resource->save();


          You would have to make pagetitle not required by setting the &required property and leaving out the pagetitle. I haven't tested any of this, but I think either method could work -- I would try the little snippet at the top first, then the plugin. Let me know how it works out.

          Someday, NewsPublisher will have postHooks, but probably not any time soon. wink
          [ed. note: BobRay last edited this post 11 years, 1 month ago.]
            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
            • 42822
            • 21 Posts
            Quote from: BobRay at Feb 18, 2013, 04:48 AM

            Another, possibly easier and more reliable method that wouldn't require you to customize NewsPublisher at all would be to create a plugin attached to OnDocFormSave (or possibly OnBeforeDocFormSave).

            <!--?php
            
            /* only execute on new docs */
            if ($mode != modSystemEvent::MODE_NEW) {
                return '';
            }
            
            /* need a test here to make sure it's a NewsPublisher doc, probably based on the parent */
            
            if ($resource--->get('parent') != 12) {
               return '';
            }
            
            /* make sure pagetitle was empty */
            if ($resource->get('pagetitle') != $modx->lexicon('resource_untitled') {
                 return '';;
            }
            
            $pt = ''; /* your computed pagetitle here using $resource->getTVValue('ProductLine') etc. */
            
            $resource->set('pagetitle', $pt);
            $resource->save();


            You would have to make pagetitle not required by setting the &required property and leaving out the pagetitle. I haven't tested any of this, but I think either method could work -- I would try the little snippet at the top first, then the plugin. Let me know how it works out.

            thanks for sharing this trick. Will this also work with tv?


            Someday, NewsPublisher will have postHooks, but probably not any time soon. wink

            meening smiley
              • 3749
              • 24,544 Posts
              If you mean could you set a TV value for the resource there, yes.
                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