You are here

function mvf_field_formatter_view in Measured Value Field 7

Implements hook_field_formatter_view().

File

./mvf.module, line 920
Define a field type of measured value.

Code

function mvf_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // In order to successfully mock up instance for a sub field, we have to know
  // view mode that corresponds to the supplied $display. We could compare each
  // display in $instance trying to find the one that equals to $display.
  // It seems easier just to extend $instance['display'] with $display
  // and then catch it out in $mocked_instance
  $mocked_view_mode = 'mvf_dummy_view_mode';
  $instance['display'][$mocked_view_mode] = $display;

  // Sort by weight the subfields.
  uasort($instance['widget']['settings']['meta_info'], 'drupal_sort_weight');
  foreach ($items as $delta => $item) {
    $element[$delta] = array();
    foreach ($instance['widget']['settings']['meta_info'] as $subfield => $meta_info) {
      if ($subfield == 'unit' && $display['type'] == 'mvf_formatter_symbol') {

        // We were requested to render the unit as symbol, so we do not delegate
        // rendering of unit to the sub formatter, we do it ourselves.
        $extra = array(
          '#markup' => $item['entity']->symbol ? filter_xss_admin($item['entity']->symbol) : entity_label('units_unit', $item['entity']),
        );
      }
      else {

        // We are supposed to delegate formatting of this subfield to the sub
        // formatter, whatever sub formatter was chosen in the settings of our
        // superior formatter.
        $mocked_field = mvf_field_mockup($field, $subfield);
        $mocked_instance = mvf_instance_mockup($field, $instance, $subfield);
        $mocked_display = $mocked_instance['display'][$mocked_view_mode];
        unset($mocked_instance['display'][$mocked_view_mode]);
        $mocked_delta = 0;
        $mocked_items = array(
          $mocked_delta => $item,
        );
        $extra = module_invoke($mocked_display['module'], 'field_formatter_view', $entity_type, $entity, $mocked_field, $mocked_instance, $langcode, $mocked_items, $mocked_display);
      }
      if (is_array($extra)) {
        if (!isset($extra['#prefix'])) {
          $extra['#prefix'] = '';
        }
        if (!isset($extra['#suffix'])) {
          $extra['#suffix'] = '';
        }

        // Wrapping subfield in a separate <div> for easy theming.
        $extra['#prefix'] = '<div class="mvf-subfield mvf-' . $subfield . '">' . $extra['#prefix'];
        $extra['#suffix'] .= '</div>';
        $element[$delta][$subfield] = $extra;

        // Adding extra CSS classes for easier theming.
        list(, , $bundle) = entity_extract_ids('units_unit', $item['entity']);
        $element[$delta]['#prefix'] = '<span class="mvf-unit-' . entity_id('units_unit', $item['entity']) . ' mvf-measure-' . $bundle . '">';
        $element[$delta]['#suffix'] = '</span>';
      }
    }
  }

  // Only add CSS if there is item to prevent rendering empty field.
  if (!empty($element)) {
    $element['#attached']['css'] = array();
    $element['#attached']['css'][drupal_get_path('module', 'mvf') . '/mvf.css'] = array();
  }
  return $element;
}