We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I’m trying to use this snippet as output to a TV, to list all the resources that use a given template - *except* for the ID of the current resource.
    <?php
    $template = (isset($template)) ? $template : '1';
    
    $c = $modx->newQuery('modResource');
    $c->leftJoin('modTemplate','Template');
    $c->where(array(
        'published' => true,
    ));
    if (is_numeric($template)) 
       { $c->where(array('template' => $template));} 
    else 
       { $c->where(array('Template.templatename' => $template)); mail('[email protected]', 'non-numeric'); }
    
    $resources = $modx->getCollection('modResource',$c);
    
    foreach($resources as $res) {
        $id = $res->get('id');
        if ($modx->resource->get('id') != $id) {
          $opts[] =  $res->get('pagetitle') . '==' . $res->get('id');
        }
    }
    return  implode('||', $opts); 
    ?>
    


    Everything is working except for the "is this the current resource" part. I’m getting the error
    "PHP Fatal error: Call to a member function get() on a non-object"

    Is $modx->resource not available from within a TV? If not, how do I get the ID of the current resource?
    • Additionally & interestingly, when I use this snippet as written as the input to a TV (via @EVAL), none of the template variables load when a page is requested that uses a template with this TV assigned. When you click on the Template Variables tab, the ajax/loading spinny icon appears and nothing happens.
        • 28215
        • 4,149 Posts
        Quote from: jrotering at May 20, 2010, 07:22 PM

        I’m trying to use this snippet as output to a TV, to list all the resources that use a given template - *except* for the ID of the current resource.
        Everything is working except for the "is this the current resource" part. I’m getting the error
        "PHP Fatal error: Call to a member function get() on a non-object"

        Is $modx->resource not available from within a TV? If not, how do I get the ID of the current resource?

        I’m assuming this is for a custom TV for the backend? If so, try $_REQUEST[’id’]. If it’s not, can you explain how you’re using this? Is it a snippet?

        Also, why the mail() statement? You can use $modx->log to do debugging:

        $modx->log(modX::LOG_LEVEL_ERROR,'My message here.');
        


        Which will log errors to the core/cache/error.log file. You could also change the log target to ECHO:

        $modx->setLogTarget('ECHO');
        $modx->log(modX::LOG_LEVEL_ERROR,'Output me to the screen.');
        


        Hopefully that’ll help. smiley
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • This isn’t for a custom TV - I haven’t ventured in to those waters yet. It is a snippet for populating a regular TV. Basically, I am populating a dropdown list box (the TV input) with the IDs and pagetitles of all the other resources that use this same template.
          @EVAL return 'inherit||none' . $modx->runSnippet('getResourcesByTemplate', array('template'=>'21'));



          The mail statement was something I threw in quick to try and troubleshoot a problem that ended up being completely unrelated to that if-else statement. The answer to the ’why’ question is because I’m basically a hack who often doesn’t know the correct way of accomplishing what I want. I usually try to edit out the embarrassing/hackish/irrelevant parts of my code before posting it in a forum, but I forgot that was there.

          Is this what you meant when you said you would be mocking me? smiley
            • 28215
            • 4,149 Posts
            Quote from: jrotering at May 20, 2010, 07:51 PM

            This isn’t for a custom TV - I haven’t ventured in to those waters yet. It is a snippet for populating a regular TV. Basically, I am populating a dropdown list box (the TV input) with the IDs and pagetitles of all the other resources that use this same template.
            Ah, @EVAL. *shudder*. Yes, Custom TVs were meant to kill off @EVAL. But if that works - that should be fine.

            Try $resourceId, and see if that works.


            Is this what you meant when you said you would be mocking me? smiley
            Oh, maybe. wink
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • Try $resourceId, and see if that works.

              It doesn’t.

              I also tried passing the resource ID as an argument from the @EVAL statement:
              @EVAL return 'inherit||none' . $modx->runSnippet('getResourcesByTemplate', array('template'=>'21', 'pid'=>$resourceId));


              $resourceId seems to resolve to the name of the TV.

              It seems as if once you are inside of an @EVAL statement populating a TV input, you lose access to the current resource?

              Is there a way to do what I’m trying to do?
                • 28215
                • 4,149 Posts
                Hm, $resourceId should work, unless eval() loses you scope...

                Try $_REQUEST[’resource’]. If that doesn’t work, as a wild shot in the dark, try $_REQUEST[’id’].
                  shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • $_REQUEST[’resource’] works. It must be a scope thing.

                  Thanks for helping me sort this.
                    • 28215
                    • 4,149 Posts
                    No problem. Whatever I can do to get you posting more on your blog. laugh
                      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                      • 36594
                      • 11 Posts
                      Hey, I need some help passing my Resource ID as an argument to my snippet. I stumbled upon this thread and It looks like you know a thing or two. I hope you can help.

                      my @EVAL code is:
                      @EVAL return $modx->runSnippet('get_like_count');


                      my snippet code is:
                      <?php
                      $source_url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');
                      
                      $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
                      $xml = file_get_contents($url);
                      $xml = simplexml_load_string($xml);
                      $shares = $xml->link_stat->share_count;
                      $likes = $xml->link_stat->like_count;
                      $comments = $xml->link_stat->comment_count;
                      $total = $xml->link_stat->total_count;
                      $max = max($shares,$likes,$comments);
                      
                      return strip_tags($likes);
                      ?>


                      If you need some more background here is the thread I have been using http://forums.modx.com/thread/70939/problem-with-snippet-based-in-tv-using-ditto