We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38878
    • 255 Posts
    I have a custom output modifier to determine if the current resource is a child of some other resource so I can modify the pagetitle value dynamically within my template. Im not getting error output but it's also not changing the pagetitle as I think it should. The snippet code is:

    /**
     * childOf
     * Ouput filter to determine if the current resource is a child of another.
     * Example use: [[*id:childOf=`69`]] would determine if current resource is a child of resource 69
     *
     */
     
    $id = (!empty($input)) ? $input : false;
    if(!empty($id)) { $id = $modx->getOption('id', $scriptProperties, $modx->resource->get('id')); }
    
    $thisResourceId = $modx->resource->get('id'); 
    // get the published children of $id
    $c = $modx->newQuery('modResource');
    $c->where(array(
       'parent' => $id,
       'published' => true
    ));
    $c->sortby('menuindex', 'ASC');
    $children = $modx->getCollection('modResource',$c);
    foreach ($children as $child) {
    	$childId = $modx->getObject('modResource', $child);
    	if(!empty($child) && $childId == $thisResourceId ) {
        		return true;
    	}
    } 
    return false;


    And my output filter looks like this:

    [[*id:childOf=`20`:then=`[[+pagetitle=`Clients`]]`]]


    No errors, just not changing the pagetitle. Any help would be uber. Thanks!

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

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It would be a lot easier to just check the "parent" field of the current resource and output your text.
      [[[[*parent:is=`20`:then=`Client`:else=`*pagetitle`]]]]


      Actually changing the pagetitle on-the-fly is another matter altogether. It's not going to be that simple. One way could be to use a snippet instead of the pagetitle tag. This way you could even use a switch in the PHP code to handle different output for different parents.
      <?php
      $parent  = $modx->resource->get('parent');
      switch ($parent) {
          case 20:
              $output = 'Clients';
          break;
          default:
              $output = $modx->resource->get('pagetitle');
          break;
      }
      return $output;
      


      You could also use FastField (or pdoTools with the custom parser option) and create a TV in the parent resource for its children's title. Again, this would allow for different return values for different parents.
      [[[[#[[*parent]].tv.title:is=``:then=`*pagetitle`]]]]

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




      [ed. note: sottwell last edited this post 12 years, 3 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        I'm not sure what you're trying to do, but this two-line snippet will return the parent's pagetitle (if there is one), which might help you:

        $parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
        return $parentObj? $parentObj->get('pagetitle') : '';


        BTW, I see a couple of problems in your original code:

        $childId = $modx->getObject('modResource', $child);


        should be

        $childId = $child->get('id');


        $child is a resource object. I would expect sending it as an argument to getObject() to throw an error, but maybe PHP just treats is as an array. Also, getObject() returns an object, not an integer.

        For a custom modifier, the part inside the back-ticks is available as $options. It wouldn't be in the $scriptProperties array in the form you're looking for it.

        What you'd want is:

        $id = isset($options)? $options : $modx->resource->get('id');


        Though I question using the current resource's ID as the default here since your getCollection() call will then get its children and I think you want its siblings.

        All that said, I think Susan's first solution is probably the best one:

        [[[[*parent:is=`20`:then=`Client`:else=`*pagetitle`]]]]


        Finally, here's a shorter version of your childOf output modifier:

        [[*id:childOf=`69`]]


        return (isset($options) && ($modx->resource->get('parent') == $options))? true : false;
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          From what I understand, what is wanted is to use the current resource's 'pagetitle' if the parent is not #20, but to use "Clients" if its parent is #20. The parent's pagetitle is not relevant.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 38878
            • 255 Posts

            Susan is correct. Thanks guys. I totally spaced the [[*parent]] placeholder. I'm really just changing a header tag on the fly in the template. I don't need to change the [[+pagetitle]] placeholder. But Im doing some other template manipulation with the http://rtfm.modx.com/extras/revo/if addon in the template which would have made it easier since I have a few conditional statements already. Ulitmately I ended up with:

            [[*id:ne=`1`:then=`<h1 class="page-header">[[*parent:is=`20`:then=`Clients`:else=`*pagetitle`]] > <span class="text-orange"> [[*longtitle]]</span></h1>`]]


            I also read Jason's article http://modx.com/blog/2012/09/14/tags-as-the-result-or-how-conditionals-are-like-mosquitoes/ and it makes alot of sense. I tried to use [[[[*parent:is=`20`:then=`Clients`:else=`*pagetitle`]]]] but it returned empty. I guess I don't really understand it totally yet.

            Anyway, it works now, and I chucked my childOf fail output filter;) Thanks again!

            Quote from: sottwell at Jun 23, 2014, 02:23 AM
            From what I understand, what is wanted is to use the current resource's 'pagetitle' if the parent is not #20, but to use "Clients" if its parent is #20. The parent's pagetitle is not relevant.
              • 3749
              • 24,544 Posts
              I'm glad you got it sorted, though this snippet would give you much faster page loads (conditional modifiers take a while to parse and execute):

              [[*id:ne=`1`:then=`<h1 class="page-header">[[*parent:is=`20`:then=`Clients`:else=`*pagetitle`]] > <span class="text-orange"> [[*longtitle]]</span></h1>`]]

              [[!PageHeader]]
              

              /* PageHeader snippet */
              
              if ($modx->resource->get('id') == 1) return;
              $output = '<h1 class="page-header">';
              $output .= $modx->resource->get('parent') == 20? 'Clients' : $modx->resource->get('pagetitle');
              $output .= '<span class="text-orange">';
              $output .= $modx->resource->get('longtitle') . '</span></h1>';
              return $output;
                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