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

    I would like to set the parent of current document as Wayfinder root (i.e. startId).

    Excepted in the case where the parent’s id equals to 1.
    In such case, the current document should be kept as root for Wayfinder.

    In Wayfinder configuration file, I put this code.
    $startId="@CODE:
    	$startId = $modx->getParent(-1,0,'id');
    	$startId = $startId['id'];
    	if ($startId == 1) {
    		$startId = $modx->getPageInfo(-1, 1, 'id');
    		$startId = $startId['id'];
            } 
    	return $startId;
    ";


    Unfortunately it fails and I get this error when trying to display the document:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
    expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\xampplite\htdocs\some_site\assets\snippets\wayfinder\configs\menu_rubriques.config.php on line 5

    Line 5 is the third line in my code, where I’m trying to retrieve the id of the parent from the array : $startId = $startId[’id’];

    Who can help ?

    Thanks a lot.
      • 15001
      • 697 Posts
      Sounds like a problem was due to the double-quotes. I tried using single quotes instead and to escape inner single quotes and to write all on one line.
      $startId='@CODE:$startId=$modx->getParent(-1,1,\'id\');$startId = $startId[\'id\'];if ($startId==1) {$startId = $modx->getPageInfo(-1,1,\'id\');$startId = $startId[\'id\'];} return $startId;';

      Another parse error displays :
      « Execution of a query to the database failed - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’:$startId=$modx->getParent(-1,1,’id’);$startId = $startId[’id’];if ($startId==1)’ at line 1 »
      Any idea what’s wrong?
        • 15001
        • 697 Posts
        Well, this works for most documents, but the exception that corresponds to the special condition where the parent id equals 1 is not properly handled.

        In Wayfinder configuration file :
        $startId=$modx->runSnippet(GetParentDocumentId);


        The GetParentDocumentId snippet :
        <?php
        $startId = $modx->getParent(-1,0,'id');
        $startId = $startId['id'];
        if ($startId == 1) {
         $startId = $modx->getPageInfo(-1, 1, 'id');
         $startId = $startId['id'];
        } 
        return $startId;
        ?>


        Not time enough to debug it right now. If someone has idea what’s wrong...
          • 4172
          • 5,888 Posts
          You can try this, (not tested):

          <?php
          return $modx->documentObject['parent']=='1'?$modx->documentObject['id']:$modx->documentObject['parent'];
          ?>
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 15001
            • 697 Posts
            Hi Bruno,

            Your code works perfectly. Thanks.

            To avoid accessing twice modx->documentObject, I tried some rewriting of your code using a $startId variable. But following code fails with a syntax error. I’m frustrated to not understand why the syntax error.
            <?php
            var $startId = $modx->documentObject['parent'];
            if ($startId == '1')
              $startId = $modx->documentObject['id'];
            return $startId;
            ?>
              • 4172
              • 5,888 Posts
              three of x posibilities:

              <?php
              $parentId = $modx->documentObject['parent'];
              if ($parentId == '1'){
                  return $modx->documentObject['id'];
              }
              return $parentId;
              ?>


              <?php
              $parentId = $modx->documentObject['parent'];
              if ($parentId == '1'){
                  return $modx->documentObject['id'];
              }else {
              return $parentId;
              }
              
              ?>


              <?php
              $parentId = $modx->documentObject['parent'];
              return $parentId=='1'?$modx->documentObject['id']:$parentId;
              ?>
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!