We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33277
    • 35 Posts
    Hello! Um, first thank all MODx developers, they’re great. Respect, guys.

    What I am doing now is writing a plugin to automatically find what context a resource is from, and switch to it. Also, I’m interested why MODx prefers not to do this on its own by default (just wondering).

    Anyway,
    For some reason, this line:
    $neededContext = $modx->resource->get('context_key');
    

    Returns this:
    Fatal error: Call to a member function get() on a non-object in [...]/core/cache/includes/elements/modplugin/1.include.cache.php on line 20
    


    Just can’t figure out what I’m doing wrong...
    The plugin is attached to onHandleRequest event.

    Thanks for your attention.
      <em>The Earth is not the hell, and people aren't cursed, and joy in not a sin, but a gift of God.</em> (Lesia Ukrainka)
      • 30107
      • 44 Posts
      Quote from: yurkobb at Jun 18, 2011, 05:58 PM
      $neededContext = $modx->resource->get('context_key');
      I get the context key like this:
      $neededContext = $modx->context->get('context_key');
        • 33277
        • 35 Posts
        Quote from: titanium at Jun 19, 2011, 01:38 AM

        I get the context key like this:
        $neededContext = $modx->context->get('context_key');

        Thank you, but, as I understand, it will return currently used context, either ’mgr’ or ’web’ disregarding of the actual resource ’native’ context. Yes, I may search each context if a resource is there, but that’s slow...
          <em>The Earth is not the hell, and people aren't cursed, and joy in not a sin, but a gift of God.</em> (Lesia Ukrainka)
          • 26903
          • 1,336 Posts
          Looks as though your trying to access the resource object before its been determined and set, i.e. your to ’high’ in the plugin hierarchy to use it, try ’OnLoadWebDocument’ instead.
            Use MODx, or the cat gets it!
            • 33277
            • 35 Posts
            Quote from: shamblett at Jun 19, 2011, 03:27 AM

            Looks as though your trying to access the resource object before its been determined and set, i.e. your to ’high’ in the plugin hierarchy to use it, try ’OnLoadWebDocument’ instead.
            Thank you! It now works. Well partially...

            For some reason the get(’context_key’) for a resource in a "tour" context still returns ’web’. The same for other non-’web’ context resources. I checked for the id of $modx->resource and found that if it’s not in ’web’, it’s always 1. What should I do to get $modx->resource be the actual non-’web’ resource? Or more generally, how do I efficiently load a resource from any context by its FURL? Thanks.
              <em>The Earth is not the hell, and people aren't cursed, and joy in not a sin, but a gift of God.</em> (Lesia Ukrainka)
              • 19302
              • 6 Posts
              Quote from: yurkobb at Jun 19, 2011, 04:10 AM

              Or more generally, how do I efficiently load a resource from any context by its FURL? Thanks.

              Ok, just going to guess here -

              $name = 'modResource';
              $criteria = array(
                  'published' => 1,
                  'alias' => 'your-alias-furl'
              );
              $document = $modx->getObject($name,$criteria);
              $key = $document->get('context_key');
              
              return ($key);
              


              Thats one way to find out by furl which context that resource is in. But, it’s pretty hard working out what you’re trying to do - this is the wiki page on contexts - http://rtfm.modx.com/display/revolution20/Contexts & Bobs Quite Excellent Guide - http://bobsguides.com/revolution-objects.html

              One thing - how are you displaying a resource from a different context at the moment - and are you doing any context switching?

                • 33277
                • 35 Posts
                Quote from: michaelm at Jun 19, 2011, 06:27 PM

                Quote from: yurkobb at Jun 19, 2011, 04:10 AM

                Or more generally, how do I efficiently load a resource from any context by its FURL? Thanks.

                Ok, just going to guess here -

                $name = 'modResource';
                $criteria = array(
                    'published' => 1,
                    'alias' => 'your-alias-furl'
                );
                $document = $modx->getObject($name,$criteria);
                $key = $document->get('context_key');
                
                return ($key);
                



                Thats one way to find out by furl which context that resource is in. But, it’s pretty hard working out what you’re trying to do - this is the wiki page on contexts - http://rtfm.modx.com/display/revolution20/Contexts & Bobs Quite Excellent Guide - http://bobsguides.com/revolution-objects.html

                One thing - how are you displaying a resource from a different context at the moment - and are you doing any context switching?
                Thank you for the code and links! I’ll definitly make use of that.

                Yes, I would then do $modx->switchContext($neededContext) to have the resource finally shown...

                But it leads to us doing by hand what modRequest does, and the reason for that is the really strange restrictions modRequest applies to resources from other contexts than current. I still can’t get why they are there.

                Thank you for the help, if I’ll have more questions, I’ll ask them here.

                Oh, I have a question: will the cache work on a resource retrieved such a way?
                  <em>The Earth is not the hell, and people aren't cursed, and joy in not a sin, but a gift of God.</em> (Lesia Ukrainka)