1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +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

@@ -106,7 +106,7 @@
* Other
*
* @property int $compile Set to 1 to enable compilation, 2 to compile file and included files, 3 for auto, or 0 to disable. #pw-group-other
* @property string $tags Optional tags that can group this template with others in the admin templates list. #pw-group-other
* @property string $tags Optional tags that can group this template with others in the admin templates list. #pw-group-tags
* @property string $pageLabelField CSV or space separated string of field names to be displayed by ProcessPageList (overrides those set with ProcessPageList config). #pw-group-other
* @property int|bool $_importMode Internal use property set by template importer when importing #pw-internal
* @property int|null $connectedFieldID ID of connected field or null or 0 if not applicable. #pw-internal
@@ -1378,6 +1378,75 @@ class Template extends WireData implements Saveable, Exportable {
return $this->wire('templates')->getPageClass($this, $withNamespace);
}
/**
* Get tags array
*
* #pw-group-tags
*
* @return array
* @since 3.0.176
*
*/
public function getTags() {
$tags = array();
foreach(explode(' ', $this->tags) as $tag) {
if(!strlen($tag)) continue;
$tags[$tag] = $tag;
}
return $tags;
}
/**
* Does this template have given tag?
*
* #pw-group-tags
*
* @param string $tag
* @return bool
* @since 3.0.176
*
*/
public function hasTag($tag) {
$tags = $this->getTags();
return isset($tags[$tag]);
}
/**
* Add tag
*
* #pw-group-tags
*
* @param string $tag
* @return $this
* @since 3.0.176
*
*/
public function addTag($tag) {
$tags = $this->getTags();
if(isset($tags[$tag])) return $this;
$tags[$tag] = $tag;
$this->set('tags', implode(' ', $tags));
return $this;
}
/**
* Remove tag
*
* #pw-group-tags
*
* @param string $tag
* @return self
* @since 3.0.176
*
*/
public function removeTag($tag) {
$tags = $this->getTags();
if(!isset($tags[$tag])) return $this;
unset($tags[$tag]);
$this->set('tags', implode(' ', $tags));
return $this;
}
/**
* Check that all file asset paths are consistent with current pagefileSecure setting and access control
*

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