Custom block type set

Hello, I want to create a custom bloc type set (like ‘Basic’, ‘Navigation’, ‘Form’, …) in order to group all my custom blocks together.
I created a package for my theme and created the page-theme.php with the code below, but it doesn’t work.
Any ideas ?
Is the page-theme.php the best choice to execute this code ?
Is there a method to remove a bloc type set programmatically ?


<?php  
namespace Concrete\Package\Sfgs;
defined('C5_EXECUTE') or die('Access Denied.');

use \Concrete\Core\Package\Package;
use BlockTypeSet;

class Controller extends Package
{
    ... code with the package handle, version, description, ...

	
    public function makeMyBlockSet($handle,$name){
		if (!BlockTypeSet::getByHandle($handle)) {
			BlockTypeSet::add($handle, $name, $pkg);
		}
	}

    public function install()
    {
        parent::install();
		
		$this->makeMyBlockSet('myblocset_handle','My Block Set');

                $this->installContentFile('install/theme.xml');
		$this->installContentFile('install/block_types.xml');		
    }

    public function upgrade()
    {
        parent::upgrade();
    	$this->installContentFile('install/theme.xml');
	$this->installContentFile('install/block_types.xml');
    }
}

I solved the problem by myself : $pkg variable has to be removed from the function

public function makeMyBlockSet($handle,$name){
		if (!BlockTypeSet::getByHandle($handle)) {
			BlockTypeSet::add($handle, $name);
		}

The question how to remove the set programmatically is still open however :thinking:

@Junglebob This is how I do it in package controller, calling this method from uninstall()

private function cleanBlockTypeSets() {
		$blockTypeSet = BlockTypeSet::getByHandle('set_handle');
		$blockTypes = $blockTypeSet->getBlockTypes();
		$count = 0;
		foreach ($blockTypes as $blockType) {
			if ($blockType) {
				// Ignore block types in this package.
				if ($blockType->getBlockTypeHandle() == 'block_type_handle') { continue; }
				$count++;
			}
			if ($count == 1) { break; } // Another block type needs the BlockTypeSet, bail out of loop.
		}
		// Clear, then delete set if otherwise empty.
		if ($count == 0) { $blockTypeSet->clearBlockTypes(); $blockTypeSet->delete(); }
	}
1 Like

Many thanks, it helped me much !

1 Like

@Junglebob glad that resolved it - can you go ahead and mark @enlil’s response as the solution for the thread? Thanks!