Grouping Attribute Keys into Sets

Once you've created attribute keys through your package installation, you'll probably find that you want to add them into attribute sets – either completely new sets you create for your package, or existing sets created by the core. For example, in our previous doc we created an "Exclude Subpages from Nav" attribute. It sure would be nice if this attribute were listed with similar attributes in the "Navigation and Indexing" set, wouldn't it? Fortunately, adding support for this is a simple matter. First, you'll need access to the newly created key object. In this code:

$key = $category->getByHandle('exclude_subpages_from_nav');
if (!is_object($key)) {
    $key = new PageKey();
    $key->setAttributeKeyHandle('exclude_subpages_from_nav');
    $key->setAttributeKeyName('Exclude Subpages from Nav');
    $key = $category->add('boolean', $key, null, $pkg);
}

The $key variable contains the attribute key in question. This object is the same whether it's retrieved via getByHandle or created via $category->add(). Once you have the attribute set, you'll need to retrieve the attribute set entity. From within your package controller, run this code:

$factory = $this->app->make('Concrete\Core\Attribute\SetFactory');
$navSet = $factory->getByHandle('navigation'); // the built-in navigation set

Now, use this code to associate the key to the set.

$service = $this->app->make('Concrete\Core\Attribute\Category\CategoryService');
$categoryEntity = $service->getByHandle('collection');
$category = $categoryEntity->getController();
$setManager = $category->getSetManager();
$setManager->addKey($navSet, $key);

That's it! This looks confusing, so let's break it down:

  1. Use the generic category service object to
  2. retrieve the page version of the Concrete\Core\Entity\Attribute\Category object, then 3. getting its specific controller class, Concrete\Core\Attribute\Category\PageCategory. 4. Finally, we get the category set manager, which is Concrete\Core\Attribute\StandardSetManager
  3. And associate the set with the key.

Creating a New Attribute Set and Associating Keys to It

The approach is similar if we want to create a completely new set and add this key to it. First, check to see if the set you want to add doesn't already exist:

$factory = $this->app->make('Concrete\Core\Attribute\SetFactory');
$set = $factory->getByHandle('fancy_new_set');
if (!is_object($set)) {

}

If $set is null, we can proceed with adding the set:

$service = $this->app->make('Concrete\Core\Attribute\Category\CategoryService');
$categoryEntity = $service->getByHandle('collection');
$category = $categoryEntity->getController();
$setManager = $category->getSetManager();
$set = $setManager->addSet('fancy_net_set', 'Fancy New Set', $pkg);

That's it! Now that we have the $set object we can continue with associating keys to it using the code above.

$setManager->addKey($set, $key);