class ReplicationForm in Deploy - Content Staging 8
Form controller for Replication edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\deploy\Entity\Form\ReplicationForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ReplicationForm
File
- src/
Entity/ Form/ ReplicationForm.php, line 21
Namespace
Drupal\deploy\Entity\FormView source
class ReplicationForm extends ContentEntityForm {
/**
* A source object.
*
* @var \Drupal\workspace\WorkspacePointerInterface
*/
protected $source = NULL;
/**
* A target object.
*
* @var \Drupal\workspace\WorkspacePointerInterface
*/
protected $target = NULL;
/**
* Format the title of the Replication process.
*
* @return string
* The title formatted with source and target labels.
*/
public function addTitle() {
$this
->setEntity(Replication::create());
if (!$this
->getDefaultSource() || !$this
->getDefaultTarget()) {
return $this
->t('Error');
}
$target = $this
->getDefaultTarget();
$remote_name = '';
if (!empty($target->remote_database->value) && !empty($target->remote_pointer->entity)) {
$target_workspace_name = $target->remote_database->value;
$remote_name = $target->remote_pointer->entity
->label();
$target_workspace_name = ucfirst($target_workspace_name);
$message = 'Deploy changes from local @source workspace to @target workspace on @remote';
}
else {
$target_workspace_name = $this
->getDefaultTarget()
->label();
$message = 'Deploy changes from local @source workspace to @target workspace';
}
return $this
->t($message, [
'@source' => $this
->getDefaultSource()
->label(),
'@target' => $target_workspace_name,
'@remote' => $remote_name,
]);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
$js = isset($input['_drupal_ajax']) ? TRUE : FALSE;
$form = parent::buildForm($form, $form_state);
$default_source = $this
->getDefaultSource();
$default_target = $this
->getDefaultTarget();
// For remote targets the remote workspace can't be loaded from workspace
// pointer.
$published = NULL;
/** @var \Drupal\multiversion\Entity\WorkspaceInterface|NULL $target_workspace */
if ($target_workspace = $default_target
->get('workspace_pointer')->entity) {
$published = $target_workspace
->isPublished();
}
if (!$default_source || !$default_target || $published === FALSE) {
$message = $this
->t('Source and target must be set, make sure your current workspace has an upstream. Go to <a href=":path">this page</a> to edit your workspaces.', [
':path' => Url::fromRoute('entity.workspace.collection')
->toString(),
]);
if ($js) {
return [
'#markup' => $message,
];
}
$this
->messenger()
->addError($message);
return [];
}
// @todo Move this to be injected.
$this->conflictTracker = \Drupal::service('workspace.conflict_tracker');
// Allow the user to not abort on conflicts.
$source_workspace = $default_source
->getWorkspace();
$conflicts = $this->conflictTracker
->useWorkspace($source_workspace)
->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' => $source_workspace
->id(),
])
->toString(),
':target' => $default_target
->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.');
}
if (!$source_workspace
->isDefaultWorkspace()) {
$form['archive'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Archive workspace after deployment'),
'#description' => $this
->t('The workspace will be archived only if the deployment finishes with success.'),
'#default_value' => FALSE,
];
}
$form['source']['widget']['#default_value'] = [
$default_source
->id(),
];
if (empty($this->entity
->get('target')->target_id) && $default_target) {
$form['target']['widget']['#default_value'] = [
$default_target
->id(),
];
}
if (!$form['source']['#access'] && !$form['target']['#access']) {
$form['actions']['submit']['#value'] = $this
->t('Deploy to @target', [
'@target' => $default_target
->label(),
]);
}
else {
$form['actions']['submit']['#value'] = $this
->t('Deploy');
}
$form['actions']['submit']['#ajax'] = [
'callback' => [
$this,
'deploy',
],
'event' => 'mousedown',
'prevent' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Deploying',
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
// Pass the abort flag to the ReplicationManager using runtime-only state,
// i.e. a static.
// @see \Drupal\workspace\ReplicatorManager
// @see \Drupal\workspace\Entity\Form\WorkspaceForm
$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);
parent::save($form, $form_state);
$input = $form_state
->getUserInput();
$js = isset($input['_drupal_ajax']) ? TRUE : FALSE;
try {
$response = \Drupal::service('workspace.replicator_manager')
->replicate($this->entity
->get('source')->entity, $this->entity
->get('target')->entity, null, $this->entity);
if ($response instanceof ReplicationLogInterface && $response
->get('ok')->value == TRUE) {
$this->entity
->set('replicated', REQUEST_TIME)
->save();
$this
->messenger()
->addMessage($this
->t('Deployment queued, refresh this page and check the status below. It might take a few minutes to complete.'));
if ($form_state
->hasValue('archive') && $form_state
->getValue('archive') == TRUE) {
$this->entity
->setArchiveSource()
->save();
}
if (!$js) {
$form_state
->setRedirect('entity.replication.collection');
}
}
else {
$this
->messenger()
->addError('Deployment error. Check recent log messages for more details.', 'error');
}
} catch (\Exception $e) {
watchdog_exception('Deploy', $e);
$this
->messenger()
->addError($e
->getMessage(), 'error');
}
if (!$js) {
$form_state
->setRedirect('entity.replication.collection');
}
}
/**
* Main method to return an Ajax Response.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* AjaxResponse object.
*/
public function deploy() {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new RedirectCommand(Url::fromRoute('entity.replication.collection')
->setAbsolute()
->toString()));
return $response;
}
/**
* Returns a source object.
*/
protected function getDefaultSource() {
if (!empty($this->source)) {
return $this->source;
}
if (!empty($this->entity
->get('source')) && $this->entity
->get('source')->entity instanceof WorkspacePointerInterface) {
return $this->source = $this->entity
->get('source')->entity;
}
/* @var \Drupal\multiversion\Entity\Workspace $workspace */
$workspace = \Drupal::service('workspace.manager')
->getActiveWorkspace();
$workspace_pointers = \Drupal::service('entity_type.manager')
->getStorage('workspace_pointer')
->loadByProperties([
'workspace_pointer' => $workspace
->id(),
]);
return $this->source = reset($workspace_pointers);
}
/**
* Returns a target object.
*/
protected function getDefaultTarget() {
if (!empty($this->target)) {
return $this->target;
}
if (!empty($this->entity
->get('target')) && $this->entity
->get('target')->entity instanceof WorkspacePointerInterface) {
return $this->target = $this->entity
->get('target')->entity;
}
/* @var \Drupal\multiversion\Entity\Workspace $workspace */
$workspace = \Drupal::service('workspace.manager')
->getActiveWorkspace();
return $this->target = $workspace
->get('upstream')->entity;
}
/**
* Generate a message render array with the given text.
*
* @param string $type
* The type of message: status, warning, or error.
* @param string $message
* The message to create with.
*
* @return array
* The render array for a status message.
*
* @see \Drupal\Core\Render\Element\StatusMessages
*/
protected function generateMessageRenderArray($type, $message) {
return [
'#theme' => 'status_messages',
'#message_list' => [
$type => [
Markup::create($message),
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentEntityForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
9 |
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
3 |
ContentEntityForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
ContentEntityForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
9 |
ContentEntityForm:: |
protected | function | Flags violations for the current form. | 4 |
ContentEntityForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
13 |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | 4 |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
ContentEntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
3 |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. | |
ContentEntityForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-level… Overrides FormBase:: |
3 |
ContentEntityForm:: |
public | function | Constructs a ContentEntityForm object. | 9 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
ReplicationForm:: |
protected | property | A source object. | |
ReplicationForm:: |
protected | property | A target object. | |
ReplicationForm:: |
public | function | Format the title of the Replication process. | |
ReplicationForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
ReplicationForm:: |
public | function | Main method to return an Ajax Response. | |
ReplicationForm:: |
protected | function | Generate a message render array with the given text. | |
ReplicationForm:: |
protected | function | Returns a source object. | |
ReplicationForm:: |
protected | function | Returns a target object. | |
ReplicationForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |