ConfigPagesClearConfirmationForm.php in Config Pages 8.3
File
src/Form/ConfigPagesClearConfirmationForm.php
View source
<?php
namespace Drupal\config_pages\Form;
use Drupal\config_pages\Entity\ConfigPages;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\field\FieldConfigInterface;
class ConfigPagesClearConfirmationForm extends ConfirmFormBase {
public function getFormId() {
return 'config_pages_clear_confirmation_form';
}
public function getQuestion() {
return t('Do you want to delete %id?', [
'%id' => $this->id,
]);
}
public function getCancelUrl() {
$entity_id = $this->id;
$entity = ConfigPages::load($entity_id);
return $entity
->toUrl();
}
public function getDescription() {
return t('Only do this if you are sure!');
}
public function getConfirmText() {
return $this
->t('Clear it Now!');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
$this->id = $id;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_id = $this->id;
$entity = ConfigPages::load($entity_id);
$fields = $entity
->getFieldDefinitions();
foreach ($fields as $name => $field) {
if ($field instanceof FieldConfigInterface) {
$entity
->set($name, $field
->getDefaultValue($entity));
}
}
$entity
->save();
$form_state
->setRedirectUrl($entity
->toUrl());
}
}