Hello,
Let me start by stating what I'm trying to do. Then I'll explain the context.
I'm trying to alter a line item's custom field label in the cart and checkout views/pages.
In the cart we display various custom fields that are relevant to a line item. These are loaded in the cart view using the "Rendered Commerce Line Item" field.
I tried using hook_views_pre_render(), but it seems like that's not what I'm looking for.
Here is an example of the exact same thing I'm doing on the product page, but using a different hook.
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function drukwm_field_widget_options_buttons_form_alter(&$element, &$form_state, $context) {
// Check if this is the correct field and if we're on an add-to-cart form.
if (!isset($form_state['build_info']['base_form_id']) || $element['#title'] != 'Levertijd' || $form_state['build_info']['base_form_id'] != 'commerce_cart_add_to_cart_form') {
return;
}
// Don't do anything if this is a partial form submit (like a file upload or AJAX request).
$line_item_fields = field_info_instances($element['#entity_type'], $element['#bundle']);
if (isset($form_state['values']) && array_diff_key($line_item_fields, $form_state['values']['line_item_fields'])) {
return;
}
global $language;
extract($context);
foreach ($element['#options'] as $option => $markup) {
$entity = entity_load_single('product_option', $option);
if(!property_exists($entity, 'field_werkdagen') || !property_exists($entity, 'field_deadline')) {
continue;
}
$entity_wrapper = entity_metadata_wrapper('product_option', $option);
$date = drukwm_calculate_workday($entity_wrapper->field_werkdagen->value(), REQUEST_TIME, $entity_wrapper->field_deadline->value());
$element['#options'][$option] .= '<span class="leverdatum-product">'. format_date($date, 'custom', 'l d F', NULL, $language->language) . '</span>';
}
}
Comments
Comment #1
rszrama commentedSorry, we don't really offer this sort of support in this queue. Please try the linked Commerce Q&A resource from the issue submission guidelines or the Drupal Stack Exchange. There's no good way for us to determine what hook you'll need here, so you'll likely just need to keep playing around with it. If this is part of a View, I'd probably look for a relevant Views template to implement.