We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41428
    • 22 Posts
    Hi everyone

    I've just made a small change to my NewsPublisher snippet that could be of use to somebody - being able to pre-set a field value in the [[!NewsPublisher]] call:

    [[!NewsPublisher?
       ...
       &show=`pagetitle,parent,content`
       &required=`pagetitle,parent,content`
       &contentValue=`blah`
       ...
    ]]
    


    in the relevant field tpl:
    <textarea rows="[[+npx.rows]]" cols="[[+npx.cols]]" class="[[+npx.class]] form-control" name="[[+npx.fieldName]]" id="np-[[+npx.fieldName]]">
       [[+np.[[+npx.fieldName]]:default=`[[+np.[[+npx.fieldName]]Value]]`]]
    </textarea>
    


    in the snippet (around line 155):
    /*set placeholder content values*/
    if(isset($scriptProperties['show'])){
        $show = explode(",",$scriptProperties['show']);
        foreach($show as $field){
            if(isset($scriptProperties[''.$field.'Value'])){
                $modx->toPlaceholder(''.$field.'Value', $scriptProperties[''.$field.'Value'], $np_prefix);
            }       
        }
    }
    


    Wasn't sure where else to put it - but figured it might be useful...
      • 3749
      • 24,544 Posts
      It's an interesting idea. When you use the NpEditThisButton to edit an existing resource, does your value overwrite the actual value of the field?
        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
        • 41428
        • 22 Posts
        Quote from: BobRay at Aug 10, 2015, 08:35 PM
        It's an interesting idea. When you use the NpEditThisButton to edit an existing resource, does your value overwrite the actual value of the field?

        Hmm yes it does, not sure of the reason though. I tried using [[+np.var:is=``:then=`...`]] and that did the same.

        EDIT:
        also tried using [[!If]], but that does the same thing
        [[!If?
           &subject=`[[+np.[[+npx.fieldName]]]]`
           &operator=`EQ`
           &operand=``
           &then=`[[+np.[[+npx.fieldName]]Value]]`
           &else=`[[+np.[[+npx.fieldName]]]]`
        ]]
        


        I guess for now I'll just have 2 resources or 2 field templates, one for adding, the other for editing [ed. note: kctechsoln last edited this post 8 years, 7 months ago.]
          • 3749
          • 24,544 Posts
          I thought it might. I'm pretty sure there's a way around that, but I'm not sure I have time to find it.

          As I'm sure you noticed, the NP code is not particularly elegant, to say the least. That's the result of it starting out as a straight conversion of the Evolution snippet, rather than a rewrite from scratch, which is what I should have done.

            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
            • 37460
            • 4 Posts
            This solution works great for pre-setting fields, but I did need the Edit to also work. I found a solution by changing the code in the newspublisher.class.php file on line 1213 from
            $this->modx->toPlaceholder($name, $ph, $this->prefix );

            to this
            if ($this->existing) {
            $this->modx->toPlaceholder($name, $ph, $this->prefix );
            }
            


            I am sure there is a better way to do it, but it worked for me.
              • 3749
              • 24,544 Posts
              I've made some changes, so my line numbers are not the same as yours. The only place I find that code is line 1244, but as far as I can tell, it only sets TV placeholders. Are you setting fields other than TVs?
                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
                • 37460
                • 4 Posts
                Hi Bob,
                The version I modified is 2.1.0-pl and yes you are correct the change I made was only for the TVs. Pre-setting the regular fields already worked for both new and existing documents using kctechsoln method. So for me a fix was not needed for those fields. Thanks.
                  • 3749
                  • 24,544 Posts
                  But doesn't the value set in the properties with kctechsoln's method override the current value for regular resource fields when editing existing resources?
                    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
                    • 37460
                    • 4 Posts
                    Hi Bob,
                    Yes you are correct, I did not notice that because I was not using any of the standard resource fields in my form. Also, I noticed with the way I set it up, the TVs were not showing their default value.
                    So my new solution was to not use the code that kctechsoln provided but just to modify the newspublisher.class.php in a couple of places. First I replaced my change above and replaced it with this (line 1213):
                    if (!$this->existing) {
                    $presetValue = $this->props[$name . '_value'];
                    if ($presetValue) {
                    $this->modx->toPlaceholder($name, $presetValue, $this->prefix );
                    } else {
                    $this->modx->toPlaceholder($name, $ph, $this->prefix );
                    }
                    } else {
                    $this->modx->toPlaceholder($name, $ph, $this->prefix );
                    }
                    


                    I also changed the displaySimple function (line 1527) by adding the same above code in order to make the pre-setting of the standard resource fields work:
                    protected function _displaySimple($name, $tplName, $maxLength = 10) {
                    if (!$this->existing) {
                    $presetValue = $this->props[$name . '_value'];
                    if ($presetValue) {
                    $this->modx->toPlaceholder($name, $presetValue, $this->prefix );
                    } else {
                    $this->modx->toPlaceholder($name, $ph, $this->prefix );
                    }
                    } else {
                    $this->modx->toPlaceholder($name, $ph, $this->prefix );
                    }
                    $PHs = array('[[+npx.maxlength]]' => $maxLength);
                    return $this->strReplaceAssoc($PHs, $this->getTpl($tplName));
                    }
                    


                    Now it only adds the pre-set value when creating the form for a new resource. Also, I made my Newspublisher snippet properties slightly different then kctechsoln, instead of "Value" added after the field name I am using "_value" like so:
                    [[!NewsPublisher?
                       ...
                       &show=`pagetitle,mytv,usernametv`
                       &pagetitle_value=`Pre-set Page Title`
                       &mytv_value=`Pre-set Template Variable Value`
                       &usernametv_value=`[[!+modx.user.id:userinfo=`fullname`]]`
                       ...
                    ]]
                    


                    Hopefully this might help someone else and thank you Bob.
                      • 3749
                      • 24,544 Posts
                      I will try to find time to work this in, though it will probably take a different form in the properties. Maybe something like this:

                      &presets=`pagetitle:SomeTitle,&mytv:Some Other Value,usernametv:[[!+modx.user.id:userinfo=`fullname`]]`


                      One suggestion on your code -- this will throw a PHP E_NOTICE error it the *_value property is not set:

                      $presetValue = $this->props[$name . '_value'];


                      I'd suggest changing it to something like:

                      $presetValue = $this->modx->getOption($name . '_value', $this->props, false, true);


                      That will set it to false if the property is missing or empty, so the rest of your code should work fine.
                        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