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

    I have discovered an inexplicable error with Modx 2.4.3 (PHP 5.54 stable) which I can not explain yet. The website is stored on a german server. Those who live in Germany are able to view any resource / page at any time. However, users in Austria can hardly view anything. Instead, the following fatal error is always displayed:

    Fatal error: Call to a member function get() on a non-object in ../core/model/modx/modtemplatevar.class.php on line 296


    Line 296 in modtemplatevar.class.php
    $context = !empty($resourceId) ? $this->xpdo->getObject('modResource', $resourceId)->get('context_key') : $this->xpdo->context->get('key');


    What I've tried so far:

    1. Reinstall Modx
    2. Reload database
    3. Create a resource without any snippets or chunks in it.
    4. Change site_status in system settings

    Thanks for your help in advance!

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      It's hard to pinpoint because there are two get() calls in the code.

      If the Austrian users are sent to a different Context, the most likely cause of the problem is that that Context has not been initialized properly when they visit the page with the TV. In that case $this->xpdo->context->get('key') is throwing the error because the context is empty (i.e. $this->xpdo->context = null).

      Often, this has to do with missing Context Settings for that Context.

      If it's the other get() that's throwing the error, it's because there's no Resource with the Resource ID it's using.

      If the Austrian users can visit any pages in that context, the second issue would be more likely, imo.

      I think I would rewrite the code of the class file and replace line 296 with this:

      if (!empty($resourceId)) {
          $this->xpdo->getObject('modResource', $resourceId);
          $context = $resource->get('context_key');
      } else {
          $context = $this->xpdo->context->get('key');
      }


      That way the error line number will tell you which problem you have.
        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
        • 36591
        • 38 Posts
        Thanks for the quick reply! I replaced line 296 with the code you suggested. The error I'm getting now is:
        Fatal error: Call to a member function get() on a non-object in ../core/model/modx/modtemplatevar.class.php on line 298


        Line 298 in the modified modtemplatevar.class.php
        $context = $resource->get('context_key');


        I still wonder how this error could occur. This isn't my first modx installation and all of a sudden I get this issue. Anyway I guess I can handle this now. [ed. note: waivor last edited this post 8 years, 7 months ago.]
          • 3749
          • 24,544 Posts
          There is a mistake in my code, I've fixed it here and I've added a sanity check that might help, logging the error, and a fallback to the current context:

          if (!empty($resourceId)) {
              $resource = $this->xpdo->getObject('modResource', $resourceId);
              if (!$resource) {
                  $modx->log(modX::LOG_LEVEL_ERROR, '[modTemplateVar] Resource not found: ' . $resourceId);
                 $context = $this->xpdo->context->get('key');
              } else {
                  $context = $resource->get('context_key');
              }
          } else {
              $context = $this->xpdo->context->get('key');
          }


          [Typos fixed, 3/7/27] [ed. note: BobRay last edited this post 7 years, 7 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
            • 53297
            • 4 Posts
            Hello!
            Could you post your solution if you found any? I am having exact same problem at the moment.

            I also tried Bob's solution but I am getting this:

            http://prntscr.com/eh6vb0 [ed. note: BobRay last edited this post 7 years, 7 months ago.]
              • 3749
              • 24,544 Posts
              There were still a couple of typos in my script. Fixed above (hopefully).
                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
                • 53297
                • 4 Posts
                Thanks for the quick reply but after fixing that typo now I'm getting this:

                [08-Mar-2017 10:49:12 UTC] PHP Fatal error: Call to a member function log() on null in /home/area/public_html/core/model/modx/modtemplatevar.class.php on line 299

                line 299:

                $modx->log(modX::LOG_LEVEL_ERROR, '[modTemplateVar] Resource not found: ' . $resourceId);


                I will try fresh install since the site is still fresh and in development.
                  • 3749
                  • 24,544 Posts
                  That's very strange. It means that MODX doesn't exist at that point.

                  That implies that there is a script that runs outside of MODX that accesses one or more template variables.

                  What are you using to direct users to the correct context?
                    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