5

Can anyone tell me why this isn't working (in the below code, or other factors preventing the insert by the hook)? I don't get an error message or page, the field labels are simply not overwritten...

I created my own module with the following hook on Drupal 8.5.7:

function MYMODULE_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id){

  if ($form_id == 'user_register_form') {

    $form['field_user_first_name']['widget']['0']['#title'] = 'First Name';
    $form['field_user_last_name']['widget']['0']['#title'] = 'Last Name';
    $form['field_user_phone_number']['widget']['0']['#title'] = 'Phone Number';
    $form['field_user_country_of_residence']['widget']['0']['#title'] = 'Country (of residence)';
  }
}
1

4 Answers 4

9
  1. Simple Solution just change ['0']['#title'] by [0]['value']['#title'].

/**
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function MYMODULE_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id){
    if ($form_id == 'user_register_form') {
        $form['field_user_first_name']['widget'][0]['value']['#title'] = 'First Name';
        $form['field_user_last_name']['widget'][0]['value']['#title'] = 'Last Name';
        $form['field_user_phone_number']['widget'][0]['value']['#title'] = 'Phone Number';
        $form['field_user_country_of_residence']['widget'][0]['value']['#title'] = 'Country (of residence)';
    }
}

  1. This solution a little bit complicated.
    After i test your code, i did some search, i found this Post say

    It is apparent that we modify any form in drupal by implementing hook_form_alter(). But there are cases when even the hook_form_alter() cannot offer a solution in altering the form fields.

I tried to use hook_element_info_alter as it suggested to alter fields. In your case Here is the code bellow.


/**
* Implements hook_element_info_alter().
*/
function MYMODULE_element_info_alter(&$type) {
  if (isset($type['textfield'])) {
    $type['textfield']['#process'][] = 'MYMODULE_process_textfield';
  }
}

/** * Process Callback Function * * Callback to process the User fields. */ function MYMODULE_process_textfield($element) { //- get the current route name $route_name = \Drupal::routeMatch()->getRouteName(); if ($route_name == 'user.register' && isset($element['#array_parents'][0])) { switch ($element['#array_parents'][0]) { case 'field_user_first_name': $element['#title'] = 'New first name label'; break; case 'field_user_last_name': $element['#title'] = 'New last name label'; break; case 'field_user_phone_number': $element['#title'] = 'New phone numbe label'; break; case 'field_user_country_of_residence': $element['#title'] = 'New country of residence label'; break; } } return $element; }

Note : all the four fields should be of type textfield Text (plain), otherwise you should add another process to other type, see the example in the article for password_confirm type.

2

following the link posted by @leymannx I clicked through to another question, which had the solution somewhere in the comments.

I forgot to put ['value'] in the string, so it had to be :

$form['field_user_first_name']['widget']['0']['value']['#title'] = 'First Name';

I can confirm it works with both :

  • ['0'] and [0]

  • = t('first name') and = 'first name'

I doesn't work, at least for me, with omitting ['widget']['0']['value'], as suggested in alter the labels used on the login form – berramou

0

Try it without [0], I don't think you need this.


Edit Okay let me rephrase my words per @leymannx. I have tested the code on my machine and removing [0] solves the problem and updates the label text. I was providing an answer and was not asking for more information or anything from the author.

0
0

In this approach, the hook is fired for all forms To avoid extra firing of the hook, use this approach (module name and form id)

function MYMODULE_user_register_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  //do your alteration
}

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.