Adding your own form_builder palette group

Matthias Weiss's picture
Matthias Weiss

Defining and populating your own palette group that should work along with the default palette of form_builder is easy once you know how to do it.

First, in the new drupal module we're implementing, we define our new pallet group with the hook_form_builder_palette_groups hook:

 * Implements hook_form_builder_palette_groups().
 * we want to define our own group of form elements
 * hence we define our own palette of elements
function my_module_form_builder_palette_groups() {

  return array(
      'my_palette_group' => array(
      'weight' => -17,
      'title' => t('My palette group title'),

This is quite self explanatory, my_palette_group is the name of this newly defined palette group that we have to use in your field definitions. Note that by specifying the weight attribute we can define whether our palette group is positioned above or below the default palette group. Smaller or negative values for weight will move it up, bigger positive values will move it down.

And this is how we put a field type into our palette group, we look again into the _my_module_form_builder_types_textfield function of my previous blog post:

function _my_module_form_builder_types_textfield() {
  $fields = array();

  $fields['first_name'] = array(
    'title' => t('First name'),
    'weight' => -80,
  $fields['first_name']['default'] = _form_builder_webform_default('textfield');
  $fields['first_name']['default']['#title'] = t('First name');
  $fields['first_name']['palette_group'] = 'my_palette_group';

  return $fields;

The important addition is of course this line

  $fields['first_name']['palette_group'] = 'my_palette_group';

This line adds the field element first_name to the palette group my_palette_group.


Add new comment