2013-07-30 43 views
1

我试图通过这个钩子添加一些字段到内容类型,就像它在Drupal 7例子中的node_example模块中完成的一样,但它甚至没有被调用。什么可能是错误的?hook_node_type_insert未被调用

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); 
      } 
    } 

回答

0

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

认为做到这一点,正确的方法是添加字段的hook_install而不是实现(yourmod.install,并在同一时间是否他们已经被添加或者检查)。

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

0

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

+0

当然,我已经试过它。 –

0

当处理自定义模块时 - 无论是构建,调试,质量检查,迁移,更新等,以下步骤通常都有帮助。如果不仔细查看代码,我会建议尝试以下步骤:

禁用模块,卸载/重新安装(如果可以从数据块擦除模块数据),重新启用模块,然后运行update.php。检查Drupal & PHP/MySQL日志,运行cron.php清除浏览器& Drupal缓存,注销&重新登录,编辑角色&烫发。冲洗。重复......经常会让无法解释的问题松动。

此外,这一切都假定您已经确认了整体模块体系结构&函数名称/拼写可以。如果一切都失败,请尝试在另一个实例上安装以查看是否可以复制该问题。

1

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

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

0

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

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

有可能是很好的原因,为什么Drupal默认不这样做:什么是正确的行为?

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); 
    } 
    } 
}