1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-23 06:44:38 +02:00

Add tag helper methods to Template class (to work with existing 'tags' property similar to the one in Field class)

This commit is contained in:
Ryan Cramer
2021-04-22 12:49:43 -04:00
parent e67c1c075c
commit 1b1dcc85db
2 changed files with 99 additions and 1 deletions

View File

@@ -785,6 +785,35 @@ class Templates extends WireSaveableItems {
return $pageClass;
}
/**
* Get all tags used by templates
*
* @param bool $getTemplateNames Get arrays of template names for each tag? (default=false)
* @return array
* @since 3.0.176
*
*/
public function getTags($getTemplateNames = false) {
$tags = array();
foreach($this as $template) {
/** @var Template $template */
$templateTags = $template->tags;
if(empty($templateTags)) continue;
$templateTags = explode(' ', $templateTags);
foreach($templateTags as $tag) {
if(empty($tag)) continue;
if($getTemplateNames) {
if(!isset($tags[$tag])) $tags[$tag] = array();
$tags[$tag][$template->name] = $template->name;
} else {
$tags[$tag] = $tag;
}
}
}
ksort($tags);
return $tags;
}
/**
* Set a Permission for a Template for and specific Role