Score:0

Alter all webform fields

us flag

If we want to alter webform item e.g. add a colon : to a label of a specific field, we use this:

$form['submitted']['my_form_component']['#title'] = $form['submitted']['my_form_component']['#title'] . ":";

But what if we want to apply that colon to all field labels and not just that one my_form_component?

Current code:

function MYTHEME_form_alter( &$form, &$form_state, $form_id ){
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['submitted']['my_form_component']['#title'] = $form['submitted']['my_form_component']['#title'] . ":";
  }
}
Score:1
de flag

You should be able to do it with a recursive function, looping through the render array and adding a colon to any elements that have a #title element.

function addColonsToTitles(&$element) {
  if (isset($element['#title'])) {
    $element['#title'] .= ':';
  }

  foreach (element_children($element) as $index) {
    addColonsToTitles($element[$index]);
  }
}

function MYTHEME_form_alter( &$form, &$form_state, $form_id ){
  if (strpos($form_id, 'webform_client_form_') === 0) {
    addColonsToTitles($form['submitted']);
  }
}
user3108268 avatar
us flag
This solves it, but I also expected for the colons to appear in emails. They actually appear by default, but toggling HTML setting for emails in Webforms for some reason strips colons. Anyway, thanks.
Jaypan avatar
de flag
You asked how to change form labels. The labels in the email will not be form labels, and the manner of adding colons would be entirely different. You can open a new issue for that.
I sit in a Tesla and translated this thread with Ai:

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.