Score:0

User form twig overwrite breaks after the submission doesn't have the current password

na flag

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?

apaderno avatar
us flag
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 avatar
us flag
In the specific, for entity forms, their ID is returned by [`EntityForm::getFormId()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityForm.php/function/EntityForm%3A%3AgetFormId/9).
pierostz avatar
na flag
I fixed the missing parenthesis. I omitted it by mistake in my post. Can you elaborate on your answer?
apaderno avatar
us flag
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.
Score:1
uz flag

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...');
  }
}
Score:0
us flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.