1

我尝试通过这个钩子向内容类型添加一些字段,就像在 Drupal 7 示例中的 node_example 模块中所做的那样,但它甚至没有被调用。有什么问题?

function education_node_type_insert($content_type){
            $fields = _anketa_installed_fields();
            foreach($fields as $field){
                field_create_field($field);
            }

            $instances = _anketa_installed_instances();
            foreach($instances as $instance){
                $instance['entity_type'] = 'node';
                $instance['bundle'] = 'anketa';
                field_create_field($instance);
            }
    }
4

6 回答 6

2

当您禁用节点模块并将其卸载时,Drupal 不会清除 node_type 表中与您的模块关联的节点类型的条目(我将其称为 Drupal 核心中的错误)。如果这些条目仍然存在;重新启用模块时, hook_node_type_insert 钩子不会运行。

如果您首先从 node_type 表中手动删除这些条目;钩子应该运行。

于 2014-03-24T04:41:44.603 回答
0

您是否尝试过卸载(不是禁用,而是在从卸载选项卡禁用它后真正卸载)模块并再次重新启用它?

于 2013-08-13T16:11:46.307 回答
0

在处理自定义模块时——无论是构建、调试、QAing、迁移、更新等——以下步骤通常会有所帮助。如果不仔细查看您的代码,我建议您尝试以下步骤:

禁用模块,卸载/重新安装(如果可以从数据库中擦除模块数据),重新启用模块然后运行 ​​update.php。检查 Drupal 和 PHP/MySQL 日志,运行 cron.php 清除浏览器和 Drupal 缓存,注销并重新登录,编辑角色和权限。冲洗。重复……经常把无法解释的问题搞得一团糟。

此外,这一切都假设您已经确认整体模块架构和功能名称/拼写是好的。如果一切都失败了,请尝试在另一个实例上安装以查看是否可以复制问题。

于 2013-08-14T13:32:58.440 回答
0

在尝试添加字段之前,您是否已经运行过此程序?因为education_node_type_insert($type)如果节点类型$type已经在node_type数据库表中,则不会调用该函数,它将在第一次运行之后调用。

认为这样做的正确方法是在 的实现中添加字段hook_install(在 中yourmod.install,并同时检查它们是否已经被添加)。

drush dis -y yourmod && drush uninstall -y yourmod && drush en -y yourmod此外,在开发过程中,每次更改字段时都需要卸载-重新安装(例如)。

于 2014-01-16T02:59:32.643 回答
0

在第一次安装后,您将需要手动删除您的节点类型。

function example_uninstall () {
  node_type_delete ('my_type');
}

Drupal 默认不这样做可能有充分的理由:正确的行为是什么?

于 2016-02-06T01:58:06.980 回答
0

为什么不使用 hook_node_insert?

这是为每个新的网络表单添加组件的工作示例:

/**
 * Implements hook_node_insert().
 */
function modulename_node_insert($node) {
  if($node->type == 'webform' && $node->is_new) {
    module_load_include('inc', 'webform', 'includes/webform.components');

    $components = array();
    $components[0] = array(
      'name' => 'Submitted Page URL',
      'nid' => $node->nid,
      'form_key' => 'hidden_submitted_page_url',
      'type' => 'hidden',
      'mandatory' => 0,
      'weight' => 99,
      'pid' => 0,
      'value' => '',
      'required' => 0,
      'extra' => array(
        'hidden_type' => 'hidden',
        'description' => '',
        'wrapper_classes' => 'hidden-submitted-page-url-wrap',
        'css_classes' => 'hidden-submitted-page-url',
        'private' => 0,
      ),
    );
    $components[1] = array(
      'name' => 'Referrer Page URL',
      'nid' => $node->nid,
      'form_key' => 'hidden_referrer_page_url',
      'type' => 'hidden',
      'mandatory' => 0,
      'weight' => 99,
      'pid' => 0,
      'value' => '',
      'required' => 0,
      'extra' => array(
        'hidden_type' => 'hidden',
        'description' => '',
        'wrapper_classes' => 'hidden-referrer-page-url-wrap',
        'css_classes' => 'hidden-referrer-page-url',
        'private' => 0,
      ),
    );
    foreach ($components as $component) {
      webform_component_insert($component);
    }
  }
}
于 2017-02-10T13:59:48.447 回答