DataPolicyRevisionEdit.php in Data Policy 8
File
src/Form/DataPolicyRevisionEdit.php
View source
<?php
namespace Drupal\data_policy\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class DataPolicyRevisionEdit extends DataPolicyForm {
protected $request;
private $revisionId;
protected $entityTypeManager;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, Request $request, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->moduleHandler = $module_handler;
$this->configFactory = $config_factory;
$this->request = $request;
$this->revisionId = $this->request
->get('data_policy_revision');
$this->entityTypeManager = $entity_type_manager;
$this->entity = $this->entityTypeManager
->getStorage('data_policy');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('module_handler'), $container
->get('config.factory'), $container
->get('request_stack')
->getCurrentRequest(), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'data_policy_data_policy_revision_edit';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->entity = $this->entityTypeManager
->getStorage('data_policy')
->loadRevision($this->revisionId);
$form = parent::buildForm($form, $form_state);
$form['active_revision']['#default_value'] = $this->entity
->isDefaultRevision();
$form['active_revision']['#disabled'] = $this->entity
->isDefaultRevision();
$form['new_revision']['#default_value'] = FALSE;
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
if (!empty($form_state
->getValue('active_revision')) && !$this->entity
->isDefaultRevision()) {
$this->entity
->isDefaultRevision(TRUE);
$config = $this->configFactory
->getEditable('data_policy.data_policy');
$ids = $config
->get('revision_ids');
$ids[$this->entity
->getRevisionId()] = TRUE;
$config
->set('revision_ids', $ids)
->save();
}
$this->entity
->save();
$this
->messenger()
->addStatus($this
->t('Saved revision.'));
$form_state
->setRedirect('entity.data_policy.version_history', [
'entity_id' => $this->entity
->id(),
]);
}
public function clearMessage() {
return FALSE;
}
}