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

 
CEO Sun* Taihei Kobayashi ở Tokyo

CEO Sun* Taihei Kobayashi - Từ vô gia cư thành CEO công ty tỷ USD

Taihei Kobayashi từng lang thang chơi đàn trên các con phố ở Tokyo trước khi thành lập hãng công nghệ có vốn hóa hơn 1 tỷ USD.

Fast, Powerful & Elegant Web Browser for Android – Dolphin Browser HD

Fast, Powerful & Elegant Web Browser for Android – Dolphin Browser HD

Dolphin Browser HD was definitely the browsing app that pioneered other browsers on the Android platform.

Command line khởi động MySQ L trên Red Hat Linux

Command line khởi động MySQ L trên Red Hat Linux

After you've installed MySQL it's often a good and convenient idea to make it start and stop automatically with the machine. These instructions are specific for Red Hat Linux and it's variants since different Linux distributions tend to layout the start up scripts differently.

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

 

Diet con trung