n How to use ConfirmFormBase to confirm the action of delete nodes in drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

in this article, I'll show you how to use ConfirmFormBase to confirm the action of delete nodes in drupal 8.

Confirmation forms are simple to build and are the recommended way to prompt a user for confirmation of an action.  As with many forms, it all begins with a route. 

codimth_form_api.routing.yml

codimth.confirm_delete_form_api:
  path: '/codimth/confirm_delete'
  defaults:
    _form: '\Drupal\codimth_form_api\Form\CodimthConfirmDeleteForm'
    _title: 'Codimth Confirm Deletion'
  requirements:
    _permission: 'administer site configuration'

src/Form/CodimthConfirmDeleteForm.php

this class contains : 

getFormId(): This needs to return a string that is the unique ID of your form. Namespace the form ID based on your module's name.

buildForm(): This returns a Form API array that defines each of the elements of your form.

submitForm(): collect data and do things, like save the data to the database, send an email, or any number of other operations.

getCancelUrl(): Returns the route to go to if the user cancels the action..

getQuestion(): Returns the question to ask the user.


<?php

namespace Drupal\codimth_form_api\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Implements a Codimth Confirm Delete Form.
 */
class CodimthConfirmDeleteForm extends ConfirmFormBase
{


  /**
   * @return string
   */
  public function getFormId()
  {
    return 'codimth_form_api_confirm_deletion';
  }


  /**
   * @param array $form
   * @param FormStateInterface $form_state
   * @return array
   */
  public function buildForm(array $form, FormStateInterface $form_state)
  {
    // Textfield.
    $form['title'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Title'),
      '#size' => 60,
      '#maxlength' => 128,
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * @param array $form
   * @param FormStateInterface $form_state
   */
  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $title = $form_state->getValue('title');
    $result = \Drupal::entityQuery("node")
      ->condition('title', $title)
      ->execute();
    $storage_handler = \Drupal::entityTypeManager()->getStorage("node");
    $entities = $storage_handler->loadMultiple($result);
    $storage_handler->delete($entities);

    \Drupal::messenger()->addStatus(t('node @title deleted.', ['@title' => $title]));
    return Url::fromRoute('<front>');
  }


  /**
   * Returns the question to ask the user.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The form question. The page title will be set to this value.
   */
  public function getQuestion()
  {
    return t('Do you want to delete this node?');
  }

  /**
   * Returns the route to go to if the user cancels the action.
   *
   * @return \Drupal\Core\Url
   *   A URL object.
   */
  public function getCancelUrl()
  {
    return Url::fromRoute('<front>');
  }
}

 

confirm action form api

 

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook