We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42492
    • 31 Posts
    Quote from: BobRay at Mar 18, 2013, 12:19 PM
    Try using getCollection() instead of getIterator(). I've had trouble with getIterator() and nesting, since getIterator() doesn't actually get the data until it's needed and if it's not needed in the snippet with the getIterator() call, it may not show up at all.

    In general, though, I think I would try to redesign this so that you don't have to nest things so deeply. It appears that you have a resource field tag inside a link tag inside a chunk inside a snippet inside a chunk. I try not to ever get more than two, or occasionally three levels deep.

    READ POST BELOW. THIS DID NOT FIX THE PROBLEM.
    Thanks a lot Bob. Not nesting so deeply fixed the issue. In your opinion, what element is better suited to be exposed to the user? Should I put a snippet into the resource ( [[ArchivedNews]] )? Or should I put that behavior into a chunk ( [[$ArchivedNews]] )? [ed. note: csauer last edited this post 13 years, 7 months ago.]
      • 42492
      • 31 Posts
      Quote from: csauer at Mar 19, 2013, 08:14 AM
      Quote from: BobRay at Mar 18, 2013, 12:19 PM
      Try using getCollection() instead of getIterator(). I've had trouble with getIterator() and nesting, since getIterator() doesn't actually get the data until it's needed and if it's not needed in the snippet with the getIterator() call, it may not show up at all.

      In general, though, I think I would try to redesign this so that you don't have to nest things so deeply. It appears that you have a resource field tag inside a link tag inside a chunk inside a snippet inside a chunk. I try not to ever get more than two, or occasionally three levels deep.

      Thanks a lot Bob. Not nesting so deeply fixed the issue. In your opinion, what element is better suited to be exposed to the user? Should I put a snippet into the resource ( [[ArchivedNews]] )? Or should I put that behavior into a chunk ( [[$ArchivedNews]] )?

      Scratch that. The problem has returned. I don't know what made it work for a few requests, but now I am back to the old buggy behavior again...

      I removed all nesting completely. I have a snippet that fetches the objects and builds up all html itself; no chunk is ever called now. Yet this weird link behavior is still present.

      I noticed that I am calling getCollection() on the $modx object. The documentation mentions that it is a member of $xpdo. Is this a problem?

      Updated code:
      <?php
      //Search for modDocuments...
      $c = $modx->newQuery('modDocument',null);
      $c->where(array(
      	'published' => '1',
      	'isfolder' => '0',
      	'context_key' => 'news',
      ));
      $c->sortby('publishedon', 'DESC');
      
      //Limiting and offset parameters
      $limit = $modx->getOption('archived_num_articles');
      if($modx->resource->get('id') == $modx->getOption('site_start')) {
      	$offset = $modx->getOption('home_num_featured');
      } else {
      	$offset = $modx->getOption('news_num_featured');
      }
      $c->limit($limit, $offset);
      
      //Get docs
      $docs = $modx->getCollection('modDocument', $c);
      
      $output = '<div id="news">';
      $output .= '<h4 class="title">News Resources</h4>';
      $output .= $modx->runSnippet('Wayfinder', array(
      	"contexts" => "web",
      	"startId" => 47,
      	"outerTpl" => "NewsResourcesTpl",
      ));
      $output .= '<ul class="dept-news-ul">';
      foreach($docs as $doc) {
      	$properties = $doc->toArray();
      	//$output .= $modx->getChunk('DepartmentalNewsListItem', $properties);
      	$output .= '<li class="dept-news-li">';
      	$output .= '<a href="'.$modx->makeUrl($properties['id']).'">';
      	$output .= $properties['pagetitle'];
      	$output .= '</a></li>';
      }
      
      $output .= '</ul>';
      $output .= '</div>';
      return $output;



      [---EDIT---]
      Ok I've been playing with the code and think I have the problem pinpointed. It seems to be in this block:
      //Search for modDocuments...
      $c = $modx->newQuery('modDocument');
      $c->where(array(
      	'published' => '1',
      	'isfolder' => '0',
      	//'context_key' => 'news',
      ));
      $c->sortby('publishedon', 'DESC');
      


      If I comment out 'context_key' => 'news' the snippet grabs links from the web context by default and all the links work perfectly. I am doing some context routing to make the news context act as a subdirectory (i.e. www.mysite.com/news). Here is the plugin that is doing the routing:

      <?php
      $pieces = explode('/', trim($_REQUEST[$modx->getOption('request_param_alias', null, 'q')], '/'), 2);
      if (count($pieces) > 0) {
          switch ($pieces[0]) {
              case 'news':
                  if (isset($pieces[1])) {
                      $_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = $pieces[1];
                  } else {
                      $_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = '';
                  }
                  $modx->switchContext('news');
                  break;
              default:
                  break;
          }
      }
      


      Could there be any relation between the routing and the weird link problem occurring when I specify the context_key in the criteria? [ed. note: csauer last edited this post 13 years, 7 months ago.]
        • 3749
        • 24,544 Posts
        Yes, I think the plugin could be a factor, though I don't understand how putting the context_key in the where() method could affect that. The first thing to try is to change your $makeUrl() call to this form:


        $modx->makeUrl($properties['id'], "", "", "full");


        The full line should be:

        $output .= '<a href="'.$modx->makeUrl($properties['id'], "", "", "full").'">';



        Calling $modx->getCollection() is fine. I do it all the time.
          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
          • 42492
          • 31 Posts
          Thanks for the reply, Bob.

          It's still not working. The hrefs are only generated correctly after I refresh the page...

          The snippet being called from the home page works fine using the basic site template. When I call the snippet from the news article template (for permalinks) it won't generate the url on the first go. Only after refreshing will they show up.

          Is it possible to completely disable all caching so I can see if that is the issue?
          [EDIT] I set the cache options cache_resource and cache_disabled accordingly and it didn't resolve anything.

          [EDIT]
          Ok I just realized that in the Home page's case the snippet is being called directly from the resource. Where in the permalinks' case the snippet is called from a template that renders news posts. The home page generates the links correctly, the templated permalinks do not.

          In fact any time I call the snippet from within the web context it works. Anytime I call it from within the news context it does not work...I have no idea what this means. I switched my context routing scripts to use this gist after reading the forum post located here becuse it is exactly what I am trying to accomplish. The routing is working correctly except for generating these links. [ed. note: csauer last edited this post 13 years, 7 months ago.]
            • 42492
            • 31 Posts
            Any other ideas?
              • 3749
              • 24,544 Posts
              Ah. I think we might be getting somewhere. $modx->makeUrl() actually calls $context->makeUrl(), so the unless the context of the Resource = the current context ($modx->context->get('key') ), $makeUrl() won't work without the context argument.


              Try this:

              $ctx = $properties['context_key'];
              $output .= '<a href="'.$modx->makeUrl($properties['id'], $ctx, "", "full").'">';
                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
                • 42492
                • 31 Posts
                Quote from: BobRay at Mar 25, 2013, 01:54 PM
                Ah. I think we might be getting somewhere. $modx->makeUrl() actually calls $context->makeUrl(), so the unless the context of the Resource = the current context ($modx->context->get('key') ), $makeUrl() won't work without the context argument.


                Try this:

                $ctx = $properties['context_key'];
                $output .= '<a href="'.$modx->makeUrl($properties['id'], $ctx, " ",="" "full").'"="">';
                </a>

                This seems to be on the right track. It still hasn't solved the problem, however sad
                I have to click the links twice to get them to generate correctly.

                Why is it that when I set the cache flag on newQuery to false that no links are generated? I have also set the cache flag to false on getCollection. Still a no go...
                <?php
                //Search for modDocuments...
                $c = $modx->newQuery('modDocument', false);
                $c->where(array(
                	'published' => '1',
                	'isfolder' => '0',
                	'context_key' => 'news',
                ));
                $c->sortby('publishedon', 'DESC');
                
                //Limiting and offset parameters
                $limit = $modx->getOption('archived_num_articles');
                if($modx->resource->get('id') == $modx->getOption('site_start')) {
                	$offset = $modx->getOption('home_num_featured');
                } else {
                	$offset = $modx->getOption('news_num_featured');
                }
                $c->limit($limit, $offset);
                
                //Get docs
                $docs = $modx->getCollection('modDocument', $c, false);
                
                $output = '<div id="news">';
                $output .= '<h4 class="title">News Resources</h4>';
                $output .= $modx->runSnippet('Wayfinder', array(
                	"contexts" => "web",
                	"startId" => 47,
                	"outerTpl" => "NewsResourcesTpl",
                ));
                $output .= '<ul class="dept-news-ul">';
                foreach($docs as $doc) {
                	$properties = $doc->toArray();
                	$ctx = $properties['context_key'];
                	$url = $modx->makeUrl($properties['id'], $ctx, "", "full");
                	//$output .= $modx->getChunk('DepartmentalNewsListItem', $properties, false);
                	$output .= '<li class="dept-news-li">';
                	$output .= '<a href="' . $url . '">';
                	$output .= $properties['pagetitle'];
                	$output .= "</a></li>";
                	//$output .= $ctx;
                }
                
                $output .= '</ul>';
                $output .= '</div>';
                return $output;


                Also, these are my settings in the news context:
                http_host: sub.domain.example.com
                base_url: /news/
                Site start: 81
                site_url: http://sub.domain.example.com/news/

                I don't know if these could affect anything. [ed. note: csauer last edited this post 13 years, 6 months ago.]
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  I'm afraid I don't know why that's happening, but I'm adding it to my list of reasons why I never use Contexts unless I absolutely have to. wink


                  You might try using a fully qualified URL in the base_url Context Setting.

                  It could also possibly be a permissions problem if the current use doesn't have a proper Context Acess ACL entry for the other context, though I wouldn't expect that to cause what you're seeing.. [ed. note: BobRay last edited this post 13 years, 6 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
                    • 42492
                    • 31 Posts
                    Yeah, I think I agree with you on contexts now...

                    If I switched from using a context to using a resource group, how would I go about that change? I have around 200 articles already in the news context, so the MODX interface of individually dragging each article into a resource group is a little less than desirable (when is that interface getting updated?!).

                    Would I create a resource group, set up a respective user group, and then create the ACL's in a manner similar to contexts?
                      • 3749
                      • 24,544 Posts
                      Before doing anything rash, you should experiment a little to see if using Resource Groups and ACL entries will do exactly what you want and think about the workflow for it over time. The resource Group/ACL approach can also cause headaches, and using Contexts does give you some options that are not available with Resource Groups (e.g., Context Settings).


                      Moving the resources into a resource group and back to the web Context is really easy to do in a utility snippet:


                      $docs = $modx->getCollection('modResource', array('context_key' => 'myContext'));
                      
                      $count = count($docs);
                      
                      foreach ($docs as $doc) {
                      
                         $doc->set('context_key', 'web');
                         $doc->joinGroup('MyResourceGroup');
                         if ($doc->save()) {
                             $output .= '<br />Moved resource: ' . $doc->get('pagetitle');
                         } else {
                             $output .= '<br />Faile to move resource: ' . $doc->get('pagetitle');
                         }
                      }
                      
                      $output .= '<br /><br />Moved ' . $count . ' documents';
                        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