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.
1 Answer
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.– vintorgCommented Jul 2, 2013 at 5:40
-
@vintorg - I don't know about omega. Why didn't you put your pre-process function in template.php file ?– RajeevKCommented Jul 2, 2013 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.– vintorgCommented Jul 2, 2013 at 23:36
-
-