2

I want to change a label/string on a node edit form for a date field.

The label is, "Show end date" - I want to change this to, "Show end date (optional)", but I can't find where this is set anywhere. My guess is that it is some sort of theming change - I took a look at the node_form and saw nothing.

3 Answers 3

2

This code is compatible with Drupal 9. The label is translatable as well.

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function new_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_new_type_form') {
    $form['field_add_date']['widget'][0]['#title'] = t(' Show end date (optional)');
  }
}
0

Simply find the adequate form by calling hook_form_alter and letting Devel print out every possible form id:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_id);
}

When you found your edit form id (let's say it's 'xyz_edit') you might inspect the form like so:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id = 'xyz_edit') {
    dpm($form);
  }
}

Now find the label by doubleclicking through the dpm output and replace the label whith your desired text.

0

You can easily do this using hook_form_alter(&$form, &$form_state, $form_id) Alter

Hook_form_alter is working before your form shown Use hook_form_alter in Your site theme template.php file.

Like your theme name is bartik,then use this function their

function bartik_form_alter(&$form, &$form_state, $form_id)
{
//Firstly get form id then use below code

if($form_id=='Your_edit_form_id'){
//here print $form and chek which field label you want to change and then alter that field //like

$form['date'][#label]='Date Field(optional)';
}


}

may this will help you.

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.