We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18654
    • 191 Posts
    I have a site where I want to share the same main menu accross multiple subdomains, but each subdomain will have a unique start page. The subdomains represent different chapters of an international Christian apologetics ministry.

    When I use the One Gateway plugin to switch contexts and changes site start it works fine, but the main menu no longer works. For example, index.php?id=2 which is in a different context (’web’) is not accessible from the context (’UNCC’). I can think of one way to work around this which is simply to prefix all my main-level menu items with the base url of the main site.

    So, instead of:
    $outputArray['page_link'] = '[[~' . $mainMenuObject->get('id') . ']]';


    I would code in the snippet
    $outputArray['page_link'] = 'http://www.ratiochristi.org/' . '[[~' . $mainMenuObject->get('id') . ']]';


    Is this the best approach to what I’m trying to accomplish?

    Here is the main domain: Ratio Christi
    Here is the subdomain: Ratio Christi - NC State

    UPDATE: I tried prefixing all the links with the main domain but the link tags from the web context aren’t getting parsed in the non-web contexts, so:
    $outputArray['page_link'] = 'http://www.ratiochristi.org/' . '[[~' . $mainMenuObject->get('id') . ']]';

    shows up as http://www.ratiochristi.org/ instead of http://www.ratiochristi.org/index.php?id=2 when parsed in the context "NCState"

    OK, this works - but I’m not sure if it’s the best approach. Also, at some point I’ll be wanting to switch to friendly URL’s and I’m not sure if this way will mess things up for that:
    $outputArray['page_link'] = 'http://www.ratiochristi.org/index.php?id=' . $mainMenuObject->get('id') ;


    Here’s the entire snippet:
    <?php 
    
    $currentPage = $modx->resource->get('id');
    $currentPageParents = $modx->getParentIds($currentPage);
    $query = $modx->newQuery('modResource');
    $query->where(array(
    		'parent' => 0
    		,'published' => 1
    		,'hidemenu' => 0
    		,'context_key' => 'web'
    	));
    $query->sortby('menuindex, id', 'ASC');
    $mainMenuObjects = $modx->getCollection('modResource',$query);  
    
    $arrayKeys = array_keys($mainMenuObjects);
    
    foreach($mainMenuObjects as $key => $mainMenuObject){
    	
    	
    	$outputArray['link_class'] = $currentPage == $mainMenuObject->get('id') ? ' active' : '';
    	//$outputArray['page_link'] = '[[~' . $mainMenuObject->get('id') . ']]';
    	//$outputArray['page_link'] = 'http://www.ratiochristi.org/' . '[[~' . $mainMenuObject->get('id') . ']]';
    	$outputArray['page_link'] = 'http://www.ratiochristi.org/index.php?id=' . $mainMenuObject->get('id') ;
    	if($outputArray['link_class'] == '') {
    		$outputArray['link_class'] = in_array($mainMenuObject->get('id'), $currentPageParents) ? ' active' : '';
    	}
    	$outputArray['menu_title'] = $mainMenuObject->get('menutitle');
    	$outputArray['menu_id'] = $mainMenuObject->get('id');
    	
    	if ($key == $arrayKeys[0]) {
    		$outputArray['link_class'] .= ' first';
    		echo $modx->parseChunk('NavFirst', $outputArray);
    	}
    			   
    	else if ($key != end($arrayKeys)) {
    		echo $modx->parseChunk('NavRepeat', $outputArray);
    	}
    	
    	else {
    		$outputArray['link_class'] .= ' last';
    		echo $modx->parseChunk('NavLast', $outputArray);	
    	}	
    }
    
    ?>
    


      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
      • 22303 MODX Staff
      • 10,725 Posts
      None of this should be necessary; the links should be generated automatically with the complete site_url defined for the context, if it is not a Resource from the current context (which would still be relative to the current context’s site_url).
        • 18654
        • 191 Posts
        I’m not sure I understand. I have a snippet (shown above) that dynamically generates links for all top-level resources in the web context. I want this same menu to show up in all contexts.

        If the NCState site_url setting is ncstate.ratiochristi.org and the web context site_url setting is ratiochristi.org it seems like when I’m in the NCState context, it will prefix the main menu links with the site_url for the NCState context (ncstate.ratiochristi.org) instead of the web context. I need for the main menu to generate links to the web context and the submenu to generate links for the NCState context.

        I supposed I could create a unique key for each context like "ncstate_site_url" and "web_site_url" - would that be the way to go? Is it possible to get site settings from another context?

        Also, I think I would still have the issue of ModX not parsing the links [[~2]] from other contexts, so something like
        [[++web_site_url]][[~3]]
        wouldn’t work in the NCState context - correct?

        Am I missing something really obvious here?

        Ok... I think maybe I get it. I set the site_url for all subdomains to the main domain (ratiochristi.org). When I’m generating links to the subdomains I DO NOT prefix the links with site_url. When I’m generating links for the main site, I DO prefix with site_url which will be the same for all subdomains. Is this what you meant?

        Only other question that remains is do I need stop using [[~whatever]] for generating links that will be used across multiple contexts since ModX doesn’t seem to parse link tags from other contexts?

          God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
          • 22303 MODX Staff
          • 10,725 Posts
          MODx automatically generates links for the proper context. Components should just use makeUrl and the Resources from other contexts will automatically use the site_url from the owning context. Did you not set the context specific values for your urls?

          You should never have to prefix URLs in MODx with anything; they are always relative to the site_url.
            • 18654
            • 191 Posts
            When setting up the new context, I followed the instructions in the manual for the settings.

            Here’s a snapshot of the web context settings.

            Here’s a snapshot of the web context settings.

            Even when I use makeurl in my navagation snippet, the links do not point to the web context.

            Here is the plugin code:
            <?php
            
            $url = parse_url($_SERVER['HTTP_HOST']);
            switch ($url['path']) {  
            	case 'ncstate.ratiochristi.org':
            		$modx->switchContext('NCState');
            		break;  
            	default:  
            		break;  
            }  
            ?>
            


            Here is the navigation snippet:
            <?php 
            
            $currentPage = $modx->resource->get('id');
            $currentPageParents = $modx->getParentIds($currentPage);
            $query = $modx->newQuery('modResource');
            $query->where(array(
            		'parent' => 0
            		,'published' => 1
            		,'hidemenu' => 0
            		,'context_key' => 'web'
            	));
            $query->sortby('menuindex, id', 'ASC');
            $mainMenuObjects = $modx->getCollection('modResource',$query);  
            
            $arrayKeys = array_keys($mainMenuObjects);
            
            foreach($mainMenuObjects as $key => $mainMenuObject){
            	
            	$outputArray['link_class'] = $currentPage == $mainMenuObject->get('id') ? ' active' : '';
            	//$outputArray['page_link'] = '[[~' . $mainMenuObject->get('id') . ']]';
            	//$outputArray['page_link'] = 'http://www.ratiochristi.org/' . '[[~' . $mainMenuObject->get('id') . ']]';
            	//$outputArray['page_link'] = 'http://www.ratiochristi.org/index.php?id=' . $mainMenuObject->get('id') ;
            	//$outputArray['page_link'] = 'http://www.ratiochristi.org/index.php?id=' . $mainMenuObject->get('id') ;
            	$outputArray['page_link'] = $modx->makeURL($mainMenuObject->get('id'), 'web');
            	if($outputArray['link_class'] == '') {
            		$outputArray['link_class'] = in_array($mainMenuObject->get('id'), $currentPageParents) ? ' active' : '';
            	}
            	$outputArray['menu_title'] = $mainMenuObject->get('menutitle');
            	$outputArray['menu_id'] = $mainMenuObject->get('id');
            	
            	if ($key == $arrayKeys[0]) {
            		$outputArray['link_class'] .= ' first';
            		echo $modx->parseChunk('NavFirst', $outputArray);
            	}
            			   
            	else if ($key != end($arrayKeys)) {
            		echo $modx->parseChunk('NavRepeat', $outputArray);
            	}
            	
            	else {
            		$outputArray['link_class'] .= ' last';
            		echo $modx->parseChunk('NavLast', $outputArray);	
            	}	
            }
            
            ?>
            
              God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
              • 18654
              • 191 Posts
              I’ve done some testing and I can see that makeUrl works perfectly fine for generating urls from other contexts when the function is run from the web context. However, when I run it from a non-web context and input a link from the web context it doesn’t work.

              So I’m guessing there’s some setting that I need to add to my non-web contexts, but I’m not sure what. As shown in the above pics, I already have site_start, base_url, http_host, and site_url defined in both the web context and the NCState context. Any idea what other setting I may need to add to the NCState context so that makeUrl will work succesfully?

              I’m starting to think this may be a bug with makeUrl because I’ve tried just about every setting I can think of so that makeUrl will generate a full http link from a non-web context to a web context and nothing is working. Has anybody had any success with this?

              Even something simple like this won’t work from a non-web context (returns Null):
              $webContext = $modx->getObject('modContext','web');


              UPDATE: Ok, I finally got it working although I still don’t understand entirely why. It was an ACL issue (related to my other post about getting a null object when requesting a context object from a non-web context) - I made the access control settings in my non-web context the exact same as the web context and now it works fine. Now maybe I can go to sleep smiley


                God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.