Hướng dẫn flag , unflag trong Drupal 7

Hướng dẫn flag , unflag trong Drupal 7

Hướng dẫn flag , unflag trong Drupal 7

Drupal's Flag module comes in handy in various situations. It does however have one major drawback for some use cases - there is no way to clear all flags of a certain content item. Imagine the following use case for example - users are allowed to mark certain comments on the website as abusive or not fit for the content. Certain users are allowed to review these flagged comments and decide if they are indeed "bad" and should be deleted or someone just accidentally marked a comment that is perfectly safe and normal for the site.

The easiest way to manage these comments is on a page created by a view. Flag provides necessary relationships, arguments, fields and filters to help out with pretty much everything you would need on such page but a way to clear all flags for a specific comment.

I'm guessing everyone building a Drupal site has at least one custom module where all overrides and form_alters go. In this module add a new menu callback like so:

/**
 * Implementation of hook_menu().
 */
function mymodule_menu() {
  $items = array();
  $items['admin/content/comment/unflag/%/%'] = array(
    'title' => 'Flag unflag callback',
    'page arguments' => array(4,5),
    'page callback' => 'mymodule_unflag_comment',
    'access arguments' => array('administer comments'),
    'description' => 'Remove all flags from a content item',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

This callback will accept both the flag name and comment ID as arguments - you could easily get away with just comment ID but if you have a need for more generic functionality (eg. you might want to do this to more than one flag) keep it as it is.

Next create the following menu callback function in your module (see note above about arguments and adjust accordingly):

/** Menu callback to allow unflagging a specific comment of all flags */
function mymodule_unflag_comment($flag_id, $id) {
  // Get the flag object
  $flag = flag_get_flag($flag_id);
  // Delete flags on comment
  $count = flag_reset_flag($flag, $id);    
  // Reset counters for this comment
  db_query('DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d', $flag->fid, $id);
  $url = 'admin/content/comment';
  drupal_goto($url);
}

The last step is to alter the view that shows flagged comments. Add a new field based on comment ID, check "Rewrite the output of this field" and change field value to <a href="/admin/content/comment/unflag/flag_name/[cid]">Remove reporting</a>. Make sure to replace "flag_name" with the actual name of the flag you are trying to remove if you want a more generic approach. If your menu callback only uses comment ID, skip this part altogether.

Note on security: in this example I am assuming that you trust the users you grant "administer comments" permission.

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

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

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

 
Google Mua Thêm 217 Bằng Sáng Chế Từ IBM

Google Mua Thêm 217 Bằng Sáng Chế Từ IBM

Theo thông tin có được từ blog SEO by the Sea mới đây thì Google vừa hoàn thành thương vụ mua lại 217 bằng sáng chế từ IBM với....

Book: Front End Drupal, Designing, Theming and Scripting

Download sách Drupal: Theming and Scripting, Designing

Drupal faces a common problem on the Web–the relative lack of new, high quality themes. Front End Drupal tackles this problem directly and is designed to help both experienced designers and rank novices get an understanding of how Drupal theming works. In fact, I’ll be the first to admit I learned a lot from this book.

So sánh Social Business Platforms: Part 3 - Drupal Commons

So sánh Social Business Platforms: Part 3 - Drupal Commons

In our first two posts we reviewed two of the most popular platforms for social business portals, Jive and Sharepoint.

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

 

Diet con trung