swan
swan

Reputation: 2599

How to alter field value drupal 7

I am working with drupal 7, and wanted to change the output of "number_float" value when it is "0.00". I have digged into field.api but has no clue what function to do.

To say it in plain English:

if the field type "number_float" and value is "0.00", print "empty value".

This is also to consider before views output. Any hint or guidance would be very much appreciated.

Thanks

UPDATE: I used hook_field_attach_view_alter. It does as expected, however I wonder if this is the right thing.

function mymodule_field_attach_view_alter(&$output, $context) {
  foreach (element_children($output) as $field_name) {
    $element = &$output[$field_name];
    if ($element['#field_type'] == 'number_float' && $element['#formatter'] == 'number_decimal') {
      foreach ($element['#items'] as $delta => $item) {
        if ($element[$delta]['#markup'] == '0.00' || $element[$delta]['#markup'] == '0,00') {
          $element[$delta]['#markup'] = t('Empty value message');
        }

      }
    }
  }
}

Any suggestion or betterment will be the answer.

thanks

Upvotes: 0

Views: 2767

Answers (2)

RobW
RobW

Reputation: 10611

A more standard Drupal way would be to manipulate the value in a preprocessor function. You can use hook_preprocess_HOOK for a theme function or template that's defined by another module. Inside of it, test for the 0.00 value and replace.

Upvotes: 2

swan
swan

Reputation: 2599

The update with hook_field_attach_view_alter is the way to go since nobody provides any other suggestion.

Upvotes: 0

Related Questions