Upcoming Teaser Outside of Articles

Modifying the Upcoming Teaser snippet to work on other resource types


In the previous article, we looked at a way to display a "teaser" for upcoming blog posts, but what if you want to use the same idea for non-articles? In this one, we'll modify the code slightly to allow it to work with other resource types. This will also be a good example of how to modify a snippet while maintaining backward compatibility.


Tags and Chunks

In order to maintain backward compatibility, neither the tag nor the Tpl chunks needs to be modified, though you'll have to add a classKey property and modify the &parent property if you want to use the snippet with a non-article resource type.

[[!UpcomingTeaser?
    $limit=`0`
    &parent=`12`
    &outerTpl=`UpcomingTeaserOuterTpl`
    &innerTpl=`UpcomingTeaserInnerTpl`
]]


New Code

In order to use the snippet with other resource types, all we need to do is add a &classKey property to the snippet tag, and use its value instead of 'Article' in the snippet. To maintain backward compatibility We'll set 'Article' as the default value for the property so the snippet will work with no changes for blog articles.

/* UpcomingTeaser snippet */

/* Save some typing and space */
$sp = $scriptProperties;

/* Get basic info from the properties */
$limit = $modx->getOption('limit', $sp, '0');
$parent = $modx->getOption('parent', $sp, null);
$outerTpl = $modx->getOption('outerTpl', $sp,
    'UpcomingArticlesOuterTpl');
$innerTpl = $modx->getOption('innerTpl', $sp,
    'UpcomingArticlesInnerTpl');

$classKey= $modx->getOption('classKey', $sp, 'Article', true);

/* Create the query */
$query = $modx->newQuery($classKey);

/* We only want articles that are unpublished children of
   the blog container, and have a pub_date later than the
   current time */
$query->where(
    array(
        'parent' => $parent,
        'published' => '0',
        'pub_date:>=' => time(),

    ),
);

/* This will make sure they are in chronological order */
$query->sortby('pub_date', 'ASC');

/* Set the number of articles to be retrieved */
$query->limit($limit);

/* Get the articles */
$docs = $modx->getCollection($classKey, $query);

if (empty($docs)) {
   /* No upcoming resources, return nothing */
   $output = '';
} else {
    /* No use getting this unless we have something to show */
    $outer = $modx->getChunk($outerTpl);

    /* Iterate through the docs formatting as we go */
    foreach($docs as $doc) {
        $fields = $doc->toArray();
        $inner .= $modx->getChunk($innerTpl, $fields);
    }
    /* Plug the articles list into the outer Tpl chunk */
    $output = str_replace('[[+upcoming_inner]]', $inner, $outer);
}

return $output;

Changes

The only changes are the line that gets the property value, the line where we create the query, and the line where we call getCollection() to get the resources.


classKey Settings

We've made the &classKey a property so the code could be used for other document classes. You might have a custom resource class, or you might just want to use the code to create a teaser for regular web pages rather than blog articles. In that case, you'd just set the &classKey property to the class you want to use.

Note that if you use &classKey=`modResource`, you'll get all upcoming resources, including weblinks, symlinks, static resources, blog articles, and any other objects that extend modResource. If you want just documents and not blog articles, use &classKey=`modDocument`.

Coming Up

What if you want to display both blog posts and other documents (say, on the site's home page)? In the next article we'll modify the snippet to allow multiple class keys.



Comments (0)


Please login to comment.

  (Login)