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.
Comments
Stephen
Mon, 11/12/2012 - 03:46
Permalink
Implementation glitch
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
Stephen
Mon, 11/12/2012 - 03:47
Permalink
oops
Sorry, I mean I put your snippet into the module file not (which of course is in the module folder...
Stephen
Mon, 11/12/2012 - 04:28
Permalink
Fixed it
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!!
sami
Tue, 03/05/2013 - 12:50
Permalink
Use with any content types like users
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.
sami
Mon, 03/11/2013 - 01:36
Permalink
Modify other functions
allright, it's not that easy. You have to modify the other functions as well to reflect all entities. Just as a sidenote.
sami
Tue, 03/05/2013 - 13:14
Permalink
release as module on do?
What about releasing this module as an official module at drupal.org? It is tiny, indeed, but it has huge impact.
Nicola
Sun, 04/07/2013 - 03:37
Permalink
Fantastic!
I have just got this working, fantastic! Many thanks.
Add new comment