2

I'm new to the Craft CMS framework and have a question about tags. How can I create a new tag group?

You can create a tag list by hand via the admin panel > settings > Tag Groups > New Tag Group. This is a way I can't use for plug-in development. So far as I know, I have to use the following code:

    $tagGroup = new TagGroup();
    $tagGroup->name = 'GroupName';
    $tagGroup->handle = 'GroupHandle';
1

You were on the right track. To save things in Craft, you need to use the according service. In your case you need the tags service:

$tagGroup = new TagGroup();
$tagGroup->name = 'GroupName';
$tagGroup->handle = 'GroupHandle';
$isTagGroupSaved = Craft::$app->getTags()->saveTagGroup($tagGroup);

if(!$isTagGroupSaved) {
    // Inspect validation errors if saving the tag group failed
    var_dump($tagGroup->getErrors());
}
1
  • Thanks! This fixed the problem :D
    – randomGuy
    Mar 9 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.