{"id":267,"date":"2014-01-25T18:34:15","date_gmt":"2014-01-25T23:34:15","guid":{"rendered":"http:\/\/agileadam.com\/?p=267"},"modified":"2014-01-30T15:33:13","modified_gmt":"2014-01-30T20:33:13","slug":"creating-field-groups-in-a-custom-module","status":"publish","type":"post","link":"https:\/\/agileadam.com\/2014\/01\/creating-field-groups-in-a-custom-module\/","title":{"rendered":"Creating Field Groups in a Custom Module"},"content":{"rendered":"

If you need your module to add a fieldgroup to your Drupal 7 site, follow this quick and easy process:\u00a0Also, if you’re looking to create fields, see this\u00a0related post<\/a>.<\/span><\/p>\n

    \n
  1. Build the fieldgroup through the “manage fields” UI.<\/li>\n
  2. Modify the code below to include a proper\u00a0$fieldgroup_name,\u00a0<\/em>$entity_type<\/em>\u00a0and\u00a0$bundle_name<\/em>. I needed to add a field to the user profile, so I set the\u00a0$entity_type<\/em>\u00a0to “user” and the\u00a0$bundle_name<\/em>\u00a0to “user”.\n
    $fieldgroup_name = ''; \/\/ e.g., group_author_info\r\n$entity_type = '';     \/\/ e.g., node\r\n$bundle_name = '';     \/\/ e.g., story\r\n\r\ninclude_once DRUPAL_ROOT . '\/includes\/utility.inc';\r\n$group = field_group_load_field_group($fieldgroup_name, $entity_type, $bundle_name, 'form');\r\n$group_var = '$' . $fieldgroup_name . ' = ' . substr(drupal_var_export($group), 9) . \";\\r\";\r\n$group_var = preg_replace(\"\/  'id'.*,\\n\/\", '', $group_var); \/\/ remove the ID property\r\n$group_var = preg_replace(\"\/  'export_type'.*,\\n\/\", '', $group_var); \/\/ remove the export_type property\r\n$output = $group_var . 'field_group_group_save($' . $fieldgroup_name . ');';\r\ndrupal_set_message(\"<textarea rows=30 style=\\\"width: 100%;\\\">\" . $output . '<\/textarea>');<\/pre>\n<\/li>\n

    <\/p>\n

  3. Run this through the Devel “Execute PHP Code” page, or use Drush.<\/li>\n
  4. Paste the output within your module’s hook_install()<\/em> function within\u00a0mymodule.install<\/em>. You should make sure this code is executed\u00a0after\u00a0<\/em>its children are created (if you’re doing that in the same hook_install()<\/em> implementation.<\/li>\n
  5. Cleanup the formatting.<\/li>\n
  6. Delete the fieldgroup you created through the UI. (warning<\/em>: glance at step 8.2 below before continuing)<\/li>\n
  7. Enable your module.<\/li>\n
  8. Check if the fieldgroup shows up. If the fieldgroup doesn’t show up:\n
      \n
    1. Check if row exists in field_group table. If no, check logs for errors. Also, you can wrap dpm() around the\u00a0field_group_group_save\u00a0<\/em>function to see the error as hook_install()<\/em> runs.<\/li>\n
    2. Make sure this fieldgroup has a “b” property value of “0” in the {variable} table’s “default_field_group<\/em>” variable. It will not show in the form if it has a value of 1. I encountered this problem because deleting the fieldgroup (step 6 above) left that fieldgroup within\u00a0default_field_group<\/em> but set the value to 1. I would’ve expected the value to go away altogether, but it is not in my installation.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n

      Example<\/h3>\n
      \/**\r\n * Implements hook_install().\r\n *\/\r\nfunction activecollab_install() {\r\n  $group_activecollab_settings = (object) array(\r\n    'identifier' => 'group_activecollab_settings|user|user|form',\r\n    'group_name' => 'group_activecollab_settings',\r\n    'entity_type' => 'user',\r\n    'bundle' => 'user',\r\n    'mode' => 'form',\r\n    'parent_name' => '',\r\n    'table' => 'field_group',\r\n    'type' => 'Overridden',\r\n    'disabled' => FALSE,\r\n    'export_module' => 'activecollab',\r\n    'label' => 'Active Collab Settings',\r\n    'weight' => '3',\r\n    'children' => array(\r\n      'field_activecollab_apikey',\r\n    ),\r\n    'format_type' => 'fieldset',\r\n    'format_settings' => array(\r\n      'formatter' => 'collapsible',\r\n      'instance_settings' => array(\r\n        'description' => '',\r\n        'classes' => '',\r\n        'required_fields' => 1,\r\n      ),\r\n    ),\r\n  );\r\n  field_group_group_save($group_activecollab_settings);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

      If you need your module to add a fieldgroup to your Drupal 7 site, follow this quick and easy process:\u00a0Also, if you’re looking to create fields, see this\u00a0related post. Build the fieldgroup through the “manage fields” UI. Modify the code below to include a proper\u00a0$fieldgroup_name,\u00a0$entity_type\u00a0and\u00a0$bundle_name. I needed to add a field to the user profile, so I set the\u00a0$entity_type\u00a0to “user” and the\u00a0$bundle_name\u00a0to “user”. $fieldgroup_name = ”; \/\/ e.g., group_author_info $entity_type = ”; \/\/ e.g., node $bundle_name = ”; \/\/ e.g., story include_once DRUPAL_ROOT . ‘\/includes\/utility.inc’; $group = field_group_load_field_group($fieldgroup_name, $entity_type, $bundle_name, ‘form’); $group_var = ‘$’ . $fieldgroup_name . ‘ = ‘ . substr(drupal_var_export($group), 9) . “;\\r”; $group_var = preg_replace(“\/ ‘id’.*,\\n\/”, ”, $group_var); \/\/ remove the ID property $group_var = preg_replace(“\/ ‘export_type’.*,\\n\/”, ”, $group_var); \/\/ remove the export_type property $output = $group_var . ‘field_group_group_save($’ . $fieldgroup_name . ‘);’; drupal_set_message(“<textarea rows=30 style=\\”width: 100%;\\”>” . $output . ‘<\/textarea>’);<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[84,83,89,90,91],"_links":{"self":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/267"}],"collection":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":8,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/267\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}