I have a number field where a user needs to enter two values. Each value should have a custom prefix but that isn't possible within the field settings.
Does anybody know how this can be done? Thanks.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThat's how you can add prefixes to node output (replace view_mode and field_name to yours):
/**
* Implements hook_preprocess_field().
*/
function mymodule_preprocess_field(&$vars) {
if ($vars['element']['#entity_type'] == 'node' &&
$vars['element']['#view_mode'] == 'full' &&
$vars['element']['#field_name'] == 'field_number') {
$vars['items'][0]['#markup'] = '<span>$</span>' . $vars['items'][0]['#markup'];
$vars['items'][1]['#markup'] = '<span>€</span>' . $vars['items'][1]['#markup'];
}
}
You can use form_alter function to do it:
/**
* Implements hook_form_alter().
*/
function your_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'article_node_form') {
$form['field_number'][LANGUAGE_NONE][0]['value']['#prefix'] = '$';
$form['field_number'][LANGUAGE_NONE][1]['value']['#prefix'] = '€';
}
}
your_module
in function name should be replaced by your module's name, form id article_node_form
should be replaced by your form id, and field_number
- to your field name. This answer can help you to create a custom module: drupal.stackexchange.com/questions/130548/…