0

how can I replace the labels of start/end date for a date field via form_alter? the code below only works for the fieldset. The "to" needs to be removed.

$form['field_my_dates']['und']['0']['#title'] = t('My new title');

see required changes in red

Code below not working neither, please help me to figure out how to do it! I forgot to mention that I'm using the popup widget for the date field.

function mywebform_date_combo_process_alter(&$element, &$form_state, &$context) {
    if ($element['#field_name'] == 'field_my_dates') {
        $element['value']['#date_title'] = 'From';
        $element['value2']['#date_title'] = 'To';
        $element['value2']['#title'] = t('');
    }
}

function mywebform_date_part_label_date($vars) {
    if ('field_my_dates' == $vars['element']['#field']['field_name']) {
        return t($vars['element']['#date_title']);
    } else {return t('Date');}
}

1 Answer 1

0

Something similar to below might help,change combo field title from 'to:' to none:

function mymodulename_date_combo_process_alter(&$element, &$form_state, &$context) {
    $element['value2']['#title'] = t('');
}

To change From and To date field labels,implement

function mymodule_date_part_label_date(&$vars) {
    if ($vars['element']['#field']['field_name'] == 'yourfield_machine_name') {
      if ($vars['element']['#date_title'] == 'Date Start date') {
        return t('Date (From:)');
      } elseif ($vars['element']['#date_title'] == 'Date End date') {
          return t('Date (To:)');
        }
    } else {
        return t($vars['element']['#date_title']);
    }
}
1
  • thanks a lot for the quick reply, wow!. The first function worked out great to remove "to". The second one is confusing because the label for start date is the same as end date, which is by default "Date". I want to remove "Date" and instead display "From" and "To" respectively. Any suggestions how to modify the second function to achieve that? my machine field name is: field_my_dates, in case you want to use it to explain where to put it.
    – SBLocal
    Commented Dec 17, 2014 at 22:32

Your Answer

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

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