`
收藏列表
标题 标签 来源
drupal7创建node 编程创建节点 Drupal 7 通过编程创建节点(node)、留言(comment)、分类/标签(taxonomy)
http://www.drupalla.com/node/811
https://drupal.org/node/1388922
Drupal 7 load node comments render entity view mode
$cids = comment_get_thread($entity, COMMENT_MODE_FLAT, 100);
if (is_array($cids)) {
  $entities = comment_load_multiple($cids);
}

if (isset($entities)) {
  $view = entity_view('comment', $entities, 'full');
}
print drupal_render($view);
hook_user 保存用户登录次数 http://www.majormoves.net/post/833/
我们创建一个名为“loginhistory”的模块,用来保存用户登录的历史记录。我们在用户的“我的帐号”页面显示用户已经登录的次数。
在sites/all/modules/custom/下面创建一个名为loginhistory的文件夹,并添加以下文件。
loginhistory.info
loginhistory.module
loginhistory.install

列表 6-3. loginhistory.info

name = Login History
description = Keeps track of user logins.
package = Pro Drupal Development
version = VERSION
core = 6.x

; Information added by drupal.org packaging script on 2011-05-25
version = "6.22"
project = "drupal"
datestamp = "1306357015"


为了存储登录信息,我们需要使用一个.install文件来创建数据库表,所以我们创建了sites/all/modules/custom/loginhistory.install文件。
该文件自动创建一个名为login_history的表,包含uid和login两个字段。

<?php
// $Id$  
/**  * Implementation of hook_install().  */
function loginhistory_install() {    
  // Create tables.    
  drupal_install_schema('loginhistory');
}  

/**  * Implementation of hook_uninstall().  */
function loginhistory_uninstall() {    
  // Remove tables.    
  drupal_uninstall_schema('loginhistory');
}
    
/**  * Implementation of hook_schema().  */
function loginhistory_schema() {    
$schema['login_history'] = array(      
  'description' => t('Stores information about user logins.'),      
    'fields' => array(          
    'uid' => array(                
      'type' => 'int',                
      'unsigned' => TRUE,                
      'not null' => TRUE,                
      'description' => t('The {user}.uid of the user logging in.'),            
    ),            
    'login' => array(                
      'type' => 'int',                
      'unsigned' => TRUE,                
      'not null' => TRUE,                
      'description' => t('Unix timestamp denoting time of login.'),            
    ),        
  ),        
  'index' => array('uid'),    
);          
return $schema;
}

创建sites/all/modules/custom/loginhistory.module文件,当我们登录的时候将记录登录次数到login_history表,当我们查看用户信息的时候,将显示用户登录的次数。

<?php
// $Id$  
/**  
* @file  
* Keeps track of user logins.
*/  
/**  
* Implementation of hook_user().  
*/
function loginhistory_user($op, &$edit, &$account, $category = NULL) {    
switch($op) {        
  // Successful login.        
case 'login':            
// Record timestamp in database.            
  db_query("INSERT INTO {login_history} (uid, login) VALUES (%d, %d)",$account->uid, $account->login);            
  break;            
// $user object has been created and is given to us as $account parameter.        
case 'load':            
  // Add the number of times user has logged in.          
   $account->loginhistory_count = db_result(db_query("SELECT COUNT(login) AS count FROM {login_history} WHERE uid = %d", $account->uid));
   break;            
// 'My account' page is being created.        
case 'view':            
  // Add a field displaying number of logins.            
  $account->content['summary']['login_history'] = array(                
  '#type' => 'user_profile_item',                
  '#title' => t('Number of Logins'),                
  '#value' => $account->loginhistory_count,                
  '#attributes' => array('class' => 'login-history'),                
  '#weight' => 10,            
  );            
  break;
  }
}


在安装了这个模块以后,对于每次成功的用户登录,都将调用user钩子的login操作,在这个钩子里面,模块将向数据库表login_history插入一条记录。在加载$user对象时,将会调用用户加载钩子,此时模块将把用户的当前登录次数添加$user->loginhistory_count中。当用户查看“我的帐号”页面时,登录次数将显示出来,如图6-5所示。
点击在新窗口中浏览此图片
注意 当你在你的模块中为对象$user或$node添加属性时,在属性名前面最好加上前缀,以避免命名空间的冲突。这就是为什么这里使用$account->loginhistory_count来代替$account->count的原因。 
Global site tag (gtag.js) - Google Analytics