Cài đặt hook_flag_access for controlling what the user can flag Drupal

Cài đặt hook_flag_access for controlling what the user can flag Drupal

Recently, a client wanted their site users to be able to show their support for certain content nodes in Drupal. This can be done very nicely with the flag module. In case you missed it, flag module is a very useful module that has many applications - such as flag as offensive, Cài đặt hook_flag_access for controlling what the user can flag Drupallike, friend, etc.

So it was very natural for us to use the flag module to let the users flag a node if they want to support it. But the client requirement was that a user should not be able to support more than one node at a time. That means we have to implement an upper limit of 1 on the number of nodes a user can support. This feature does not come with the flag module. But the 2.x version of this module does provide a special hook called hook_flag_access that can help implement this feature in your own module.

/**
 * Implementation of hook_flag_access()
 */
function MYMODULE_flag_access($flag, $content_id, $action, $account) {
  if($flag->name == 'membersupport' && $action == 'flag') {
    if($flag->get_user_count($GLOBALS['user']->uid) > 0) {
      return FALSE;
    }
  }
}

In the code above, when the flag being flagged is a certain one ('membersupport') and the action is 'flag', we check the number of nodes this user has already flagged with the same flag. If that number is greater than zero, then they are not allowed to flag any more.

 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Xuất hiện máy tính bảng chạy Android 4 giá dưới 2,1 triệu đồng

Xuất hiện máy tính bảng chạy Android 4 giá dưới 2,1 triệu đồng

MIPS Technologies hợp tác cùng hãng sản xuất chip Ingenic vừa giới thiệu chiếc máy tính bảng giá rẻ kèm theo cấu hình tương đối "khủng" sánh vai cùng hệ điều hành Android 4.0 Ice Cream Sandwich.

Giới thiệu 8 Web về Design and Development Training Sites

Giới thiệu 8 Web về Design and Development Training Sites

In a world where twelve-year-olds are able to create an intricate social networking framework, many businesses have transitioned to nearly full digital marketplace capabilities

Chia sẻ kinh nghiệm thi Acquia Drupal Developer Certification Exam

Chia sẻ kinh nghiệm thi Acquia Drupal Developer Certification Exam

According to a few people who have already taken the Acquia certification exam, it tested real-world Drupal knowlege rather than rote memorization

Công ty diệt chuột T&C

 

Diet con trung