Help - Search - Members - Calendar
Full Version: does MTArchiveCount work within MTArchivePrevious?
Movable Type Community Forum > Additional Resources > Tips and Tricks
otravers
I'm not getting the behavior I'm expecting from the following code:
CODE
<MTArchivePrevious><a href="<$MTArchiveLink$>" title="<$MTArchiveCount$> entries">« <$MTArchiveTitle$></a></MTArchivePrevious>

I'm expecting to get the number of entries for the previous monthly or daily archive (I like to hover metadata over links,) but I always get "1" as a result no matter what's the real number of entries for that period. Am I doing something wrong or is this a bug with MT? MTArchiveCount works fine for me outside of MTArchivePrevious/Next. I'm using MT 2.64.
otravers
I'm reposting this here as I didn't get any answer in the Tips and Tricks forum and I don't see where I might be making a mistake, so maybe it's a bug after all.

I'm not getting the behavior I'm expecting from the following code:
CODE
<MTArchivePrevious><a href="<$MTArchiveLink$>" title="<$MTArchiveCount$> entries">« <$MTArchiveTitle$></a></MTArchivePrevious>

I'm expecting to get the number of entries for the previous monthly or daily archive (I like to hover metadata over links,) but I always get "1" as a result no matter what's the real number of entries for that period. MTArchiveCount works fine for me outside of MTArchivePrevious/Next. I'm using MT 2.64.
girlie
The section of the manual about MTArchiveCount states (emphasis added):

QUOTE
MTArchiveCount
The number of entries in a specific archive grouping; for example, if you are using Monthly archiving, this corresponds to the number of entries in this month. This tag should only be used inside of a <MTArchiveList> container, like this:

<MTArchiveList>
<a href="<$MTArchiveLink$>"><$MTArchiveTitle$> (<$MTArchiveCount$>)</a><br>
</MTArchiveList>
girlie
Merging topics rather than moving post.
girlie
If you don't get a reply to your initial post, please just "bump" it up in the forum by adding a new reply to it, rather than creating a new topic. Thanks! smile.gif
Lummox JR
The reason the count only comes up as 1 is because the handler for the MTArchiveNext and MTArchivePrevious tags only loads one entry into the context. The handler for MTArchiveCount works by counting the number of entries loaded into the context, or by using the archive_count value if one has been stashed.

Apparently, MTEntries won't work here either; you'd only see one entry.

It seems the only solution for now is to 1) create a plugin to load the entries for a time period, and use that tag within MTArchivePrevious and MTArchiveNext, or 2) hack lib/Template/Context.pm to redefine the handler. The hack would look like this:
CODE
   sub _hdlr_archive_prev_next {
       my($ctx, $args, $cond) = @_;
       my $tag = $ctx->stash('tag');
       my $is_prev = $tag eq 'ArchivePrevious';
       my $ts = $ctx->{current_timestamp}
           or return $ctx->error(MT->translate(
              "You used an [_1] without a date context set up.", "<MT$tag>" ));
       my $at = $_[1]->{archive_type} || $ctx->{current_archive_type};
       return $ctx->error(MT->translate(
           "[_1] can be used only with Daily, Weekly, or Monthly archives.",
           "<MT$tag>" ))
           unless $at eq 'Daily' || $at eq 'Weekly' || $at eq 'Monthly';
       my $res = '';
       my @arg = ($ts, $ctx->stash('blog_id'), $at);
       push @arg, $is_prev ? 'previous' : 'next';
       my $helper = $TypeHandlers{$at}{helper};
       if (my $entry = get_entry(@arg)) {
           my $builder = $ctx->stash('builder');

           # Comment out this line
           # local $ctx->{__stash}->{entries} = [ $entry ];

           my($start, $end) = $helper->($entry->created_on);
           local $ctx->{current_timestamp} = $start;
           local $ctx->{current_timestamp_end} = $end;

           # Add this section
           local $ctx->{__stash}->{entries};
           if($args->{all_entries}) {
               my @entries = MT::Entry->load({blog_id => $ctx->stash('blog_id'),
                                              created_on => [$start, $end],
                                              status => MT::Entry::RELEASE()},
                                             {range => {created_on => 1}});
               $ctx->{__stash}->{entries} = \@entries;
               }
           else {
               $ctx->{__stash}->{entries} = [ $entry ];
               }
           # End section

           defined(my $out = $builder->build($ctx, $ctx->stash('tokens'),
               $cond))
               or return $ctx->error( $builder->errstr );
           $res .= $out;
       }
       $res;
   }

I caution that's untested but it should work.

I'd guess the reason MT doesn't load all the entries by default is that there's no reason to do so for most purposes. This hack adds an all_entries argument to the MTArchivePrevious and MTArchiveNext tags, so if you put all_entries="1" in there it should load the entries successfully. Then MTArchiveCount should work, as should MTEntries.

Since a hack is a bit of an ugly solution anyway though, I'd still suggest using a plugin. If no such plugin exists, I'll gladly step up.

Lummox JR
otravers
girlie: sorry for the repost, I assumed I didn't get any answer because I might have posted in the wrong forum. Sorry also for failing to understand the manual (I had read that part but it somehow didn't sink in!)

Lummox JR: thanks for the hack but since I don't know PERL and we don't have anyone in the organization to back me up technically, I'd rather not engage in tweaks I don't fully understand!

A plugin to support previous/next entry count might be useful to me (moderately so, it's not mission critical,) though as I said in another post I'm uncomfortable asking people to do work that I can't pay for. Unless you really want to do it for your own usage, don't worry too much, as I'll probably find a solution using MTArchiveList and PHP (though I'm not sure I really understand how MTArchiveDate and MTArchiveDateEnd are supposed to work.)
Lummox JR
After reading your post I realized that the failure of next/previous contexts to load entries also has implications for other kinds of contexts, so I did go ahead and build that plugin. One of the things I figured it'd be useful for was something I hadn't realized wasn't possible with my ArchiveYear plugin: Listing all entries in a year. That plugin sets up contexts for each month, but doesn't load any of the entries.

I'm not ready to officially announce the release until I can also update another plugin, but anyway it's up and you can try it out: ArchiveLoad

The <MTArchiveLoad> container tag introduced by the plugin will load all of the entries from a date-based archive context, allowing <MTEntries> and <MTArchiveCount> to function.

Lummox JR
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.