We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53432
    • 46 Posts
    How can I get the name of the current context?

    These both return the context key, but what I really need is the name:
    $modx->context->key
    $modx->resource->context_key


    I have multiple contexts and I’d like to pull their names dynamically into the menu system. For what it’s worth, this does not work:
    $modx->context->name


    And the following code only works for contexts other than the current one (…I’d love to understand why):

    $ctx = $modx->getContext( $key );
    return $ctx->get('name');


    Any ideas?
      • 3749
      • 24,544 Posts
      This would be the usual method for the current context:

      $modx->context->get('name');


      But if you skipped over some major (x.x.0) version upgrades, some contexts may not have a name.

        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
        • 53432
        • 46 Posts
        Thanks, that’s helpful but I don’t think it applies. All contexts have names in the DB table. And I did not skip any x.x.0 upgrades. The base install was 2.6.5, then I upgraded to 2.7.0 recently. There was nothing in between as far as I know.

        Here’s my code. It actually works fine except that the current context always has no name. I have 5 contexts including the default.

        When a visitor is within the “europe” context, their “Europe Home” button is blank. When they switch to any other context, “Europe Home” will work, but then the current region’s home button will become blank. I’m mystified! Any ideas?

        <?php
        $output = array();
        $keys = array('europe','asia','middleeast','americas');
        foreach ($keys as $key){
        	$ctx = $modx->getContext( $key );
        	if ($ctx) {
        		$output[$ctx->get('key')] = array(
        			'key'        => $ctx->get('key'),
        			'name'       => $ctx->get('name'),
        			'http_host'  => $ctx->getOption('http_host', null, ''),
        			'site_url'   => $ctx->getOption('site_url', null, ''),
        			'base_url'   => $ctx->getOption('base_url', null, ''),
        			'site_start' => $ctx->getOption('site_start', null, ''),
        		);
        	}
        }
        return $output;

          • 3749
          • 24,544 Posts
          Have you tried $modx->context->get('name') for the current context?

          You might try this, which will be a little more efficient and will get the information directly from the DB rather than relying on the cache:

          $output = array();
          $keys = array('europe','asia','middleeast','americas');
          foreach ($keys as $key){
              $ctx = $modx->getObject('modContext', array('key' => $key), false);
              if ($ctx)  {
                  $output[$key] = $ctx->toArray()
              }
          }
          
          return $output;


          [Update]

          The code above worked for me to get the name of the current context, even without the "false" argument in the getObject() call (which bypasses the cache). [ed. note: BobRay last edited this post 5 years, 8 months ago.]
            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