We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41305
    • 4 Posts
    Hi - To speed up our site (on first load after cache refresh) I'm writing the output of wayfinder into a chunk - the idea is when you clear the cache it writes the chunk.

    I wrote an isolated PHP file to do this and it works perfectly - but when I throw the code into a plugin - wayfinder doesn't seem to output anything - i.e. returns an empty string. The custom plugin itself seems to run fine.

    The original PHP file had this at the top:

    <?php
    require_once '/home/lnx6equi/www/config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();


    I assume this isn't needed in the plugin as it's already initialized - below follows what I've put into the plugin window:

    <?php
    
    // perhaps this stuff isn't needed either?
    $modx->initialize('web');
    $modx->getService('error','error.modError');
    $siteStartId = $modx->getOption('site_start');
    $modx->resource = $modx->getObject('modResource',$modx->getOption('site_start'));
    $modx->user = $modx->getObject('modUser',1);
    
    $myChunk = 'fastermenu'; // define chunk to write to
    
    $params['startId'] = '0';
    $params['level'] = '2';
    $params['innerTpl'] = 'MAINNAV_NavInner';
    $params['rowTpl'] = 'MAINNAV_NavRow';
    $params['innerRowTpl'] = 'MAINNAV_NavInnerRow';
    $params['startItemTpl'] = 'MAINNAV_NavRow';
    
    $html = $modx->runSnippet('Wayfinder', $params); //$HTML seems empty?
    
    $chunk = $modx->getObject('modChunk', array('name' => $myChunk));
    if (!empty($chunk)) {
        $chunk->setContent($html);
        $chunk->save();
    }


    If I pass "debug=1" into the params - wayfinder returns "No resources found for menu". It's as though it's ignoring the startid param.

    Any tips would be helpful!

    thanks,

    Tim.








    [ed. note: timmyh last edited this post 14 years ago.]
      • 3749
      • 24,544 Posts
      If your chunk is empty to begin with, it's never going to get written to.

      If you're running inside of MODX, you definitely don't want to initialize $modx.

      Wayfinder uses $this->modx->resource to get the current resource in four places, and that's not set in the environment of some plugins. You're setting it yourself, but the value won't get through to Wayfinder if it's set on a different instance of $modx. It might or might not get through if you take out the initialization.

      You might also try setting an 'id' field in your array to the ID of the site_start page. Wayfinder will use that for the 'here' location.


      ------------------------------------------------------------------------------------------
      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
        • 41305
        • 4 Posts
        Thanks for this - try as we might we just couldn't get wayfinder to work this way so we just called the external PHP file.
        <?php
        $base = $modx->getOption('wayfinder.core_path',$scriptProperties,$modx->getOption('core_path').'../www/apicalls/quickmenu.php');
        $modx->log(MODX_LOG_LEVEL_ERROR, print_r(include $base, true ) );
        
        return true;


        This appears to work fine, writing to the chunk when the cache is cleared - with one wierd issue - if I now update the menu, wayfinder still returns the same output, i.e. it isn't reflecting any menu changes. Stranger still if I call the quickmenu.php script on it's own, it works fine and shows any modifications!

        Can you think why this might be happening? Code of quickmenu.php below:

        if(!isset($modx))
        {
            require_once '/var/www/www.gradwell.com/src/www/config.core.php';
            require_once MODX_CORE_PATH.'model/modx/modx.class.php';
            $modx = new modX();
            $modx->initialize('web');
            $modx->getService('error','error.modError');
        }
        
        $siteStartId = $modx->getOption('site_start');
        $modx->resource = $modx->getObject('modResource',$modx->getOption('site_start'));
        $myChunk = 'fastermenu'; // define chunk to write to
        
        $params['startId'] = '0';
        $params['level'] = '2';
        $params['outerTpl'] = 'MAINNAV_NavOuter';
        $params['innerTpl'] = 'MAINNAV_NavInner';
        $params['rowTpl'] = 'MAINNAV_NavRow';
        $params['innerRowTpl'] = 'MAINNAV_NavInnerRow';
        $params['startItemTpl'] = 'MAINNAV_NavRow';
        $params['depth'] = '1';
        
        $html = $modx->runSnippet('Wayfinder', $params); //$HTML seems empty?
        
        $chunk = $modx->getObject('modChunk', array('name' => $myChunk));
        if (!empty($chunk)) {
            $chunk->setContent($html);
            $chunk->save();
        }
        


        [ed. note: timmyh last edited this post 14 years ago.]
          • 41305
          • 4 Posts
          Ok fixed it - needed to clear the cache in the code before reading wayfinder