Skip to content

Instantly share code, notes, and snippets.

@bfncs
Last active December 19, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfncs/5936650 to your computer and use it in GitHub Desktop.
Save bfncs/5936650 to your computer and use it in GitHub Desktop.
Processwire Module: HelperAuthorTags
<?php
class HelperAuthorTags extends WireData implements Module
{
public static function getModuleInfo()
{
return array(
'title' => 'Helper Author Tags',
'description' => 'Regenerate author tags on article save',
'version' => 1,
'singular' => true,
'autoload' => true
);
}
public function init()
{
$this->pages->addHookAfter("save", $this, "hookPageSave");
}
public function hookPageSave(HookEvent $event)
{
$page = $event->argumentsByName("page");
/**
* @var Pages $pages
*/
$pages = wire('pages');
if ($page->template == 'works-single') {
/**
* @var tpl_works_single $page
*/
if (!($page->isChanged('workAuthors')
|| $page->isChanged('workTags')
|| $page->isChanged('workMedium'))) {
return;
}
foreach ($page->workAuthors as $author) {
/**
* @var tpl_authors_single $authorPage
*/
$authorPage = $pages->get("id=$author");
// remove all tags first
$authorPage->authorTags->removeAll();
// get all relevant tags again
$authorTags = new PageArray();
$authorArticles = $pages->find("template=works-single, workAuthors=$author");
foreach ($authorArticles as $article) {
$authorTags->import($article->workTags);
}
// sort tags by number of works
$authorTags = $authorTags->sort('-numPages');
// add tags to the author's page and save it
$authorPage->authorTags = $authorTags;
$authorPage->save('authorTags');
// tell the user about it
$this->message("Regenerated tag data for author $author->title");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment