MeetingDeleteForm.php in Opigno Moxtra 8
File
src/Form/MeetingDeleteForm.php
View source
<?php
namespace Drupal\opigno_moxtra\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\ConfirmFormHelper;
class MeetingDeleteForm extends ContentEntityConfirmFormBase {
private $hasTraining;
private $hasResults;
private function _hasTraining() {
$training = $this->entity
->getTrainingId();
return (bool) $training;
}
private function _hasResults() {
$training = $this->entity
->getResultsIds();
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 getDescription() {
if ($this->hasResults) {
return $this
->t('There are some results for this Live Meeting, and it cannot consequently be deleted in order to keep the users achievements');
}
if ($this->hasTraining) {
return $this
->t('This Live Meeting 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.');
}
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()),
];
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the %meeting Live Meeting?', [
'%meeting' => $this->entity
->label(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return Url::fromRoute('entity.opigno_moxtra_meeting.canonical', [
'opigno_moxtra_meeting' => $this->entity
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addMessage($this
->t('The Live Meeting %meeting has been deleted.', [
'%meeting' => $this->entity
->label(),
]));
}
}