0

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.

1 Answer 1

1

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>',
];
2
  • 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. Sep 27, 2021 at 9:44
  • So I'm happy with setting the class attribute over the $actions array for now. Oct 2, 2021 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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