Properly adding an action to a ContentEntityForm I've created an add/edit form for a custom entity type, which extends Drupal\Core\Entity\ContentEntityForm. By default this form always comes with a "Save" button, and a "Delete" button if editing. I've been asked to add a "Cancel" button as well, but can't figure out how this is supposed to be done in Drupal.

I've seen plenty of answers about using hook_form_alter(), but nothing that is really working. My current solution is to add

$form['cancel'] = array(
    '#type' =&gt; 'submit',
    '#weight' =&gt; 10001,
    '#submit' =&gt; array('cancel_submit'),
    '#value' =&gt; t('Cancel'),
);


to the form's form() function, where cancel_submit() is a custom function, but that renders like  which looks awful.

How can I get the two buttons to be in line with each other? Is there a way to properly add a button to the form actions?
This is relatively simple when you define the form class yourself and one of the reasons Drupal 8 has gone all out on OOP. What you need to do is to extend the form class and implement the ::actions method. This would look like this:

class CustomEntityForm extends ContentEntityForm {
  ...

  protected function actions(array $form, FormStateInterface $form_state) {
    $actions =  parent::actions($form, $form_state);
    $actions['cancel'] = [
      '#type' =&gt; 'submit',
      '#value' =&gt; $this-&gt;t('Cancel'),
      '#submit' =&gt; ['::cancelSubmit'],
    ];
    return $actions;
  }

  public function cancelSubmit($form, FormStateInterface $form_state) {
    // Du custom submit validation
  }

}


You could also achieve this with normal form alter, but would need to place the submit button inside the actions element, which is usually called actions, fx

$form['actions']['cancel'] = array(
  '#type' =&gt; 'submit',
  '#weight' =&gt; 10001,
  '#submit' =&gt; array('cancel_submit'),
  '#value' =&gt; t('Cancel'),
);