DeleteBannerForm.php in Dynamic Banner 8
File
src/forms/DeleteBannerForm.php
View source
<?php
namespace Drupal\dynamic_banner\forms;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
class DeleteBannerForm extends ConfirmFormBase {
protected $bid;
public function getFormId() {
return "frm_deletebannerform";
}
public function getQuestion() {
return t('Are you sure you want to delete the banner?');
}
public function getCancelUrl() {
return FALSE;
}
public function getConfirmText() {
return t('Delete');
}
public function getCancelRoute() {
}
public function buildForm(array $form, FormStateInterface $form_state, $bid = NULL) {
$this->bid = $bid;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$status = $this
->dynamic_banner_admin_delete($this->bid);
\Drupal::logger('DynamicBanner')
->error('Deleted BD Contact Submission with id %id.', array(
'%id' => $this->bid,
));
if ($status == 0) {
$this
->messenger()
->addStatus(t('Error while deleting'));
}
else {
$this
->messenger()
->addStatus(t('Banner %id has been deleted.', array(
'%id' => $this->bid,
)));
}
$form_state
->setRedirect('cdb.listbanners');
}
public function dynamic_banner_admin_delete($dbid = 0) {
\Drupal::database()
->delete('dynamic_banner')
->condition('dbid', $dbid)
->execute();
$this
->messenger()
->addStatus(t('The banner has been deleted, the image still exists though'));
}
}