You are here

function shariff_field_formatter_view in Shariff Social Media Buttons 7

Implements hook_field_formatter_view().

File

./shariff.module, line 523
Integrating Shariff library, providing settings form and block.

Code

function shariff_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'shariff_formatter':
      $settings = _shariff_get_settings();
      if (!empty($settings['services'])) {
        foreach ($items as $delta => $item) {

          // Only display buttons if the field has the right value or it should be displayed by default.
          if ($item['value'] === NULL && !$display['settings']['hide_by_default'] || $item['value'] == '1') {
            $uri = entity_uri($entity_type, $entity);
            $uri['options'] += array(
              'absolute' => TRUE,
            );
            $settings['url'] = url($uri['path'], $uri['options']);

            // Prepare settings variables to be used in the render array.
            $render_vars = array();
            foreach ($settings as $key => $setting) {
              $render_vars['#' . $key] = $setting;
            }
            $element[$delta]['#theme'] = 'shariff';
            $element[$delta] += $render_vars;
          }
        }
      }
      break;
    default:
      break;
  }
  return $element;
}