0

I am overwriting the user edit form using this code.

function demetra_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_form') {
    $form['field_first_name']['widget'][0]['value']['#title'] = t('');
    $form['field_first_name']['widget'][0]['value']['#placeholder'] = t('Enter your name...');
  }
}

This works fine except when the user doesn't enter his password. The reason for this is that the user_form ID changes after the save to something like this user-form--hWMonV_OXao, therefore any overwriting is not working.

How do I handle this?

4
  • There is a missing parenthesis after if. What you shown is probably the CSS ID, not the form ID, since that does not change. Form IDs are returned from a method implemented by the form class, which usually returns a literal string.
    – apaderno
    Apr 28 at 7:32
  • 1
    In the specific, for entity forms, their ID is returned by EntityForm::getFormId().
    – apaderno
    Apr 28 at 7:38
  • I fixed the missing parenthesis. I omitted it by mistake in my post. Can you elaborate on your answer?
    – pierostz
    Apr 28 at 7:40
  • You said that the form ID changed, but form IDs do not change. user-form--hWMonV_OXao is not a form ID. In fact, form IDs do no contain - characters.
    – apaderno
    Apr 29 at 7:37

2 Answers 2

1

check if the beginning of the $form_id string matches the string user_form

function demetra_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, 'user_form') === 0) {
    $form['field_first_name']['widget'][0]['value']['#title'] = t('');
    $form['field_first_name']['widget'][0]['value']['#placeholder'] = t('Enter your name...');
  }
}
0

A form ID does not change when the form is submitted. It does not contains hyphens too; therefore, user-form--hWMonV_OXao is not a form ID.

The Contact module alters the user profile form using the following code. (See contact_form_user_form_alter().) It works whenever users enter their current password or not.

function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
  $form['contact'] = [
    '#type' => 'details',
    '#title' => t('Contact settings'),
    '#open' => TRUE,
    '#weight' => 5,
  ];
  $account = $form_state->getFormObject()->getEntity();
  if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
    $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  }
  $form['contact']['contact'] = [
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
    '#default_value' => $account_data ?? \Drupal::config('contact.settings')
      ->get('user_default_enabled'),
    '#description' => t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
  ];
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
}

As a side note, the current password is only required when users change their own email or the password. The description for the Current password field says that.

Required if you want to change the Email address or Password below.

The following hook implementation is sufficient.

function demetra_form_user_form_alter(&$form, FormStateInterface $form_state) {
  $form['field_first_name']['widget'][0]['value']['#title'] = '';
  $form['field_first_name']['widget'][0]['value']['#placeholder'] = t('Enter your name...');
}

As a further side note, an empty string is not translatable: It is still an empty string ('') in every language.

Your Answer

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

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