class RobotDeleteForm

Same name in other branches
  1. 3.x modules/config_entity_example/src/Form/RobotDeleteForm.php \Drupal\config_entity_example\Form\RobotDeleteForm
  2. 4.0.x modules/config_entity_example/src/Form/RobotDeleteForm.php \Drupal\config_entity_example\Form\RobotDeleteForm

Class RobotDeleteForm.

Provides a confirm form for deleting the entity. This is different from the add and edit forms as it does not inherit from RobotFormBase. The reason for this is that we do not need to build the same form. Instead, we present the user with a simple yes/no question. For this reason, we derive from EntityConfirmFormBase instead.

Hierarchy

  • class \Drupal\config_entity_example\Form\RobotDeleteForm extends \Drupal\Core\Entity\EntityConfirmFormBase

Expanded class hierarchy of RobotDeleteForm

Related topics

File

config_entity_example/src/Form/RobotDeleteForm.php, line 20

Namespace

Drupal\config_entity_example\Form
View source
class RobotDeleteForm extends EntityConfirmFormBase {
    
    /**
     * Gathers a confirmation question.
     *
     * The question is used as a title in our confirm form. For delete confirm
     * forms, this typically takes the form of "Are you sure you want to
     * delete...", including the entity label.
     *
     * @return string
     *   Translated string.
     */
    public function getQuestion() {
        return $this->t('Are you sure you want to delete robot %label?', [
            '%label' => $this->entity
                ->label(),
        ]);
    }
    
    /**
     * Gather the confirmation text.
     *
     * The confirm text is used as the text in the button that confirms the
     * question posed by getQuestion().
     *
     * @return string
     *   Translated string.
     */
    public function getConfirmText() {
        return $this->t('Delete Robot');
    }
    
    /**
     * Gets the cancel URL.
     *
     * Provides the URL to go to if the user cancels the action. For entity
     * delete forms, this is typically the route that points at the list
     * controller.
     *
     * @return \Drupal\Core\Url
     *   The URL to go to if the user cancels the deletion.
     */
    public function getCancelUrl() {
        return new Url('entity.robot.list');
    }
    
    /**
     * The submit handler for the confirm form.
     *
     * For entity delete forms, you use this to delete the entity in
     * $this->entity.
     *
     * @param array $form
     *   An associative array containing the structure of the form.
     * @param \Drupal\Core\Form\FormStateInterface $form_state
     *   An associative array containing the current state of the form.
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        // Delete the entity.
        $this->entity
            ->delete();
        // Set a message that the entity was deleted.
        $this->messenger()
            ->addMessage($this->t('Robot %label was deleted.', [
            '%label' => $this->entity
                ->label(),
        ]));
        // Redirect the user to the list controller when complete.
        $form_state->setRedirectUrl($this->getCancelUrl());
    }

}

Members

Title Sort descending Modifiers Object type Summary
RobotDeleteForm::getCancelUrl public function Gets the cancel URL.
RobotDeleteForm::getConfirmText public function Gather the confirmation text.
RobotDeleteForm::getQuestion public function Gathers a confirmation question.
RobotDeleteForm::submitForm public function The submit handler for the confirm form.