You are here

function socialfield_field_formatter_view in Social field 7

Implements hook_field_formatter_view().

File

./socialfield.module, line 907
Provides a field for adding social services links.

Code

function socialfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if (!$items) {
    return;
  }
  $list_items = array();
  switch ($display['type']) {
    case 'socialfield_formatter':
      uasort($items, 'drupal_sort_weight');
      $services = variable_get('socialfield_services');
      foreach ($items as $item) {

        // Checking if the service was deleted.
        if (!isset($services[$item['service']])) {
          continue;
        }

        // Social link.
        $hidden_text = '';
        if (isset($display['settings']['link_text']) && $display['settings']['link_text']) {

          // Wanting to set hidden text on link.
          $hidden_text = '<span class="element-invisible">' . check_plain($item['service']) . '</span>';
        }
        $link = l('<i class="icon ' . $services[$item['service']]['icon'] . '">' . $hidden_text . '</i>', $item['url'], array(
          'html' => TRUE,
          'attributes' => array(
            'target' => '_blank',
          ),
        ));
        $list_items[] = array(
          'data' => $link,
          'class' => array(
            'field-item',
            'service-' . $item['service'],
          ),
        );
      }

      // Checking if there are links to display.
      if (!$list_items) {
        return;
      }

      // Make list from items.
      $element[0] = array(
        '#theme' => 'socialfield_formatter',
        '#items' => $list_items,
        '#field' => $field,
      );
      $element['#attached']['css'][] = drupal_get_path('module', 'socialfield') . '/css/socialfield.css';
      break;
  }
  return $element;
}