pwFoo Posted December 19, 2014 Posted December 19, 2014 MarkupTagcloud The module generates a simple tagcloud based an page references as tags and a default template. Settings and template can be easily changed to fit your needs. Version 0.5.2Download Processwire module page: http://modules.processwire.com/modules/markup-tagcloud/ Bitbucket Repo: https://bitbucket.org/pwFoo/markuptagcloud/ Usage Readme fileExample output <ul> <li class="tag3"> <a class="tag3" href="/pw/tags/mytag/">myTag</a> </li> <li class="tag1"> <a class="tag1" href="/pw/tags/tag123/">tag123</a> </li> <li class="tag2"> <a class="tag2" href="/pw/tags/hello/">Hello</a> </li> <li class="tag2"> <a class="tag2" href="/pw/tags/tagged/">Tagged</a> </li> <li class="tag1"> <a class="tag1" href="/pw/tags/template/">Template</a> </li> <li class="tag1"> <a class="tag1" href="/pw/tags/template2/">Template2</a> </li> </ul> Tagcloud / list can be styled by css via weight / class. 3
pwFoo Posted December 19, 2014 Author Posted December 19, 2014 Updated to 0.5.2 removed selector method (convert array key => value to a PW selector string) use plain PW selector string as parse() param3 instead of an assoc array minor fixes
Soma Posted December 21, 2014 Posted December 21, 2014 What if I have thousands of pages and more? I think it could give you some problems with performance real quick // get pages as PageArray $tags = $this->pages->find($tagsSelector); // register back references / "tagged" pages and count foreach ($tags as $tag) { $tagged = $this->pages->find("{$tagField}={$tag},{$backRefSelector}"); // pages tagged / referenced to this tag $tag->set('use', $tagged); $tag->set('useCount', $tagged->count()); } All pages are searched for each tag and even cached/stored. At least use a count instead of find $this->pages->count("{$tagField}={$tag},{$backRefSelector}");
pwFoo Posted December 21, 2014 Author Posted December 21, 2014 Hi Soma, I used a count only in the first version, but I also use the module to build a list of tags with tagged content. That's why I need the PageArray "$tagged". But you're right. Dependent on the use case I should use a count (tagcloud) or a find / count combination (tag list with tagged pages)... Is there a better way to get the "tagged pages" of a tag? Thanks!
Soma Posted December 21, 2014 Posted December 21, 2014 A better way? There's no other way than search for them, but to scale you'd maybe use a limit. Also maybe make that optional. Well if you have 50 tags and 1000 pages you'll end up 50'000 pages or even more (since pages can have multiple tags) and it will make 50 finds! It may work but take some time and memory.
pwFoo Posted December 21, 2014 Author Posted December 21, 2014 Would it better to have two finds and work with the page arrays? $tags = $this->pages->find("parent=/tags"); $tagged = $this->pages->find("tags={$tags}"); foreach ($tags as $tag) { if ($match = $tagged->filter($tag)) { $tag->use->add($match->id); } }
pwFoo Posted December 22, 2014 Author Posted December 22, 2014 Updated version 0.5.3 for a better performance with a simple tagclout (without need to get tagged pages). public function parse($tagRoot = null, $tagField = null, $backRef = null, $tagsSelector = null, $backRefSelector = null) { $tagRoot = ($tagRoot ? $tagRoot : $this->tagRoot); $tagField = ($tagField ? $tagField : $this->tagField); $tagsSelector = ($tagsSelector ? "parent={$tagRoot},{$tagsSelector}" : "parent={$tagRoot}"); $backRefSelector = ($backRefSelector ? "$backRefSelector" : ''); // get pages as PageArray $tags = $this->pages->find($tagsSelector); foreach ($tags as $tag) { if (!$backRef) { // only count tagged / referenced pages for better performance $tagCount = $this->pages->count("{$tagField}={$tag},{$backRefSelector}"); $tag->set('useCount', $tagCount); } else { // pages tagged / referenced to this tag $tagged = $this->pages->find("{$tagField}={$tag},{$backRefSelector}"); $tag->set('use', $tagged); $tag->set('useCount', $tagged->count()); } } $this->tags = $tags; return $this; }
pwFoo Posted December 27, 2014 Author Posted December 27, 2014 Would it better to have two finds and work with the page arrays? $tags = $this->pages->find("parent=/tags"); $tagged = $this->pages->find("tags={$tags}"); foreach ($tags as $tag) { if ($match = $tagged->filter($tag)) { $tag->use->add($match->id); } } Because I don't know how PW work with the database and internal data... Would the code above perform better, equal or will it be slower? Two find calls outside of the loop. Inside it working with the page arrays (will that also fire db queries?)
pwFoo Posted March 26, 2015 Author Posted March 26, 2015 It doesn't support an active class. You can set outerTpl and innerTpl used for each row. Instead of use the module own render() method you could parse and get the data to handle it your own... $mtc = $modules->get('MarkupTagcloud'); // parent, tag field $mtc->parse('1003', 'tag'); // all used tags, sort by title (for example) $tags = $mtc->getTags('sort=title'); foreach ($tags as $tag) { $nArticles = $tag->useCount; // your code here... } If you also need the tagged pages you could add a third param "true" to the parse() method, but it will have some overhead. $mtc = $modules->get('MarkupTagcloud'); // parent, tag field, get tagged pages $mtc->parse('1003', 'tag', true); // all used tags, sort by title (for example) $tags = $mtc->getTags('sort=title'); foreach ($tags as $tag) { $nArticles = $tag->useCount; $taggedPages = $tag->use; // your code here foreach ($taggedPages as $taggedPage) { // page object of tagged pages... } } Examples are untested! But why use the overhead with the MarkupTagcloud module if your code works fine?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now