如何在Drupal7中隐藏hook_disable上的自定义用户配置文件字段



我创建了一个模块,它向用户配置文件中添加了一个字段。我使用了field_create_field和field_create_instance来完成此操作。当我禁用该模块时,我希望该字段不再显示在用户配置文件中,但我不想破坏它。我希望能够启用该模块,并显示该字段,并且数据仍保持原来输入的状态。有人能告诉我如何做到这一点吗?

这是我用来创建字段的代码:

  $field = array(
    'field_name'  => $field_name,
    'type'        => 'text',
    'visibility'  => 1,
    'category'    => 'API',
  );
  $field = field_create_field($field);
  $field_instance = array(
    'field_name'    => $field_name,
    'entity_type'   => 'user',
    'bundle'        => 'user',
    'label'         => t('API Token'),
    'cardinality'   => 1,
    'translatable'  => 0,
    'description'   => t('By using this API token, you agree to the site <a href="/about/site-policies/terms-and-conditions">Terms and Conditions</a> and to acknowledge that your submission does not include protected health information or personal identifiers.'),
    'widget'        => array(
      'type'        => 'text_textfield',
      'weight'      => 10,
    ),
    'formatter'     => array(
      'label'       => t('field formatter label'),
      'format'      => 'text_default'
    ),
    'settings'      => array(
    ),
  );

当您使用drupal实体(如用户、节点等)创建字段时,crud操作会自动应用于该实体。

由于您使用了"field_create_field"字段的api,因此当您卸载模块时,它会使用实体的api自动创建字段,而不是删除字段。

首先告诉我,当您卸载自定义模块时,然后从配置文件中删除自定义字段。?如果是,那么很难处理您的用例。如果没有,那么在Drupal的系统表中,您可以获得模块的状态,无论是禁用还是启用。如果状态为0,则使用户配置文件的alter hook和隐藏字段

我没能完全完成我想要的,但我最终安装了字段额外的小部件模块,并将字段完全隐藏在编辑表单上。然后,我使用钩子菜单alter创建了一个本地任务选项卡,并在该选项卡上显示字段。

最新更新