<?php

/**
 * Returns an array of list node type (machine name) to allow manage share buttons.
 */
function _share_btn_content_type() {

  return array(
      'product_display',
      'product',
	  'diamond'
  );
}

/**
 * Implements of hook_entity_view($entity, $type, $view_mode, $langcode)
 */
function share_btn_entity_view($entity, $type, $view_mode, $langcode) {
  $extra_fields = field_info_extra_fields($type, 'product_display', 'display');
  $weight = isset($extra_fields['share_btn']['display']['default']['weight']) ? $extra_fields['share_btn']['display']['default']['weight'] : 10;
  if (isset($extra_fields['share_btn']['display']['default']['visible']) && $extra_fields['share_btn']['display']['default']['visible']) {
    $content_types = _share_btn_content_type();
    if (!empty($entity->type) && in_array($entity->type, $content_types) && $view_mode =='full') {
      $share_btn_display = theme('share_btn', array('entity' => $entity, 'entity_type' => $type));
      $entity->content['share_btn_field'] = array(
          '#markup' => $share_btn_display,
          '#weight' => $weight,
      );
    }
  }
}

/**
 * Implements hook_field_extra_fields().
 */
function share_btn_field_extra_fields() {
  $extra = array();
  $content_type = _share_btn_content_type();

  foreach ($content_type as $type) {

    $extra['node'][$type]['form']['share_btn'] = array(
        'label' => t('Share buttons'),
        'description' => t('Checkboxes for toggling share buttons'),
        'weight' => 10,
    );
    $extra['node'][$type]['display']['share_btn'] = array(
        'label' => t('Share buttons: links'),
        'description' => t('share buttons link'),
        'weight' => 10,
    );
  }
  return $extra;
}

/** Implements of hook_theme() */
function share_btn_theme($existing, $type, $theme, $path) {
  return array(
      'share_btn' => array(
          'variables' => array('entity' => NULL, 'entity_type' => NULL),
          'template' => 'share_btn',
      ),
  );
}
