Hi,

I'm pretty new to Drupal and coding - I'm not a developer but I work in a drupal agency. So right now I have to write Behat tests to test functionalities of our features in Drupal 8. The tests need element IDs in the most cases but whyever there are hardly no IDs to select.
So my idea was to add IDs to certain elements like buttons (dropbuttons for example) or radio buttons.

I'm testing the Paragraphs module right now and need an ID for the "Add Paragraph" dropbutton where you can select the paragraphs. How can I add an ID there?

I hope you understand my intentions and forgive me if my question sounds stupid :)

Thanks a lot,

Julia

Comments

Jaypan’s picture

HTML IDs? Or data IDs on the back-end (php/db)?

jkadauke’s picture

HTML IDs i think so I can select them as CSS selectors for the behat tests.

dotmundo’s picture

Have you looked at hook_form_alter or overriding the template?

dotmundo’s picture

For example

function hook_form_alter (&$form, FormStateInterface &$form_state) {

    // Add #id attribute to all my form elements
    foreach ($form as $key => $frm) {
        $form[$key]['#id'] = $key;
    }

}

mrlexington’s picture

You may have to dig deeper in the $form array to set the id of the <input> form element. Otherwise you're just setting the id of a wrapping div.

function hook_form_alter(&$form, FormStateInterface $form_state) {
  $form['field_element_name']['widget']['0']['value']['#id'] = 'element_name_id';
}