ILTDeleteForm.php in Opigno Instructor-led Trainings 8
File
src/Form/ILTDeleteForm.php
View source
<?php
namespace Drupal\opigno_ilt\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\opigno_ilt\Entity\ILT;
use Drupal\Core\Form\ConfirmFormHelper;
class ILTDeleteForm extends ContentEntityConfirmFormBase {
private $hasResults;
private $hasTraining;
private function _hasResults() {
$ilt = ILT::load($this->entity
->id());
if ($ilt
->getResultsIds()) {
return TRUE;
}
return FALSE;
}
private function _hasTraining() {
$training = $this->entity
->getTrainingId();
return (bool) $training;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->hasResults = $this
->_hasResults();
$this->hasTraining = $this
->_hasTraining();
$form = parent::buildForm($form, $form_state);
return $form;
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the %ilt Instructor-Led Training?', [
'%ilt' => $this->entity
->label(),
]);
}
public function getDescription() {
if ($this->hasResults) {
return $this
->t('There are some results for this ILT, and it cannot consequently be deleted in order to keep the users achievements');
}
if ($this->hasTraining) {
return $this
->t('This ILT is being used and it needs to be removed from the training(s) using it before being able to delete it.');
}
return $this
->t('This action cannot be undone.');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return Url::fromRoute('entity.opigno_ilt.canonical', [
'opigno_ilt' => $this->entity
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addMessage($this
->t('The Instructor-Led Training %ilt has been deleted.', [
'%ilt' => $this->entity
->label(),
]));
}
protected function actions(array $form, FormStateInterface $form_state) {
$attributes = [];
if ($this->hasResults || $this->hasTraining) {
$attributes = [
'disabled' => 'disabled',
];
}
return [
'submit' => [
'#type' => 'submit',
'#attributes' => $attributes,
'#value' => $this
->getConfirmText(),
'#submit' => [
[
$this,
'submitForm',
],
],
],
'cancel' => ConfirmFormHelper::buildCancelLink($this, $this
->getRequest()),
];
}
}