You are here

function field_ipaddress_field_formatter_view in IP address fields 7

Implements hook_field_formatter_view().

File

./field_ipaddress.module, line 53

Code

function field_ipaddress_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'field_ipaddress_shorthand':
      $networks = array();
      foreach ($items as $delta => $item) {
        if (!empty($item)) {
          $networks[] = _field_ipaddress_long2shorthand($item['start'], $item['end']);
        }
      }
      if (!empty($networks)) {
        if (count($networks) === 1) {
          $element[0] = array(
            '#markup' => $networks[0],
          );
        }
        else {
          $element[0] = array(
            '#theme' => 'item_list',
            '#items' => $networks,
          );
        }
      }
      break;
    case 'field_ipaddress_cidr':
      $networks = array();
      foreach ($items as $delta => $item) {
        if (!empty($item)) {
          $networks[] = _field_ipaddress_long2cidr($item['start'], $item['end']);
        }
      }
      if (!empty($networks)) {
        if (count($networks) === 1) {
          $element[0] = array(
            '#markup' => $networks[0],
          );
        }
        else {
          $element[0] = array(
            '#theme' => 'item_list',
            '#items' => $networks,
          );
        }
      }
      break;
  }
  return $element;
}