ContactFormDisableForm.php in Contact Storage 8
File
src/Form/ContactFormDisableForm.php
View source
<?php
namespace Drupal\contact_storage\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class ContactFormDisableForm extends EntityConfirmFormBase {
public function getQuestion() {
return $this
->t('Are you sure you want to disable the contact form %form?', [
'%form' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.contact_form.collection');
}
public function getConfirmText() {
return $this
->t('Disable');
}
public function getDescription() {
return $this
->t('Disabled contact forms are not displayed. This action can be undone from the contact forms administration page.');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['contact_storage_disabled_form_message'] = [
'#type' => 'textfield',
'#title' => t('Default disabled contact form message'),
'#description' => t('Default message to display if the contact form is disabled. It will be saved when clicking "Disable".'),
'#default_value' => $this
->getEntity()
->getThirdPartySetting('contact_storage', 'disabled_form_message', $this
->t('This contact form has been disabled.')),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->setThirdPartySetting('contact_storage', 'disabled_form_message', $form_state
->getValue('contact_storage_disabled_form_message'));
$this->entity
->disable()
->save();
$this
->messenger()
->addStatus($this
->t('Disabled contact form %form.', [
'%form' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}