4

I'm using Display Suite with Drupal Commerce, and I need to pre-process some fields in my product display. I need to change some values based on some conditions, and need to wrap a few fields in some markup. Should I write a hook to pre-process and change the values, and use a template to wrap the fields in markup? If so, which template file would I use, because DS comes with a few templates.

7

You can customize your field at theme level by using TEMPLATE_preprocess_field() function at template.php

You can get get some field value by node object or by fetching field values itself. You can do something like this to get fields value by node object --

function MYTHEME_preprocess_field(&$vars) { //Replace your theme name MYTHEME here.
  if ($node = menu_get_object()){
    if($node->type == 'PRODUCT_DISPLAY'){//Use your product display name here.
      $vars['node'] = $node;
      $product_id = $node->FIELD_PRODUCT_REFERENCE[LANGUAGE_NONE][0]['product_id'];//Use your product ref field name here.
      $product = commerce_product_load($product_id);
      $manufacturer = $product->FIELD_PRODUCT_MANUFACTURER[LANGUAGE_NONE][0]['value'];//Use your field name of product type here.
      $final_body_label = "Description by ".$manufacturer;

      //Set body label for node from here itself.
      $node->content['body']['#label_display'] = $final_body_label;

      //Or set a variable to use it further. 
      $$vars['manufacturer_description'] = $final_body_label;
  }
}

Further you can use your default template for node or page if it fulfils what you want or you can make separate TPL for fields too, which will be named like field--field_image.tpl.php, where field_image is field name of your content or product.

You will get default field.tpl in $base_path/modules/field/theme.

  • I created a preprocess-field.inc file (I'm using Omega), and added the themename_preprocess_field() function, but it's not being called. – vintorg Jul 2 '13 at 5:40
  • @vintorg - I don't know about omega. Why didn't you put your pre-process function in template.php file ? – RajeevK Jul 2 '13 at 6:57
  • Omega suggests pre-process functions are stored in their own file. You would put themename_preprocess_page(), for example, in a file called preprocess-page.inc. – vintorg Jul 2 '13 at 23:36
  • @vintorg Can you try it once with template.php ? – RajeevK Jul 3 '13 at 3:25
  • Actually, this is a known issue: drupal.org/node/1536070 – vintorg Jul 3 '13 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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