【问题标题】:Creating Drupal 7 Field Groups Programmatically以编程方式创建 Drupal 7 字段组
【发布时间】:2013-02-22 19:43:24
【问题描述】:

我正在尝试自动对由我的自定义模块创建的字段进行分组,但它并不完全有效。我的期望是创建一个字段组并将现有字段分配给它。我正在使用 Field Groups 模块,因为它看起来很标准。

根据http://drupal.org/node/1606758 的建议,我已使用 ctools 批量导出来生成代码。我已将以下代码移动到我的模块中。不知道为什么这不起作用:

  • 我的hook_field_group_info()hook_ctools_plugin_api() 都被击中了;正在记录我添加的调试标志。
  • 指定的字段命名正确并且确实存在于数据库中。

我可能做错了什么?

========== 来自 Ctools Bulk Exporter:===========

将其放入 nmc_directory.info

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

将其放在 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);
  }
}

将其放入 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;
}

【问题讨论】:

    标签: drupal drupal-7 field


    【解决方案1】:

    供将来参考 - 此代码(和方法)完美运行。

    我的问题是这个模块变得如此庞大(4 种内容类型、60 多个字段、6 个词汇表等),以至于它在我的本地主机上运行得非常缓慢。当我在登台服务器上运行它时,它运行得非常好。我提高了我的 localhost php.ini 上的最大执行时间,现在它在本地也可以正常工作了。

    FWIW:我能够使用 /devel/reinstall(来自 Devel 模块)重新安装模块,比通过模块管理页面快一点。不知道为什么这也没有超时,但它没有。

    编辑: 嗯..所以几天后,我不确定问题完全是由于php执行时间造成的。这发挥了作用(某些字段从未创建 + 加上其他问题),但现在卸载和重新安装我的模块会产生字段组的分散结果。

    • 有些字段组根本没有出现(数据库中的{field_group}表中没有记录)
    • 但是,批量导出器模块确实将我的组列为可导出
    • 添加的字段组不一定将指定的权重应用于其子项
    • 字段组似乎在卸载我的模块时被删除 (which follows this methodology)

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 1970-01-01
      • 2014-11-17
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多