Snippet Snack: Create Display Suite fields for Flag & Flag Count

I've decided to make a new home for the small snippets that I post periodically: Snippet Snacks. This will be small repository for useful Drupal snippets.

Here's the first one—a snippet that forms a bridge between Display Suite and Flag.

When added to a custom module, this snippet will provide Display Suite 'Flag this' and 'Flag Count' fields for each applicable Flag type.

/**
 * Implements hook_ds_fields_info().
 */
function yourmodule_ds_fields_info($entity_type) { 
  $fields = array();
 
  if ($flags = flag_get_flags($entity_type)) {
    // Build flag options.
    foreach ($flags as $name => $flag) {
      $fields['flag_' . $name] = array(
        'title' => t('Flag - @flag_title', array('@flag_title' => $flag->title)),
        'field_type' => DS_FIELD_TYPE_FUNCTION,
        'function' => 'yourmodule_ds_field_flag',
        'properties' => array('flag' => $flag),
      );
      $fields['flag_count_' . $name] = array(
        'title' => t('Flag Count - @flag_title', array('@flag_title' => $flag->flag_short)),
        'field_type' => DS_FIELD_TYPE_FUNCTION,
        'function' => 'yourmodule_ds_field_flag_count',
        'properties' => array(
          'flag' => $flag,
        ),
      );
    }
  }
 
  return array('node' => $fields);
}
/**
 * Generate flag link for Display Suite.
 */
function yourmodule_ds_field_flag($field) {
  $entity = $field['entity'];
  $flag = $field['properties']['flag'];
  return flag_create_link($flag->name, $entity->nid);
}
 
/**
 * Generate flag count for Display Suite.
 */
function yourmodule_ds_field_flag_count($field) {
  $entity = $field['entity'];
  $flag = $field['properties']['flag'];
 
  // We are electing not to use flag_get_counts() because it is not flag specific.
  $query = db_select('flag_counts' ,'fc');
  $query->fields('fc', array('count'));
  $query->condition('fc.fid', $flag->fid);
  $query->condition('fc.content_type', 'node');
  $query->condition('fc.content_id', $entity->nid);
  $count = $query->execute()->fetchColumn();
 
  return !empty($count) ? $count : 0;
}

You might ask "Why wouldn't I just drop  flag_create_link('myflag', $entity->nid); into a code field?" I don't like putting PHP into textfields if it can be avoided. Apart from being a security risk, you can't control it with SCM.

Drupal Version Compatibility: 

Comments

Hi there, I created a module folder, an info file and module file. I put your snippet into the module folder. I named the folder and files after my module name and replaced the "yourmodule" in your snippet with my module name.

The module can be turned on under admin. Under a content type with DS active I can now find the flags for bookmark and the custom flags I have made in Flag module! Hooray!

However even though I can place them in the display under content type > manage display... The flag links don't appear on the front end...

What am I doing wrong? Do I need more that the info file and and your snippet to make the module work.

I am using
flag - 7.x-2.0
display suite - 7.x-1.6

Sorry, I mean I put your snippet into the module file not (which of course is in the module folder...

Ok, it's as simple as creating the module as in my first comment. The problem was that I forgot to go to my flags and make sure they were turned on for the content type I was working with.

Thanks, this is a really great snippet!!

Thanks a lot for sharing this snippet, it helped a lot.
One tiny improvement: If you store $flag->content_type in a variable and use it as key in the return value in yourmodule_ds_fields_info() you can use this snippet for any content type.

allright, it's not that easy. You have to modify the other functions as well to reflect all entities. Just as a sidenote.

<a href="http://www.cgomedia.com/">SEO Agency</a>
Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks

Wonderful article. It is useful for me, thanks a lot for giving everyone an exceptionally splendid possiblity to read in detail from this blog. I really enjoy reading in your blog! SEO Liverpool

What about releasing this module as an official module at drupal.org? It is tiny, indeed, but it has huge impact.

Thanks for giving us opportunity to post in your blog. This gonna be fun lots of people will comment and provides information that make this page so interesting. Thanks for giving us opportunity to post in your blog.Web Design Manchester

I have just got this working, fantastic! Many thanks.

Received this when saved the display with the Count field.

Notice: Undefined index: type in field_ui_display_overview_form_submit() (line 1360 of mysite/modules/field_ui/field_ui.admin.inc).

Received this when went to homepage with content:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fc.content_type' in 'where clause': SELECT fc.count AS count FROM {flag_counts} fc WHERE (fc.fid = :db_condition_placeholder_0) AND (fc.content_type = :db_condition_placeholder_1) AND (fc.content_id = :db_condition_placeholder_2) ; Array ( [:db_condition_placeholder_0] => 3 [:db_condition_placeholder_1] => node [:db_condition_placeholder_2] => 123 ) in mymodule_ds_field_flag_count() (line 111 of mymodule.module).

111 is second to last line of your code:
$count = $query->execute()->fetchColumn();

I have been waiting for someone to share this post. This has actually made me think and I hope to read more. Thanks a lot for sharing with us.SEO Manchester

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.