Score:0

Change order of elements in a Webform Variant

to flag

I am trying to figure out if it is possible to change the order of elements in a webform variant.

Using a very simple example form with two elements, I created a new variant and then overrode the "Elements" yaml two different ways, neither which changed the order of the elements on the form.

First, I simply switched the order of the elements in the yml, which didn't change anything. Second, I added a #weight attribute to each element with different values - this had no effect as well.

Is it possible to change the order of elements in a variant?

thanks,
-mike

Score:0
us flag

Your need to reorder elements makes sense. The #weight attribute is currently ignored because it can cause unpredictable rendering. I don't think a Variant can reorder elements without patching the webform module.

Score:0
in flag

Albeit brute-force, theoretically you might be able to do this via a custom variant plugin type.

class MyVariant extends WebformVariantBase {
  // ...
  // ...

  /**
   * {@inheritdoc}
   */
  public function applyVariant() {
    $webform = $this->getWebform();
    $elements = $webform->getElementsDecoded();

    $element_to_move = $elements['element_to_move'];
    $new_position = array_search('element_to_insert_after', array_keys($elements), TRUE);
    $before = array_slice($elements, 0, $new_position);
    $after = array_slice($elements, $new_position, count($elements) - $new_position);
    $new_elements = $before + ['element_to_move' => $element_to_move] + $after;
    $webform->setElements($new_elements);

    return TRUE;
  }
}
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.