1

I used Drupal Console to create a custom content entity called 'Service' (with command drupal geco) which generated all expected code including 'ServiceDeleteForm.php':

<?php
namespace Drupal\sel\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
 * Provides a form for deleting Service category entities.
 *
 * @ingroup sel
 */
class ServiceDeleteForm extends ContentEntityDeleteForm {
}

I added some code of mine there:

class ServiceDeleteForm extends ContentEntityDeleteForm {

  protected function getDeletionMessage() {
    $entity = $this->getEntity();
    drupal_set_message($this->t('Le service « %label » a été supprimé.', [
      '%label' => $entity->label(),
    ]));
  }

  public function getQuestion() {
    return $this->t('Êtes-vous certain de vouloir supprimer le service « %label » ?', [
      '%label' => $this->getEntity()->label(),
    ]);
  }

}

which works fine.
I'd like to add a redirection at the end of the Delete process, something like $form_state->setRedirect('elsewhere', [some argument]); but I don't know how to do it.
Could someone help? Thanks in advance.

1

Finally, this code works:

<?php
namespace Drupal\sel\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a form for deleting Service entities.
 *
 * @ingroup sel
 */

class ServiceDeleteForm extends ContentEntityDeleteForm {

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this->getEntity();

    $entity->delete();
    $form_state->setRedirect('elsewhere', [some argument]);
    drupal_set_message($this->getDeletionMessage());

  }

  protected function getDeletionMessage() { some code }

  public function getQuestion() { some code }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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