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

 
Phân tích của một chiến dịch SEO audit bao gồm 5 yếu tố

Phân tích của một chiến dịch SEO audit bao gồm 5 yếu tố

Với tất cả những finding mình khuyến cáo các bạn nên copy đường link của trang đó vào một file excel

SEO và PPC

Nghiên cứu của Google về mối quan hệ giữa SEO và PPC

Một nghiên cứu thú vị của Google về  mối quan hệ giữa kết quả tìm kiếm tự nhiên (Organic Search, ám chỉ kết quả từ phương thức SEO) và quảng cáo trả phí (Paid/PPC, cụ thể là Adwords) trên Google Search: ngay cả khi đứng đầu trên bảng xếp hạng tự nhiên thì bạn vẫn có thể tăng gấp đôi lượng click bằng quảng cáo trả phí kênh tìm kiếm.

Resizing Fields in Drupal 8 Without Losing Data

Hướng dẫn Resizing Fields trong Drupal 8 để không bị Losing Data

Drupal's field system is awesome and it is one of the reasons why I started using Drupal in the first place. However, there are some small limitations in it which surface from time to time.

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

 

Diet con trung