You are here

function boolean_field_formatter_view in Boolean Field 7

Implements hook_field_formatter_view().

File

./boolean.module, line 241
Defines boolean field type.

Code

function boolean_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  // Allow for a site using the cardinality patch.
  // @see https://www.drupal.org/files/issues/1029298-60-move-cardinality-setting.patch
  $cardinality = isset($instance['cardinality']) ? $instance['cardinality'] : $field['cardinality'];
  $labels = explode("\n", $instance['settings']['labels']);
  switch ($display['type']) {
    case 'number_boolean':
      $display_delta = count($items) > 1;
      foreach ($items as $delta => $item) {
        if ($settings['omit_unchecked'] && $item['value'] != 1) {
          continue;
        }
        $display_label = '';
        if ($cardinality == FIELD_CARDINALITY_UNLIMITED) {
          $display_label = $delta;
        }
        elseif ($settings['labels'] && !empty($labels[$delta])) {
          $display_label = $labels[$delta];
        }
        elseif ($cardinality > 1) {
          $display_label = $delta;
        }
        $prefix = $suffix = '';
        $states = boolean_value_info();
        $strings = $instance['settings'][$states[$item['value']]];
        $class = 'state-' . $states[$item['value']];

        //         if (TRUE || $item['value']) { // @todo Do we need a check here?
        if ($settings['prefix_suffix']) {
          $prefix = isset($strings['prefix']) ? $strings['prefix'] . ' ' : 'Instance ';
          $suffix = isset($strings['suffix']) ? ' ' . $strings['suffix'] : '';
        }
        elseif (!$settings['labels']) {
          $prefix = 'Instance ';
          $suffix = '';
        }
        $output = '<span class="' . $class . '">';
        $output .= $prefix . $display_label . $suffix;
        $output .= '</span>';
        $element[$delta] = array(
          '#markup' => $output,
          '#attributes' => array(
            'class' => 'state-' . $states[$item['value']],
          ),
        );

        //         }
      }
      break;
  }
  return $element;
}