需要建议在d7中使我的逻辑正确

时间:2011-12-19 15:49:52

标签: drupal drupal-7

目标:是保存更新的节点的信息。我们需要收集更新的节点的节点ID以及为其添加书签的人的用户名。

实施

我已经设法使用标志和规则模块来获取这两个细节。我制作了一个自定义模块,它实现了钩子以获取此信息。

我被困在这里:

现在我需要保存用户名和节点ID。我仍在决定是否要使用字段或数据库层。

一个用户名可以保存多个节点ID。

现在问题是我不确定有多少节点就足够了。这取决于用户。可能需要为一个用户保存500个甚至5000个节点ID。

那么如何为此做好准备?

所以我坚持逻辑。我应该如何使用db层或自定义内容类型中的字段来保存它?我应该怎么做?

请指教。我正在使用d7。

自定义模块代码

/*
* Implementation of the hook_rules_action_info() 
* 
*/
function customvishal_rules_action_info()
{
$actions = array(
'customvishal_action_userdetail' => array(
    'label' =>t('Custom function to send notifications'),
    'group'=>t('Cusotm Code for sending notifications'),
    'parameter'=> array(
     'account'=> array(
         'type'=>'user',
         'label'=>t('Going to get user list'),

     ),

     // for the node
     'productdetail'=> array(
         'type'=>'node',
         'label'=>t('Passding the node data'),

     ),   



    )
 ),



 ); 

 return $actions; 

 }


/*
* The action function for the rules exampled hello world
* 
*/
function customvishal_action_userdetail($account,$productdetail)
{

 drupal_set_message(t('This user #username! has flagged it', 
      array('#username' => $account->mail)));

 drupal_set_message(t('This node #nid has got updated', 
      array('#nid' => $productdetail->nid)));


 // The above takes care of the node and the user information later I will put 
 // it in a db or  something like  that.


 // end of the function customvishal_action_userdetail
 }

1 个答案:

答案 0 :(得分:0)

您似乎应该使用hook_node_update()hook_node_insert()来访问刚刚添加或更新的节点。

如果您希望在保存之前访问节点数据,那么hook_node_presave()就是要使用的节点数据。

我认为你不需要preave,因为你提到你需要节点ID,而presave还没有新节点的那个。

这是处理新节点和更新节点的方法。前两个函数只是挂钩到正确的位置并将节点路由到第三个函数。

<?php
// hook into node inserts
function customvishal_node_insert($node) {
  if ($node->type == 'mynodetype') {
    customvishal_handle_data($node);
  }
}

// hook into node updates
function customvishal_node_update($node) {
  if ($node->type == 'mynodetype') {
    customvishal_handle_data($node);
  }

}

// custom handler for the nodes
function customvishal_handle_data($node) {
  // load a user object of the node's author
  $author = user_load($node->uid);

  // now do what we need to do with $node and $user data

}

请记住,您需要清除Drupal缓存,以便模块中的新挂钩在D7中工作。

相关问题