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.

<?php
/**
* 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([email protected]_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([email protected]_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

<?php
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

Pretty good post. I found your website perfect for my needs. Thanks for sharing the great ideas.
<a href="http://myblogfit.blogspot.com/2014/03/abcbola-agen-judi-bola-piala-dunia-2014.html">ABCbola.com Agen Judi Bola Terpercaya Piala Dunia 2014</a>
<a href="http://master-cyberseo.blogspot.com/2014/03/abcbola-agen-judi-bola-terpercaya-piala-dunia.html">ABCbola.com Agen Judi Bola Terpercaya Piala Dunia 2014</a>
<a href="http://myblogfit.blogspot.com/2014/03/afatogel-situs-bandar-togel-online-terbesar.html">Afatogel.com Bandar Terbesar Situs Togel Online Terpercaya</a>
<a href="http://goo.gl/3Wj3RW">batikpoke.com judi poer online uang asli indonesia</a>
<a href="http://goo.gl/QMd7Xj">CMCpokr.com Agen Judi Pker Online , Agen Judi Domino Online Indonesia Terpercaya</a>
<a href="http://pusat-cara.blogspot.com/2014/05/itubola-piala-dunia.html">http://pusat-cara.blogspot.com/2014/05/itubola-piala-dunia.html</a>

Cheapest Bali Car Hire
Cheap Bali Car Rental
Cheap Bali Car Hire
Bali Car Rental
Bali Car Hire

http://bermainfo.blogspot.com/2014/05/abcbolacom-agen-judi-bola-terpercaya.html

ituBola.com JUDI BOLA, AGEN BOLA, AGEN JUDI BOLA PIALA DUNIA
http://goo.gl/hlYF60

ABCBOLA.COM Agen Judi Bola Terpercaya Piala Dunia 2014
http://goo.gl/Odi388
http://goo.gl/C59qoj

http://junaysukses.blogspot.com/2014/12/77dewa-com.html

http://situsbacaonline.blogspot.com/2014/11/judi-bola-online-itubolanet.html

http://myblogfit.blogspot.com/2014/10/bola-pelangi-agen-bola-sbobet-ibcbet.html

http://disallowsites.blogspot.com/

http://candilaya.blogspot.com/

http://seoallsite.blogspot.com/

http://oketopseo.esy.es/

http://goo.gl/55h1Wi
http://goo.gl/aozPO2
http://goo.gl/B5V2Q1

http://goo.gl/EwaunU
http://goo.gl/c4azUf
http://goo.gl/Or8YVB
http://goo.gl/eG9u6c
http://goo.gl/GrpjE1
http://goo.gl/WxbQy4

Kemungkinan berita bola terbaik jatuh pada situs taruhan bola moderen <a href="http://agenbolaliga.info/">Agen Bola Terpercaya</a>

Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - which is living with the results of other people's thinking. Don't let the noise of others' opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition.
<a href="http://www.willischappellbujan.com/contact.html">Willis Custom Homes</a>

http://indoogle.net/indonalo-net-agen-judi-togel-online-nasional-indonesia/
http://indoogle.net/togel-online-asli4d-net-terpercaya-number-game-3d-indonesia/
http://blog.indoogle.net/2015/09/qq288-com-situs-bandar-taruhan-judi-online-terbesar.xhtml

SUDAH BOSEN DENGAN GAME ONLINE ITU-ITU SAJA DAN MASIH BELUM MENDAPATKAN GAME ONLINE BARU LAGI??

JIKA BELUM MENDAPATKAN GAME BARU YANG ANDA CARI MARI BERGABUNG SEKARANG JUGA BERSAMA INDOMONOPOLY GAME SATU-SATU NYA TERBARU YANG MENGGUNAKAN UANG ASLI INDONESIA..

HANYA REGISTER DAN LIVECHAT ANDA SUDAH BISA MENDAPATKAN FREE BERLIAN SEBESAR Rp.50.000/idr

DAN ANDA SUDAH BISAMENIKMATI SUASANA GAME YANG BERBEDA DARI GAME LAIN NYA YANG PERNAH ANDA MAIN KAN..

AYOO JADILAH MILYONER PERTAMA DI INDOMONOPOLY SEKARANG JUGA..

UNTUK KETERANGAN LEBIH LANJUT SILAHKAN HUBUNGI TEAM LIVECHAT KAMI RAMAH SIAP MELAYANI CHAT ANDA 24JAM..

DAN DI BACA ARTICEL-ARTICEL MENARIK NYA YA:

http://idrmonopolyindo.blogspot.com

fans page facebook https://goo.gl/vHund8

line official http://bit.ly/1PyPyKP

BBM Pin : 5649B320

SUDAH BOSEN DENGAN GAME ONLINE ITU-ITU SAJA DAN MASIH BELUM MENDAPATKAN GAME ONLINE BARU LAGI??

JIKA BELUM MENDAPATKAN GAME BARU YANG ANDA CARI MARI BERGABUNG SEKARANG JUGA BERSAMA INDOMONOPOLY GAME SATU-SATU NYA TERBARU YANG MENGGUNAKAN UANG ASLI INDONESIA..

HANYA MULAI MELAKUKAN DEPOSIT SEBESAR Rp.20.000 DAN WITHDRAW 50.000 ANDA SUDAH BISA MENIKMATI SUASANA GAME YANG BERBEDA DARI GAME LAIN NYA YANG PERNAH ANDA MAIN KAN..

DAN TENTUNYA ADA JUGA KOMISI REFERRAL YANG DI BERIKAN INDOMONPOLY.COM SEBESAR 50%

AYOO JADILAH JUTAWAN PERTAMA DI INDOMONOPOLY SEKARANG JUGA..

UNTUK KETERANGAN LEBIH LANJUT SILAHKAN HUBUNGI TEAM LIVECHAT KAMI RAMAH SIAP MELAYANI CHAT ANDA 24JAM..

DAN DI BACA ARTICEL-ARTICEL MENARIK NYA YA:

http://idrmonopolyindo.blogspot.com

fans page facebook https://goo.gl/vHund8

line official http://bit.ly/1PyPyKP

BBM Pin : 5649B320

Line : hermilyrostan

YANG SUKA MAIN GAME ONLINE SEKARANG JUGA BERGABUNG DI POKER BETDANWIN.
KARENA POKER BETDANWIN LAGI MEMBERIKAN PROMO UNTUK PARA MEMBER SETIA
* PROMO BONUS 20 % DARI NILAI DEPOSIT ANDA
* PROMO REFERRAL SAMPAI 50 % SEUMUR HIDUP DIBAGI SETIAP SENIN
* MINIMAL DEPOSIT Rp. 10.000
* MINIMAL WITHDRAW Rp. 20.000
JADI TUNGGU APALAGI SEGERA DAFTARKAN ID ANDA HANYA DI POKER BETDANWIN DAN MENANGKAN
CHIPS SEBANYAK BANYAKNYA.BET NOW AND WIN NOW.
JANGAN LUPA KUNJUNGI JUGA WEBSITE KAMI DI WWW.BETDANWIN.COM ATAU HUBUNGI LIVECHAT KAMI
YANG RAMAH DAN SIAP MEMBANTU ANDA 24 JAM SETIAP HARI.

HATI-HATI PERHATIKAN GAME-GAME DI SEKITAR ANDA YANG BISA BERBAHAYA DAN HANYA MENJANJIKAN TAPI TIDAK PASTI

JIKA INGIN MENDAPATKAN GAME YANG TERBAIK,TERPERCAYA DAN PASTI MENJANJIKAN

BURUAN SEKARANG JUGA YUK BERGABUNG BERSAMA KAMI DI INDOMONOPOLY.COM

INDOMONOPOLY.COM ADALAH GAME ONLINE INDONESIA YANG MENGGUNAKAN UANG ASLI TERPERCAYA DAN TERPOPOLER SAAT INI

DAN APABILA ANDA SUDAH BERMAIN DI INDOMONOPOLY MAKA KAMI YAKIN ANDA PASTI MERASA KETAGIHAN KARENA BANYAK HADIAH-HADIAH YANG DI BERIKAN

-DEPOSIT 20.000

-WITHDRAW 50.000

-REFERRAL 50% + ACCESSORIES 7HARI

-CASHBACK 10%

UNTUK KETERANGAN LEBIH LANJUT SILAHKAN HUBUNGI TEAM LIVECHAT KAMI YANG RAMAH UNTUK MELAYANI ANDA

website http://goo.gl/SjhVsd

fans page facebook https://goo.gl/vHund8

line official http://bit.ly/1PyPyKP

BBM Pin : 5649B320

Line : hermilyrostan

http://idrmonopolyindo.blogspot.com

Add new comment

Plain text

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