0

I have used hook_entity_base_field_info in my custom module to add a field to the user entity:

$fields['phone'] = BaseFieldDefinition::create('telephone')
  ->setName('phone')
  ->setLabel(t('Phone number'))
  ->setDescription(t('User phone number information.'))
  ->setRequired(TRUE)
  ->setRevisionable(TRUE)
  ->setTranslatable(FALSE)
  ->setSetting('max_length', 15)
  ->setDisplayOptions('form', [
    'type' => 'telephone_default',
    'weight' => -3,
    'settings' => [
      'placeholder' => '123-456-7890',
    ],
  ])
  ->setDisplayOptions('view', [
    'type' => 'telephone_default',
    'weight' => -3,
    'label' => 'above',
  ])
  ->setDisplayConfigurable('view', TRUE)
  ->setDisplayConfigurable('form', TRUE);

But in the user form, the name of the field is name="phone[0][value]" as follows:

<input data-drupal-selector="edit-phone-0-value" aria-describedby="edit-phone-0-value--description" type="tel" id="edit-phone-0-value" name="phone[0][value]" value="" size="30" maxlength="256" placeholder="123-456-7890" class="form-tel required form-element form-element--type-tel form-element--api-tel" required="required" aria-required="true">

I want my field name to be like name="phone" and id="edit-phone". How can i make my phone field name like email field:

<input data-drupal-selector="edit-mail" aria-describedby="edit-mail--description" type="email" id="edit-mail" name="mail" value="" size="60" maxlength="254" class="form-email required form-element form-element--type-email form-element--api-email" required="required" aria-required="true">

I also tried to create field in install yml file, but the result was the same.

1
  • You may have to use a different framework.
    – cilefen
    Dec 7, 2023 at 11:44

1 Answer 1

0

Since the question compares the form element added for a telephone entity field with the form element used for the account email, and wonders why in the latest case the form element name is simply mail, I will answer this part.

The Email address form element is not added as entity field, but it is a normal form element added by AccountForm::form().

  $form['account']['mail'] = [
    '#type' => 'email',
    '#title' => $this->t('Email address'),
    '#description' => $this->t('The email address is not made public. It will only be used if you need to be contacted about your account or for opted-in notifications.'),
    '#required' => !(!$account->getEmail() && $user->hasPermission('administer users')),
    '#default_value' => !$register ? $account->getEmail() : '',
  ];

If you add a new email entity field (for example, Secondary email), its HTML element name would be similar to the one you noticed for the telephone field.

<input data-drupal-selector="edit-field-secondary-email-0-value" type="email" id="edit-field-secondary-email-0-value" name="field_secondary_email[0][value]" value="" size="60" maxlength="254" placeholder="" class="form-email form-element form-element--type-email form-element--api-email">

This does not happen just for the email form element, but also for the Username field, which is added from AccountForm::form() too.

  $form['account']['name'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Username'),
    '#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
    '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
    '#required' => TRUE,
    '#attributes' => [
      'class' => [
        'username',
      ],
      'autocorrect' => 'off',
      'autocapitalize' => 'off',
      'spellcheck' => 'false',
    ],
    '#default_value' => !$register ? $account->getAccountName() : '',
    '#access' => $account->name->access('edit'),
  ];

The HTML markup added for that form element is the following one, on Drupal 10.2.

<input class="username form-text required form-element form-element--type-text form-element--api-textfield" autocorrect="off" autocapitalize="none" spellcheck="false" autocomplete="off" data-drupal-selector="edit-name" aria-describedby="edit-name--description" type="text" id="edit-name" name="name" value="admin" size="60" maxlength="60" required="required" aria-required="true">

It could be possible to get a simpler HTML element name for a telephone entity field, by adding a form element that overrides the one used by the entity field, but only when the form element is added by an entity form class like the AccountForm class. Anything added after, for example in a hook_form_alter()/hook_form_FORM_ID_alter() implementation, would not have any effect.

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.