5

I have a field called field_price. How do I get that field on my page to display Free when the value is $0.00, and the actual value every other time?

I tried to intercept the loading of the field by doing things like:

mytemplate_hook_load(){
// do stuff
}

mytemplate_process_field(){
// do stuff
}

mytemplate_preprocess_field(){
// do stuff
}

etc.. etc.. etc..

I tried so many different permutations of the above as modules, template, changing function names etc... But none of them ever fire. I know I have the Field module enabled.

What am I doing wrong?

Note: I am using drupal 7

0

2 Answers 2

4

Ok, i figured it out. I put this code into my template.php of my theme:

function johncustom_field__field_price(&$variables) {
        if($variables['element'][0] == '$0.00') return 'Free';
        else return $variables['element'][0];
}

I also had to clear the cache several times before this function would fire.

2
0

I believe hook_field_formatter_view is the answer for your question. It says

Build a renderable array for a field value.

Using above hook in a custom module you can change the field value !

For Example, if your custom module name is johncustom then your function inside module file should be

function johncustom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  // Change display type & Check for your field here
  switch ($display['type']) {
    // Override check plain
    case 'taxonomy_term_reference_plain':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => mymodule_crazy_fun_plain($item['value']),
        );
      }
    break;
  }
  return $element;
}
6
  • Ok thanks. let's say I have a module called johncustom. So would my hook be called johncustom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) ? I tried putting that in my module but it doesn't fire.
    – John
    Oct 6, 2012 at 5:04
  • Any suggestions on why my function is not firing? I tried putting a die() statement in the code you provided. But it doesn't fire. Eveyr other function/hook in my johncustom.module is firing. I made sure I have Field UI and Field modules installed and enabled. Is there somethign else I need to check/do?
    – John
    Oct 6, 2012 at 5:19
  • Do this to verify , put drupal_set_message("I am in John custom module"); just above $element = array(); and go to any node view page where you are displaying fields. Check message is appearing or not !
    – Anil Sagar
    Oct 6, 2012 at 5:21
  • I did as you say. The message is not appearing on any of my pages. ANy other ideas?
    – John
    Oct 6, 2012 at 5:26
  • 2
    @John That hook is only fired for modules implementing field formatters, you can't use it to alter other modules' implementations of a formatter
    – Clive
    Oct 6, 2012 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.