Score:0

Change product price title in form

cn flag

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.

cn flag
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 https://drupal.stackexchange.com/questions/5802/how-can-i-find-the-form-id-of-a-form
cn flag
Form ID= "commerce_product_default_add_form". Where to go from here?
cn flag
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. https://www.drupaleasy.com/quicktips/drupal-8-debugging-kareful-klicking-kint
cn flag
This doesn't work: `function THEME_commerce_product_default_add_form_form_alter(&$form, &$form_state, $form_id) {dpm($form_id); }`
cn flag
This seems to work: `function THEME_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'commerce_product_default_add_form') {dpm($form); } }`
cn flag
It would be `THEME_form_commerce_product_default_add_form_alter` for the more specific hook - you had the first `form_` part at the end
Score:1
cn flag
$form['variations']['widget']['entity']['price']['widget'][0]['#title'] = t('Amount');
cn flag
This is exactly what I needed!
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.