You are here

class WorkspaceMergeForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/Form/WorkspaceMergeForm.php \Drupal\workspaces\Form\WorkspaceMergeForm

Provides a form that merges the contents for a workspace into another one.

Hierarchy

Expanded class hierarchy of WorkspaceMergeForm

1 string reference to 'WorkspaceMergeForm'
workspaces.routing.yml in core/modules/workspaces/workspaces.routing.yml
core/modules/workspaces/workspaces.routing.yml

File

core/modules/workspaces/src/Form/WorkspaceMergeForm.php, line 17

Namespace

Drupal\workspaces\Form
View source
class WorkspaceMergeForm extends ConfirmFormBase implements WorkspaceFormInterface, ContainerInjectionInterface {

  /**
   * The source workspace entity.
   *
   * @var \Drupal\workspaces\WorkspaceInterface
   */
  protected $sourceWorkspace;

  /**
   * The target workspace entity.
   *
   * @var \Drupal\workspaces\WorkspaceInterface
   */
  protected $targetWorkspace;

  /**
   * The workspace operation factory.
   *
   * @var \Drupal\workspaces\WorkspaceOperationFactory
   */
  protected $workspaceOperationFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new WorkspaceMergeForm.
   *
   * @param \Drupal\workspaces\WorkspaceOperationFactory $workspace_operation_factory
   *   The workspace operation factory service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(WorkspaceOperationFactory $workspace_operation_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->workspaceOperationFactory = $workspace_operation_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('workspaces.operation_factory'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'workspace_merge_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, WorkspaceInterface $source_workspace = NULL, WorkspaceInterface $target_workspace = NULL) {
    $this->sourceWorkspace = $source_workspace;
    $this->targetWorkspace = $target_workspace;
    $form = parent::buildForm($form, $form_state);
    $workspace_merger = $this->workspaceOperationFactory
      ->getMerger($this->sourceWorkspace, $this->targetWorkspace);
    $args = [
      '%source_label' => $this->sourceWorkspace
        ->label(),
      '%target_label' => $this->targetWorkspace
        ->label(),
    ];

    // List the changes that can be merged into the target.
    if ($source_rev_diff = $workspace_merger
      ->getDifferringRevisionIdsOnSource()) {
      $total_count = $workspace_merger
        ->getNumberOfChangesOnSource();
      $form['merge'] = [
        '#theme' => 'item_list',
        '#title' => $this
          ->formatPlural($total_count, 'There is @count item that can be merged from %source_label to %target_label', 'There are @count items that can be merged from %source_label to %target_label', $args),
        '#items' => [],
        '#total_count' => $total_count,
      ];
      foreach ($source_rev_diff as $entity_type_id => $revision_difference) {
        $form['merge']['#items'][$entity_type_id] = $this->entityTypeManager
          ->getDefinition($entity_type_id)
          ->getCountLabel(count($revision_difference));
      }
    }

    // If there are no changes to merge, show an informational message.
    if (!isset($form['merge'])) {
      $form['description'] = [
        '#markup' => $this
          ->t('There are no changes that can be merged from %source_label to %target_label.', $args),
      ];
      $form['actions']['submit']['#disabled'] = TRUE;
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Would you like to merge the contents of the %source_label workspace into %target_label?', [
      '%source_label' => $this->sourceWorkspace
        ->label(),
      '%target_label' => $this->targetWorkspace
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->t('Merge workspace contents.');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return Url::fromRoute('entity.workspace.collection', [], [
      'query' => \Drupal::destination()
        ->getAsArray(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->workspaceOperationFactory
      ->getMerger($this->sourceWorkspace, $this->targetWorkspace)
      ->merge();
    $this
      ->messenger()
      ->addMessage($this
      ->t('The contents of the %source_label workspace have been merged into %target_label.', [
      '%source_label' => $this->sourceWorkspace
        ->label(),
      '%target_label' => $this->targetWorkspace
        ->label(),
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 2
ConfirmFormBase::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText 22
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkspaceMergeForm::$entityTypeManager protected property The entity type manager.
WorkspaceMergeForm::$sourceWorkspace protected property The source workspace entity.
WorkspaceMergeForm::$targetWorkspace protected property The target workspace entity.
WorkspaceMergeForm::$workspaceOperationFactory protected property The workspace operation factory.
WorkspaceMergeForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
WorkspaceMergeForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
WorkspaceMergeForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
WorkspaceMergeForm::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormBase::getDescription
WorkspaceMergeForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
WorkspaceMergeForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
WorkspaceMergeForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
WorkspaceMergeForm::__construct public function Constructs a new WorkspaceMergeForm.