ConsentAgreementForm.php in General Data Protection Regulation 8
File
modules/gdpr_consent/src/Form/ConsentAgreementForm.php
View source
<?php
namespace Drupal\gdpr_consent\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConsentAgreementForm extends ContentEntityForm {
protected $currentUser;
public function __construct(EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
parent::__construct($entity_manager, $entity_type_bundle_info, $time);
$this->entityManager = $entity_manager;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('current_user'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if (!$this->entity
->isNew()) {
$form['new_revision'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create new revision'),
'#default_value' => TRUE,
'#weight' => 10,
];
}
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
if (!$form_state
->isValueEmpty('new_revision') && $form_state
->getValue('new_revision') != FALSE) {
$entity
->setNewRevision();
$entity
->setRevisionCreationTime(REQUEST_TIME);
$entity
->setRevisionUserId($this->currentUser
->id());
}
else {
$entity
->setNewRevision(FALSE);
}
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Consent Agreement.', [
'%label' => $entity
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Consent Agreement.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.gdpr_consent_agreement.canonical', [
'gdpr_consent_agreement' => $entity
->id(),
]);
}
protected function showRevisionUi() {
return FALSE;
}
}