Skip to content
Snippets Groups Projects
Commit 39f461f4 authored by Rakesh Kumar Regar's avatar Rakesh Kumar Regar Committed by Kapil Kumar
Browse files

Issue #3392626: Create a saparate Delete button on the List page

parent 262abf63
No related branches found
No related tags found
1 merge request!9Issue #3392626: Create a saparate Delete button on the List page
......@@ -53,5 +53,15 @@ advance_script_manager.delete_form:
_title: 'Delete Script'
requirements:
_permission: 'advance_script_manager_settings'
options:
_admin_route: TRUE
advance_script_manager.delete_multiple_form:
path: '/admin/config/development/advance_script_manager/delete_multiple'
defaults:
_form: '\Drupal\advance_script_manager\Form\ListScriptsFormDeleteMultiple'
_title: 'Delete Multiple Scripts'
requirements:
_permission: 'advance_script_manager_settings'
options:
_admin_route: TRUE
\ No newline at end of file
<?php
namespace Drupal\advance_script_manager\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ListScriptsFormDeleteMultiple extends ConfirmFormBase {
/**
* Database connection object.
* @var Drupal\Core\Database\Connection;
*/
protected $database;
/**
* @inheritdoc
*/
public function __construct(Connection $database) {
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'advance_script_manager_delete_multiple_form';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete below listed scripts?');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$html = 'This action cannot be undon.';
$html .= $this->getItemlist();
return $html;
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('advance_script_manager.advance_script_controller_build');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return $this->t('Cancel');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$ids = $this->requestStack->getCurrentRequest()->get('ids');
$id = explode('|',$ids);
if (!empty($id)) {
$res = $this->database->delete('advance_script_manager')
->condition('id', $id, 'IN')
->execute();
if ($res) {
$this->messenger()->addMessage($this->t('Script has been deleted.'));
$form_state->setRedirect('advance_script_manager.advance_script_controller_build');
}
}
else {
$this->messenger()->addError($this->t('Script was not deleted, Something went wronge.'));
$form_state->setRedirect('advance_script_manager.advance_script_controller_build');
}
}
/**
* Function to get CurrentItemsList.
*/
public function getItemlist() {
$ids = \Drupal::request()->get('ids');
$id = explode('|',$ids);
$query = $this->database->select('advance_script_manager', 'asm');
$query->fields('asm', ['script_name']);
$query->condition('asm.id', $id, 'IN');
$res = $query->execute()->fetchAll();
$html = '<ul>';
foreach ($res as $value) {
$html .= '<li>' . $value->script_name . '</li>';
}
$html .= '</ul>';
return $html;
}
}
\ No newline at end of file
......@@ -107,22 +107,24 @@ class ListscriptsForm extends FormBase {
'0' => $this->t('-Select-'),
'activate' => $this->t('Activate selected scripts'),
'deactivate' => $this->t('Deactivate selected scripts'),
'delete' => $this->t('Delete selected scripts'),
'move_script_to_header' => $this->t('Move scripts to header'),
'move_script_to_body' => $this->t('Move scripts to body'),
'move_script_to_footer' => $this->t('Move scripts to footer'),
],
];
$form['submit'] = [
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Apply to selected scripts'),
'#prefix' => '<div class="form-actions js-form-wrapper form-wrapper">',
'#suffix' => '</div>',
'#submit' => ['::submitForm'],
'#attributes' => [
'class' => ['button--primary']
],
'#button_type' => 'primary'
];
$form['actions']['delete'] = [
'#type' => 'submit',
'#value' => $this->t('Delete'),
'#submit' => ['::deleteMultiple'],
'#button_type' => 'danger'
];
return $form;
......@@ -189,15 +191,6 @@ class ListscriptsForm extends FormBase {
$action = $form_state->getValue('bulk_action_type');
switch ($action) {
case 'delete':
$res = $this->database->delete('advance_script_manager')
->condition('id', $item_ids, 'IN')
->execute();
if ($res) {
$this->messenger->addMessage($this->t('Scripts has been deleted.'));
}
break;
case 'deactivate':
$res = $this->database->update('advance_script_manager')
->fields(['status' => 2])
......@@ -255,4 +248,19 @@ class ListscriptsForm extends FormBase {
}
/**
* {@inheritdoc}
*/
public function deleteMultiple(array &$form, FormStateInterface $form_state) {
$item_ids = $form_state->getValue('list_scripts');
$ids = implode("|",$item_ids);
if (!empty($ids)) {
$url = Url::fromRoute('advance_script_manager.delete_multiple_form', ['ids' => $ids]);
$form_state->setRedirectUrl($url);
}
else {
$this->messenger->addError($this->t('No items selected'));
}
}
}
......@@ -17,7 +17,7 @@ class ScriptsFormDelete extends ConfirmFormBase {
protected $database;
/**
* @inheritdoc
* {@inheritdoc}
*/
public function __construct(Connection $database) {
$this->database = $database;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment