How can I check if specific URL in my site is flagged?

CommentFileSizeAuthor
#4 flag_unflag_conf.png5.03 KBitsnotme

Comments

alexpott’s picture

Assigned: Unassigned » alexpott
Status: Active » Fixed

To find out if a url is flagged in code you do something like this... (it's using the methods provided by the flag object)

  // Set $flag_name to whatever page flag you want to check
  // Create a flag object
  $flag = flag_get_flag($flag_name);
  //Check if user has access to flag
  if ($flag->user_access()) {
    $url = (isset($_GET['q'])) ? $_GET['q'] : variable_get('site_frontpage', 'node');
    $content_id = $flag->get_content_id($url);
    if ($flag->is_flagged($content_id)) {
      // Do something if the page is flagged
    }
  }

Depending on what you are doing you may or may not need the user access check.

Igal’s picture

alexpott, thanks for the reply. It is works.

I noticed that if I unflag the page, the entry of unflagged page will not be deleted from flag_page_data_url table. Is this correct behavior?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

itsnotme’s picture

StatusFileSize
new5.03 KB

It looks like this to me:

Flagging without confirming
* Page gets flagged: one entry in table flag_page_data (makes content_ID for url), one in flag_content (which ties fcid - flag type - content ID - the user).
* Page gets unflagged: Entry is removed from flag_content but not from flag_page_data.

Flagging with confirming
* Page gets flagged -> two entries into table flag_page_data and one in flag_content.
* Page gets unflagged -> an additional third entry is made in flag_page_data, and the entry in flag_content is removed.

The screenshot for flag_page_data is attached; MMS was bookmarked without confirmation, Playground was bookmarked and unbookmarked with confirmation. These additional two entries don't make much sense to me, admittedly. An explanation would be welcome :)

alexpott’s picture

Hi itsnotme,

If you use the version that is compatible with Flag 2.x the extra entries will not be put into the flag_page_data table. It has a way of lazily loading this table.

In the first version of flag page every page of the site will end up in flag_page_data whether it is flagged on not. Pages which are flagged end up in the flag_content table.

Also if you have further questions can you open a new issue as this issue has already been solved and closed. Thanks!