Browse Source

added share field formatter

Bachir Soussi Chiadmi 9 years ago
parent
commit
7b71ab2e1b

+ 4 - 3
sites/all/modules/features/chapitre/chapitre.features.field_instance.inc

@@ -499,15 +499,16 @@ function chapitre_field_default_field_instances() {
     'display' => array(
       'chapter' => array(
         'label' => 'above',
-        'module' => 'text',
+        'module' => 'jeemod',
         'settings' => array(),
-        'type' => 'text_default',
+        'type' => 'share_butons',
         'weight' => 4,
       ),
       'default' => array(
         'label' => 'above',
+        'module' => 'jeemod',
         'settings' => array(),
-        'type' => 'hidden',
+        'type' => 'share_butons',
         'weight' => 4,
       ),
       'teaser' => array(

+ 1 - 0
sites/all/modules/features/chapitre/chapitre.info

@@ -6,6 +6,7 @@ dependencies[] = features
 dependencies[] = field_collection
 dependencies[] = field_group
 dependencies[] = image
+dependencies[] = jeemod
 dependencies[] = node
 dependencies[] = strongarm
 dependencies[] = text

+ 78 - 2
sites/all/modules/gui/jeemod/jeemod.module

@@ -41,8 +41,6 @@ function jeemod_entity_info_alter(&$entity_info) {
 }
 
 
-
-
 /**
  * Implements hook_video_embed_field_handlers_alter().
  */
@@ -106,3 +104,81 @@ function jeemod_handle_vimeo($url, $settings) {
                  '?' . $settings_str . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>',
   );
 }
+
+
+/**
+ * Implements hook_field_formatter_info().
+ */
+function jeemod_field_formatter_info() {
+  return array(
+    'share_butons' => array(
+      'label' => t('Share butons (Twitter, Facebook)'),
+      'field types' => array('text'),
+    ),
+  );
+}
+
+/**
+ * Implements hook_field_formatter_view().
+ */
+function jeemod_field_formatter_view($entity_type, $entity, $field, $instance, $lang_code, $items, $display) {
+ $element = array();
+  foreach ($items as $delta => $item) {
+    $element[$delta] = array(
+      '#theme' => 'jeemod_share_links',
+      '#entity' => $entity,
+      // '#url' => $entity->field_partager['und'][0]['url'],
+    );
+ }
+  return $element;
+}
+
+/**
+* Implements hook_theme().
+*/
+function jeemod_theme() {
+  return array(
+    'jeemod_share_links' => array(
+      'template' => 'jeemod_share_links',
+      'variables' => array(
+        'entity' => NULL,
+        // 'url' => NULL,
+      ),
+    ),
+  );
+}
+
+function jeemod_preprocess_jeemod_share_links(&$vars){
+  global $base_url;
+  // dsm($vars);
+  $entity = $vars['entity'];
+
+  $sitetitle = variable_get('site_name', '');
+  $nodetitle = $entity->title;
+  $nodeurl = $base_url . base_path() . "#principe-" . $entity->nid;
+
+  $text = $nodetitle ." | ". $sitetitle . " - " . $nodeurl;
+
+  $tags = explode(' ', str_replace('#', '', $entity->field_partager['und'][0]['value']));
+
+  $vars['twitter_url'] = url("https://twitter.com/intent/tweet", array(
+    'absolute'=>true,
+    'query'=>array(
+      // "button_hashtag"=>implode(',',$tags),
+      "button_hashtag"=>$tags[0],
+      "text"=>$text,
+    )
+  ));
+  $vars['twitter_title'] = "Twitter $nodetitle";
+
+  $vars['fb_url'] = url("https://www.facebook.com/sharer.php", array(
+    'absolute'=>true,
+    'query'=>array(
+      "u"=>$nodeurl,
+      "t"=>$text,
+    )
+  ));
+  $vars['fb_title'] = "Partager $nodetitle";
+
+
+}

+ 7 - 0
sites/all/modules/gui/jeemod/jeemod_share_links.tpl.php

@@ -0,0 +1,7 @@
+<a class="twitter-buttuon" href="<?php print $twitter_url; ?>" target="_blank">
+  <?php print $twitter_title; ?>
+</a>
+<br/>
+<a class="fb-button" href="<?php print $fb_url; ?>" target="_blank">
+  <?php print $fb_title; ?>
+</a>