How can I access the label of the product price field to change it for example to "amount" instead of "price"?

The html for the field is:

<div class="js-form-item form-item js-form-type-commerce-number form-item-variations-entity-price-0-number js-form-item-variations-entity-price-0-number">

<label for="edit-variations-entity-price-0-number" class="js-form-required form-required">Price</label>

<input data-drupal-selector="edit-variations-entity-price-0-number" type="text" id="edit-variations-entity-price-0-number" name="variations[entity][price][0][number]" value="" size="10" maxlength="128" class="form-text required" required="required">

I have tried various alternatives, but couldn't find a way to reach the title or the price field.

function THEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    $form['variations']['entity']['price'][0]['number']= t('Amount');
    $form['variations']['entity']['price']['number']= t('Amount');
    $form['price']= t('Amount');

I also couldn't find any examples to use form_alter on Drupal 8 Commerce product form while it was possible with Drupal 7.

  • You need to use hook_form_alter() on the correct form ID. So the first step is to find the form ID, and then use that to dump the form and figure out what you need to change. See drupal.stackexchange.com/questions/5802/… Feb 18, 2022 at 9:13
  • Form ID= "commerce_product_default_add_form". Where to go from here?
    – cateye
    Feb 18, 2022 at 10:29
  • Install the Devel module (not on production!) and use Kint. Then you can do something like kint($form) to get a somewhat pretty list of all the variables. drupaleasy.com/quicktips/… Feb 18, 2022 at 10:35
  • This doesn't work: function THEME_commerce_product_default_add_form_form_alter(&$form, &$form_state, $form_id) {dpm($form_id); }
    – cateye
    Feb 18, 2022 at 12:01
  • This seems to work: function THEME_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'commerce_product_default_add_form') {dpm($form); } }
    – cateye
    Feb 18, 2022 at 12:09

1 Answer 1

$form['variations']['widget']['entity']['price']['widget'][0]['#title'] = t('Amount');
  • This is exactly what I needed!
    – cateye
    Feb 22, 2022 at 17:34

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.