View source
<?php
namespace Drupal\workspace\Entity\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityConstraintViolationListInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\multiversion\Workspace\ConflictTrackerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceForm extends ContentEntityForm {
protected $conflictTracker;
protected $entity;
public function __construct(EntityManagerInterface $entity_manager, ConflictTrackerInterface $conflict_tracker, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
parent::__construct($entity_manager, $entity_type_bundle_info);
$this->conflictTracker = $conflict_tracker;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('workspace.conflict_tracker'), $container
->get('entity_type.bundle.info'));
}
public function form(array $form, FormStateInterface $form_state) {
$workspace = $this->entity;
if ($this->operation == 'edit') {
$this->conflictTracker
->useWorkspace($workspace);
$conflicts = $this->conflictTracker
->getAll();
if ($conflicts) {
$form['message'] = $this
->generateMessageRenderArray('error', $this
->t('There are <a href=":link">@count conflict(s) with the :target workspace</a>. Pushing changes to :target may result in unexpected behavior or data loss, and cannot be undone. Please proceed with caution.', [
'@count' => count($conflicts),
':link' => Url::fromRoute('entity.workspace.conflicts', [
'workspace' => $workspace
->id(),
])
->toString(),
':target' => $workspace
->get('upstream')->entity ? $workspace
->get('upstream')->entity
->label() : '',
]));
$form['is_aborted_on_conflict'] = [
'#type' => 'radios',
'#title' => $this
->t('Abort if there are conflicts?'),
'#default_value' => 'true',
'#options' => [
'true' => $this
->t('Yes, if conflicts are found do not replicate to upstream.'),
'false' => $this
->t('No, go ahead and push any conflicts to the upstream.'),
],
'#weight' => 0,
];
}
else {
$form['message'] = $this
->generateMessageRenderArray('status', 'There are no conflicts.');
}
$form['#title'] = $this
->t('Edit workspace %label', [
'%label' => $workspace
->label(),
]);
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 128,
'#default_value' => $workspace
->label(),
'#description' => $this
->t("Label for the Workspace."),
'#required' => TRUE,
'#weight' => 1,
];
$form['machine_name'] = [
'#type' => 'machine_name',
'#required' => TRUE,
'#title' => $this
->t('Workspace Machine Name'),
'#maxlength' => 128,
'#default_value' => $workspace
->get('machine_name')->value,
'#machine_name' => [
'exists' => [
$this,
'exists',
],
],
];
return parent::form($form, $form_state, $workspace);
}
protected function getEditedFieldNames(FormStateInterface $form_state) {
return array_merge([
'label',
'machine_name',
], parent::getEditedFieldNames($form_state));
}
protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
$field_names = [
'label',
'machine_name',
];
foreach ($violations
->getByFields($field_names) as $violation) {
list($field_name) = explode('.', $violation
->getPropertyPath(), 2);
$form_state
->setErrorByName($field_name, $violation
->getMessage());
}
parent::flagViolations($violations, $form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$is_aborted_on_conflict = !$form_state
->hasValue('is_aborted_on_conflict') || $form_state
->getValue('is_aborted_on_conflict') === 'true';
drupal_static('workspace_is_aborted_on_conflict', $is_aborted_on_conflict);
$workspace = $this->entity;
$is_new = $workspace
->isNew();
$workspace
->save();
$info = [
'%info' => $workspace
->label(),
];
$context = [
'@type' => $workspace
->bundle(),
'%info' => $workspace
->label(),
];
$logger = $this
->logger('workspace');
$replication_status = drupal_static('publish_workspace_replication_status', NULL);
if ($replication_status !== NULL) {
$logger
->notice('@type: updated %info.', $context);
if ($replication_status == TRUE) {
drupal_set_message($this
->t('Workspace :source has been updated and changes were pushed to :target.', [
':source' => $workspace
->label(),
':target' => $workspace
->get('upstream')->entity
->label(),
]), 'status');
$form_state
->setValue('id', $workspace
->id());
$form_state
->set('id', $workspace
->id());
$redirect = $this
->currentUser()
->hasPermission('administer workspaces') ? $workspace
->toUrl('collection') : $workspace
->toUrl('canonical');
$form_state
->setRedirectUrl($redirect);
}
else {
$previous_workflow_state = drupal_static('publish_workspace_previous_state', NULL);
if ($previous_workflow_state === NULL) {
throw new \Exception('The publish_workspace_replication_status should be set.');
}
$workspace->moderation_state->target_id = $previous_workflow_state;
$workspace
->save();
$form_state
->setRebuild();
}
}
else {
if ($is_new) {
$logger
->notice('@type: added %info.', $context);
drupal_set_message($this
->t('Workspace %info has been created.', $info));
}
else {
$logger
->notice('@type: updated %info.', $context);
drupal_set_message($this
->t('Workspace %info has been updated.', $info));
}
if ($workspace
->id()) {
$form_state
->setValue('id', $workspace
->id());
$form_state
->set('id', $workspace
->id());
$redirect = $this
->currentUser()
->hasPermission('administer workspaces') ? $workspace
->toUrl('collection') : $workspace
->toUrl('canonical');
$form_state
->setRedirectUrl($redirect);
}
else {
drupal_set_message($this
->t('The workspace could not be saved.'), 'error');
$form_state
->setRebuild();
}
}
}
protected function generateMessageRenderArray($type, $message) {
return [
'#theme' => 'status_messages',
'#message_list' => [
$type => [
Markup::create($message),
],
],
];
}
public function exists($machine_name) {
return (bool) $this->entityTypeManager
->getStorage('workspace')
->getQuery()
->condition('machine_name', $machine_name)
->execute();
}
}