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 Tran 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
  • Phone/Zalo: (+84) 944 225 212
  • WhatsApp: (+84) 944 225 212
  • Line Messenger: (+84) 944 225 212
  • Email: asaleotestf@gmail.com
  • Telegram Messenger: https:/t.me/tommytran0401

Quảng cáo việc làm

 

Thích hợp các bạn nữ mảng thợ may làm việc tại nước NGA

Đơn hàng Tuyển dụng 100 Thợ may đi Nga(đợt 1 tháng 3.2021, đợt 2 tháng 5.2021). Lương thực lãnh 800 USD, bao ăn ở, vé máy bay và visa, phí xuất cảnh(1800 USD)trả khi đi làm có lương. Bạn có thể liên hệ CÔNG TY qua Phone/Zalo: (+84) 944 225 212. Công ty sẽ tư vấn cho bạn.

Xem chi tiết: >>> https://bit.ly/3o9NOfR

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

 
Chat to MSN, Facebook, AIM, GTalk,Yahoo Friends with eBuddy Messenger for Androi

Chat to AIM, GTalk,Yahoo Friends with eBuddy Messenger

If you absolutely love instant messaging on your Android then eBuddy messenger is the app that you must have on your phone.

Đến Từ NAG Ryan McKinnon

[Share Preset] Tone Màu Film Cho Ảnh Sexy Đến Từ NAG Ryan McKinnon

Bao gồm 02 preset với tone màu đậm, đặc biệt phù hợp với ảnh nude, sexy….theo tác giả, đây không phải là preset “bấm phát ăn ngay”

Phần 2 - view, controller và router trong Symfony 2

CRUD sẽ làm cho mọi thừ đơn giản, hãy dùng nó để bắt đầu việc ... đọc hiểu. Chạy các lệnh sau:

BLOG POSTS