We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    It should work according to the docs (though I haven't tested it), but try replacing line 15 with this and taking out line 16:

    $id = $rgrs[0] ->get('document');


    It will be faster anyway, since you don't really need the resource, just its ID.


    ------------------------------------------------------------------------------------------
    PLEASE, PLEASE specify the version of MODX you are using.
    MODX info for everyone: http://bobsguides.com/modx.html
      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
      • 9439
      • 90 Posts
      So here's my simplified code, as it still doesn't work:

      <?php
      $name = 'test';
      $resourceGroup = $modx->getObject('modResourceGroup', array('name' => $name));
      $rgrs = $resourceGroup->getMany('ResourceGroupResources');
      	if ( ! empty ($rgrs)) {
      		$id = $rgrs[0] ->get('document');
      		echo "id = $id";
      	}else{
      		echo "empty";
      		
      ?>


      Running this gives a blank screen. Commenting out line 4 it runs (to "empty" obviously) but I'm not sure if it's line 4 that's the problem, or line 3, as $resourceGroup array appears empty. Is this code correct? It seems a very basic function but I cannot get it to work.
        David Heriot
        pfsmedia
        • 9439
        • 90 Posts
        Ok, got it to work. Here's the code:
        <?php
        $name = 'test';
        $id = '11';
        $resourceGroup = $modx->getObject('modResourceGroup', array('name' => $name));
        $groups = $resourceGroup->getMany('ResourceGroupResources');
        	foreach ($groups as $group) {
        		$redir $group->get('document');
        	}
        ?>


        Just a little change and I can't explain why this works and the get('document above doesn't. And I can see it's rather inelegant, eg what happens if there's 2 resources in the resource group? But worry about that later. Got to make it work now as a Login postHook.

        Bob, thanks for your help once more.
          David Heriot
          pfsmedia
          • 9439
          • 90 Posts
          Just to finish this off (and I realise it's probably not the correct forum). In the final part of this redirect exercise I encountered the problem that others before me have experienced, namely when the postHook fires the logged in user won't yet be reflected in $modx->user->get('id'), so basing a resource redirect on this call doesn't work (actually redirects on logout).

          Instead, by using the loginResourceId to redirect to an interim 'redirect resource' where the identify & redirect snippet lives, it works correctly. Hoorah!

          However, that uses unneccessary cycles and is really a bit clumsy. There's a more elegant and tighter solution out there somewhere (I tried using $hook->getValue('id'); but it failed), and if anyone knows it please let me know.

          Once I get the code cleaned up I'll post the whole problem/solution for reference.
            David Heriot
            pfsmedia