Score:0

Actions form array overriding to set HTML id attribute

cn flag

I extend the EntityForm class and I try to override a submission button, to set its HTML ID attribute (try #1). Since it doesn't work, I tried to wrap the button in a wrapper <div>. I tried to wrap with two different ways (try #2 and try #3), but they don't work. The only way I can override the submision button is to set '#attributes' to set the HTML attribute class and to add my own CSS class, but my wish was to set a HTML id attribute. And I wanted to do this overriding in actions() method (not form()).

use Drupal\Core\Entity\ContentEntityForm;

class MyForm extends ContentEntityForm {

  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);

    $actions['submit'] = [
      '#type' => 'submit',
      '#id' => 'save-button', // <-- try #1
      '#value' => t('Save'),
      '#ajax' => [
        'callback' => '::ajaxCallback',
        'event' => 'mousedown',
        'wrapper' => 'bar-wrapper-1',   // <-- try #2
      ],
      '#prefix' => '<div id="bar-wrapper-2">',  // <-- try #3
      '#suffix' => '</div>',
      '#attributes' => [
        'class' => ['foo'],   // <-- try #4
      ],
    ];

    $actions['submit_wrapper'] = [
      '#type' => 'markup',
      '#markup' => '<div id="bar-wrapper-1"></div>'
    ];

    return $actions;
  }

}

I suppose I can only override the HTML ID within form(), but it looks a little inconsistent because I can override/add a CSS class.

Score:1
ch flag
STF

a sample here :

$form['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#weight' => 99,
  '#attributes' => ['class' => ['ex-black'], 'id' => ['testId']],
  '#prefix' => '<div id="testIdWrapper">',
  '#suffix' => '</div>',
];
Hermann Schwarz avatar
cn flag
Please note, I want to override the HTML attribute ID within actions() method in my subclass of `ContentEntityForm`. It's the $actions array, where I try to make the overriding. This way I can override `class` attribute, but not the `id`.
Hermann Schwarz avatar
cn flag
So I'm happy with setting the `class` attribute over the $actions array for now.
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.