Jump to content

Cross-referencing tag used in Pages


j00st
 Share

Recommended Posts

Hi Everyone!

Wondering if the community can help me out on the following issue I'm struggling with;

I'm using a Page titled Tags, with children Pages for separate Tags (a Tag, maybe obvious, but still...) 
These tags are being used as...tags! From a dropdown on the article-page in the CMS. Works like a charm. But...

I actually want to use these tags as cross-reference as well. Basically I want to make a page with all tags on it,
and for each tag have a list with all the Pages it's connected to (and preferably even pull content from those pages).
Might be I overlooked a previous post on this subject, or that there's a Module for these kind of things,
but since I couldn't find anything so far, I thought I'd try my luck here! 

Ideas, options, solutions, suggestions...all welcome!
Thanks!

Link to comment
Share on other sites

@j00st, you might like to take a look at the Connect Page Fields module:

Once the module is installed it takes care of keeping the fields in sync but if you already have your articles(?) tagged you would write some API code for a one-time operation to iterate over your article pages and add them to the selected tag pages.

  • Like 1
Link to comment
Share on other sites

Hi Sérgio and Robin, thanks for your quick replies!

@Sérgio; yes, that would be the result, with the tag clickable to show the connected articles. Is this something you're familiar with in setting it up? And with my suggestion to showing the related articles would you refer to Robin's plugin?

@Robin S; Ah good, I'll definitely have a look at the module! Just read through the page quickly, and the basis of it would be for me to link a Pagefield 'articles' on each Tag page, and on the Article use the existing Pagefield Tags, correct?
And to perhaps complicate it; If I also want to connect the authors of articles to a tag, this would be another connected field pair right?
 

Link to comment
Share on other sites

1 hour ago, j00st said:

@Robin S; Ah good, I'll definitely have a look at the module! Just read through the page quickly, and the basis of it would be for me to link a Pagefield 'articles' on each Tag page, and on the Article use the existing Pagefield Tags, correct?
And to perhaps complicate it; If I also want to connect the authors of articles to a tag, this would be another connected field pair right?

Yes, correct on both.

One advantage of the Connect Page Fields module is that it can make the generation of your tags page more efficient. How much so depends on what you want to show on the tags page. To explain...

For a page like the screenshot from @Sérgio, with a count next to each tag link, the typical way using only common API methods would be something like:

$tags = $pages->find("template=tag, sort=title");
foreach($tags as $tag) {
    $count = $pages->count("tags=$tag"); // count how many pages have this tag selected
    // now output the tag link and count
}

And if you happened to want to list the articles that use each tag underneath that tag you would do something like:

$tags = $pages->find("template=tag, sort=title");
foreach($tags as $tag) {
    $tagged_pages = $pages->find("tags=$tag"); // find pages that have this tag selected
    // now output the tag and the list of pages
}

A $pages->count() has less overhead than a $pages->find(), but it's still a lot of DB queries if you have a lot of tags. You'd probably want to cache this if you did it this way.

With Connect Page Fields, the pages that use the tag are stored in a Page field so you can easily get a count or the pages themselves directly:

$tags = $pages->find("template=tag, sort=title");
foreach($tags as $tag) {
    // you probably wouldn't bother assigning these to variables but just for demonstration
    $count = $tag->articles->count();
    $tagged_pages = $tag->articles;
    // output your tag, count, etc
}

And for one more option involving a single SQL query on the tags table see this:

 

  • Like 3
Link to comment
Share on other sites

@Robin S's code is an excellent approach, @j00st! The ConnectPageFields module is great to have the admin user manage the relations is a much easier way. I have to implement it on this site the screenshot came from yet, as when I developed these connections the module wasn't available. :)

As for your question, the code I used to create this page shown on the screenshot is taken from Ryan's blog profile tag template, mine version is below:


/**
 * Generate a list of tags A-Z
 *
 * Used by the /site/templats/tags.php template file.
 *
 * VARIABLES:
 * ==========
 * $tags PageArray of tags
 *
 * Each of the tags has a numPosts property containing the number of posts used by the tag.
 *
 */

$lastLetter = '';
$out = '';
$letters = array();
$tags = $page->children();

foreach($tags as $tag) {
  $letter = strtoupper(
    substr(
      $sanitizer->pageName($tag->title, true)
    , 0, 1
    )
  );

  if($letter != $lastLetter) {
    if($lastLetter) $out .= "</ul></div>";
    $out .= "<div class='letter-group'>";
    $out .= "<h3 id='letter_$letter' class='bg-tags text-center'>$letter</h3>";
    $out .= "<ul class='no-bullet'>";
    $letters[] = $letter;
  }
  $lastLetter = $letter;

  $page_count = $pages->count("tags=$tag");
  $class = $page_count >= 10 ? 'u-strong' : '';

  if($page_count > 1) {
    $out .= "<li class={$class}><a href='{$tag->url}'>{$tag->title} <span class='color-tags-dark25'>({$page_count})</span></a></li>";
  }

}
$out .= "</ul></div>";
/I'm using TemplateLatteReplace module to outup to a view file.
$view->letters = $letters; 
$view->tag_list = $out;

 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...