0

I am wondering if there is a simple way (dropdown option or checkbox) to add a title to a field in the Drupal Commerce checkout page. Specifically, I want the second field for Street Address to have the label: Address line 2.

screenshot

I was unable to find this field in Admin > Structure > View that allows the option to "Manage Display" or "Manage Form Display" and make a label for a field "Hidden" or otherwise.

I also looked in the Commerce sections to no avail.

Next step is to find out if there is a Billing Information > "Manage Form Display menu somewhere.

Not in Home > Administration > Configuration > People > Profiles > Profile types > Edit Customer > Manage fields either.

  • 1
    Please be aware that we do not provide tutorials or step-by-step instructions for achieving functionality from a description, screenshot or other website, as explained in the help center. Instead explain your attempt(s), and be as complete as possible. What have you tried, what did you expect, what actually happened? If you have any code to include in the question, please add it. The more complete you are, the better we can formulate an answer. – Neograph734 Aug 21 '17 at 20:07
  • I am not looking for a tutorial, more so for a menu or option that I am not yet aware of. *Original post re-worded to reflect this. – Parker Brown Aug 21 '17 at 22:05
0

Not possible in User Interface (via link or menu or whatever). But if you use the following code in a module, you can add a title to your form element:

<?php
function CUSTOM_form_alter($form, $form_state, $form_id) {
  // You should probably check for a form_id in here, as this runs on every form.
  // Below you will need to find the part of the $form array that has the billing address.
  $form['[see note above]']['widget'][0]['address']['address_line2']['#title'] = t('Address 2');
}
0

The second line of the address field has an invisible title. It's explicitly set by the Address module.

You can see that in action here: cgit.drupalcode.org/address/tree/src/Element/Address.php?h=272a7ce#n231

// Hide the label for the second address line.
if (isset($element['address_line2'])) {
  $element['address_line2']['#title_display'] = 'invisible';
}

You could use hook_element_info_alter() to define new process or after_build callbacks.

function mytheme_element_info_alter(array &$types) {
  $types['address']['#process'][] = ['\Drupal\mytheme\Element\Address', 'processAddress'];
}

Overriding the element:

namespace Drupal\mytheme\Element;

use \Drupal\address\Element\Address as DefaultAddress;
use \Drupal\Core\Form\FormStateInterface;

/**
 * {@inheritdoc}
 */
class Address extends DefaultAddress {

  /**
   * {@inheritdoc}
   */
  public static function processAddress(array &$element, FormStateInterface $form_state, array &$complete_form) {
    $element = parent::processAddress($element, $form_state, $complete_form);
    unset($element['address_line2']['#title_display']);
    return $element;
  }

}
0

This answer is clumsy as it makes all form element labels visible, but ultimately gets the job done.

Using this theme and modifying specifically this template: DOrange_form-element-label.html.twig with an additional conditional before the set classes statment:

{%- if title_display == 'invisible'-%}
  {%-
    set title_display = [
      'visible',
    ]
  -%}
{%- endif -%}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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