简体   繁体   中英

Creating Drupal 7 Field Groups Programmatically

I am trying to automatically group fields created by my custom module, but it's not quite working. My expectation is that a field group will be created and existing fields would be assigned to it. I'm using the Field Groups module, as it appears to be pretty standard.

As advised on http://drupal.org/node/1606758 , I have used ctools bulk export to generate the code. I have moved the following code into my module. Not sure why this isn't working:

  • My hook_field_group_info() and hook_ctools_plugin_api() are both being hit; the debugging flags I've added are being logged.
  • The specified fields are named correctly and do exist in the database.

What I might be doing wrong?

========== From Ctools Bulk Exporter: ===========

Place this in nmc_directory.info

name = nmc_directory export module
description = Export objects from CTools
dependencies[] = field_group
package = Chaos tool suite
core = 7.x

Place this in nmc_directory.module

<?php

/**
 * Implements hook_ctools_plugin_api().
 */
function nmc_directory_ctools_plugin_api($module, $api) {
  if ($module == 'field_group' && $api == 'field_group') {
    return array('version' => 1);
  }
}

Place this in nmc_directory.field_group.inc

<?php

/**
 * Implements hook_field_group_info().
 */
function nmc_directory_field_group_info() {
  $field_groups = array();

  $field_group = new stdClass();
  $field_group->disabled = FALSE; /* Edit this to true to make a default field_group disabled initially */
  $field_group->api_version = 1;
  $field_group->identifier = 'group_dir_phys_consumer_info|node|dir_physicians|form';
  $field_group->group_name = 'group_dir_phys_consumer_info';
  $field_group->entity_type = 'node';
  $field_group->bundle = 'dir_physicians';
  $field_group->mode = 'form';
  $field_group->parent_name = '';
  $field_group->data = array(
    'label' => 'Online Medical Providers Directory: Website (Consumer Information)',
    'weight' => '0',
    'children' => array(
      0 => 'dir_phys_gender',
      1 => 'dir_phys_category',
      2 => 'dir_phys_title',
      3 => 'dir_phys_fname',
      4 => 'dir_phys_lname',
      5 => 'dir_phys_suffix',
      6 => 'dir_phys_medfield_1',
      7 => 'dir_phys_medfield_2',
      8 => 'dir_phys_phone_public',
    ),
    'format_type' => 'fieldset',
    'format_settings' => array(
      'label' => 'Online Medical Providers Directory: Website (Consumer Information)',
      'instance_settings' => array(
        'required_fields' => 1,
        'classes' => '',
        'description' => '',
      ),
      'formatter' => 'collapsed',
    ),
  );
  $field_groups['group_dir_phys_consumer_info|node|dir_physicians|form'] = $field_group;

  return $field_groups;
}

For future reference - this code (and the approach) works perfectly.

My problem was that this module is getting so large (4 content types, 60+ fields, 6 vocabularies, etc) that it runs pretty slowly on my localhost. When I ran it on my staging server, it worked very nicely. I bumped up the max execution time on my localhost php.ini and its working fine locally now too.

FWIW: I was able to use /devel/reinstall (from the Devel module) to reinstall the module a little faster than going through the module admin page. Not sure why this didn't timeout too, but it didn't.

Edit: hmm.. so several days later, I'm not as certain that the problem was entirely due to the php execution time. That played a part (some fields were never created + plus other problems), but now uninstalling & reinstalling my module produces scattered results with the field groups.

  • some field groups do not appear at all (there is no record in the {field_group} table in the DB)
  • however, the Bulk Exporter module does list my groups as being available for export
  • field groups that are added don't necessarily apply the specified weights to their children
  • field groups appear to be removed when uninstalling my module ( which follows this methodology )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM