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

    I’ve created my own "UltimateParent" snippet, because it didn’t really do what I wanted. Now my question is, can I re-assign placeholders? I’ve tried unsetting them but that doesn’t seem to work. The output stays the same and even with a suffix it still doesn’t work.

    <?php
    	if (!isset($types)) {
    		return 'Could not count $types on account of being empty or not set';
    	}
    	
    	$parents = array();
    	$types = explode(',', $types);
    	
    	if (!isset($id)) {
    		$id = (int) $modx->resource->get('id');
    	}
    	
    	$lvl = 0;
    		
    	while($lvl < count($types)) {
    		$pId = $modx->getParent($id, 1, 'id');
    		$id = $pId['id'];
    		
    		$parents[] = $modx->getObject('modResource', $id)->toArray();
    		
    		$lvl++;
    	}
    	
    	$modx->unsetPlaceholders($types);
    	
    	foreach(array_reverse($types) as $key => $value) {
    		$parents[$key]['link'] = $modx->makeUrl($parents[$key]['id']);
    		
    		if (isset($suffix)) {
    			$modx->setPlaceholders($parents[$key], $value.'.'.$suffix.'.');
    		} else {
    			$modx->setPlaceholders($parents[$key], $value.'.');
    		}
    	}
    	
    	return;
    	
    ?>


    The idea is that it explodes on $types, which are for example ’section,category’. By that, the level upwards would be 2. Reversing that order and starting with the first parent of the resource, this would be the category, secondly the section and so forth. This assigns ’section’ as [[+section.pagetitle]] and [[+category.pagetitle]]. Now the first assign works perfectly, but using them in a chunk which is looped by getResources, it doesn’t reassign the placeholder. I thought getResources parsed a chunk, added it to it’s output variable and then continues on, using the same placeholder twice or more (because each item in the chunk is called with [[+pagetitle]] and so on).

    I attempted to do the same, but my knowledge of the MODx API is still limited and I can’t seem to find a complete documentation.

    <?php
    
    	if (isset($type) && is_string($type)) {
    		$chunk = $modx->newObject('modChunk');
    		$chunk->setCacheable(false);
    		$output = $chunk->process(array('pagetitle' => 'test'), $tpl);
    		
    		return $output;
    	}
    
    ?>


    This is of course testcode. The input is as follows: [[!fetchParent? &type=`section` &tpl=`<span>[[+pagetitle]]</span>`]]

    Am I missing something or am I completely reading the chunk object wrong?
      @MarkGHErnst

      Developer at Adwise Internetmarketing, the Netherlands.
      • 24865
      • 289 Posts
      Really? No one?
        @MarkGHErnst

        Developer at Adwise Internetmarketing, the Netherlands.
        • 3749
        • 24,544 Posts
        I’m sorry, I can’t tell what you’re trying to do, but I can tell you that in your second code example, the chunk has no content at all, since it’s new, so processing it won’t get you anything.

        Here are some things that might (or might not) help you:

        You can get all the parent IDs of a resource into an array (assuming that you get the resource first with $modx->getObject() ) like this:

        $resource = $modx->getOne('modResource',12);  // get by ID
        $resource = $modx->getOne('modResource',array('pagetitle'=>'MyResource');  // get by name
        $parents = array();
        $parents = $resource->getParentIds();


        To get an array containing the all the parent objects themselves:

        $parentObject = $resource->getMany('Parents');


        If you have an associative array of placeholder keys and values, you can set (or reset) all the placeholder values like this:

        $placeholders = array('ph1'=>'value1','ph2'=>'value2);
        
        $output = $modx->getChunk('ChunkName',$placeholders);
        return $output;


        You might as well use an existing chunk that contains the placeholder tags rather than creating a new one.
          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
          • 24865
          • 289 Posts
          Thanks for your answer. It got me thinking, which is usually good. I interpeted the structure of the getResource snip and by reading the MODx API last knight (pun intended) I found that I can, by using a string, build my own chunk on-the-go.

          As we know, the getChunk and parseChunk basically find a resource chunk from the database or ftp/file structure. By replacing that method with the inline method I was trying to use, I came up with the following:

          <?php
          
          	/**
          	 * @author ReSpawN
          	 * @copyright 2010
          	 */
          	
          	$chunk = $modx->newObject('modChunk', array('snippet' => '[[+test]]'));
          	return $chunk->process(array('test' => 'This a inline test chunk'));
          	
          	// For MODxCMS forum; it can also be used like this
          	
          	$chunk = $modx->newObject('modChunk');
          	return $chunk->process(array('test' => 'This a inline test chunk'), '[[+test]]');
          
          ?>


          Both methods are valid, but if you like to parse the chunk before outputting, like, let’s say, applying a plugin, your own replacement code or something like that, you need to use the first method, as the chunk is then already compiled.

          I found my answer and I also hope this helps someone else.

          Then again, on a whole diffirent but actually similar subject I’m still stuck with the set and unset placeholder method. I’ve created a snip this morning that simply assings a mt_rand value from php, from 0 to 1000. I assign it to [[+test]], or ’test’ if you will, and then echo out [[+test]] in the resource underneath the snippet and repeat the same process 3 times. Every time, I unset the ’test’ placeholder before I set a new one (setting a new one should actually replace the old one, but ok) but it still echo’s the same (eg: 518). I’m not yet sure how these two methods work. Echo’ing $modx->_placeholders; definitly shows the variable but the value is the same.
            @MarkGHErnst

            Developer at Adwise Internetmarketing, the Netherlands.
            • 3749
            • 24,544 Posts
            Try it uncached:

             [[!+test]]
              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