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.

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

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

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.