Commit c01dee41 authored by catch's avatar catch
Browse files

Issue #2998454 by amateescu, raman.b, Gold, dixon_, catch, Sam152: Make...

Issue #2998454 by amateescu, raman.b, Gold, dixon_, catch, Sam152: Make terminology regarding deploy/publish consistent
parent 03bf48df
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  config:
    - core.entity_form_mode.workspace.deploy
  module:
    - workspaces
id: workspace.workspace.deploy
targetEntityType: workspace
bundle: workspace
mode: deploy
content: {  }
hidden:
  parent: true
  uid: true
+0 −9
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - workspaces
id: workspace.deploy
label: Deploy
targetEntityType: workspace
cache: true
+0 −2
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@
 *       "edit" = "\Drupal\workspaces\Form\WorkspaceForm",
 *       "delete" = "\Drupal\workspaces\Form\WorkspaceDeleteForm",
 *       "activate" = "\Drupal\workspaces\Form\WorkspaceActivateForm",
 *       "deploy" = "\Drupal\workspaces\Form\WorkspaceDeployForm",
 *     },
 *   },
 *   admin_permission = "administer workspaces",
@@ -58,7 +57,6 @@
 *     "edit-form" = "/admin/config/workflow/workspaces/manage/{workspace}/edit",
 *     "delete-form" = "/admin/config/workflow/workspaces/manage/{workspace}/delete",
 *     "activate-form" = "/admin/config/workflow/workspaces/manage/{workspace}/activate",
 *     "deploy-form" = "/admin/config/workflow/workspaces/manage/{workspace}/deploy",
 *     "collection" = "/admin/config/workflow/workspaces",
 *   },
 * )
+157 −0
Original line number Diff line number Diff line
@@ -2,60 +2,53 @@

namespace Drupal\workspaces\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\workspaces\WorkspaceAccessException;
use Drupal\workspaces\WorkspaceInterface;
use Drupal\workspaces\WorkspaceOperationFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the workspace deploy form.
 * Provides the workspace publishing form.
 */
class WorkspaceDeployForm extends ContentEntityForm implements WorkspaceFormInterface {
class WorkspacePublishForm extends ConfirmFormBase implements WorkspaceFormInterface, ContainerInjectionInterface {

  /**
   * The workspace entity.
   * The workspace that will be published.
   *
   * @var \Drupal\workspaces\WorkspaceInterface
   */
  protected $entity;
  protected $workspace;

  /**
   * The messenger service.
   * The workspace operation factory.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   * @var \Drupal\workspaces\WorkspaceOperationFactory
   */
  protected $messenger;
  protected $workspaceOperationFactory;

  /**
   * The workspace operation factory.
   * The entity type manager.
   *
   * @var \Drupal\workspaces\WorkspaceOperationFactory
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $workspaceOperationFactory;
  protected $entityTypeManager;

  /**
   * Constructs a new WorkspaceDeployForm.
   * Constructs a new WorkspacePublishForm.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @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(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, MessengerInterface $messenger, WorkspaceOperationFactory $workspace_operation_factory) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->messenger = $messenger;
  public function __construct(WorkspaceOperationFactory $workspace_operation_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->workspaceOperationFactory = $workspace_operation_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
@@ -63,47 +56,58 @@ public function __construct(EntityRepositoryInterface $entity_repository, Entity
   */
  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('messenger'),
      $container->get('workspaces.operation_factory')
      $container->get('workspaces.operation_factory'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
  public function getFormId() {
    return 'workspace_publish_form';
  }

    $workspace_publisher = $this->workspaceOperationFactory->getPublisher($this->entity);
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, WorkspaceInterface $workspace = NULL) {
    $this->workspace = $workspace;

    $form = parent::buildForm($form, $form_state);

    $workspace_publisher = $this->workspaceOperationFactory->getPublisher($this->workspace);

    $args = [
      '%source_label' => $this->entity->label(),
      '%source_label' => $this->workspace->label(),
      '%target_label' => $workspace_publisher->getTargetLabel(),
    ];
    $form['#title'] = $this->t('Deploy %source_label workspace', $args);
    $form['#title'] = $this->t('Publish %source_label workspace', $args);

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

      $form['actions']['submit']['#value'] = $this->formatPlural($total_count, 'Publish @count item to @target', 'Publish @count items to @target', ['@target' => $workspace_publisher->getTargetLabel()]);
    }
    else {
      // If there are no changes to push or pull, show an informational message.
    if (!isset($form['deploy']) && !isset($form['refresh'])) {
      $form['help'] = [
        '#markup' => $this->t('There are no changes that can be deployed from %source_label to %target_label.', $args),
        '#markup' => $this->t('There are no changes that can be published from %source_label to %target_label.', $args),
      ];

      // Do not allow the 'Publish' operation if there's nothing to publish.
      $form['actions']['submit']['#value'] = $this->t('Publish');
      $form['actions']['submit']['#disabled'] = TRUE;
    }

    return $form;
@@ -112,53 +116,41 @@ public function form(array $form, FormStateInterface $form_state) {
  /**
   * {@inheritdoc}
   */
  public function actions(array $form, FormStateInterface $form_state) {
    $elements = parent::actions($form, $form_state);
    unset($elements['delete']);

    $workspace_publisher = $this->workspaceOperationFactory->getPublisher($this->entity);

    if (isset($form['deploy'])) {
      $total_count = $form['deploy']['#total_count'];
      $elements['submit']['#value'] = $this->formatPlural($total_count, 'Deploy @count item to @target', 'Deploy @count items to @target', ['@target' => $workspace_publisher->getTargetLabel()]);
      $elements['submit']['#submit'] = ['::submitForm', '::deploy'];
    }
    else {
      // Do not allow the 'Deploy' operation if there's nothing to push.
      $elements['submit']['#value'] = $this->t('Deploy');
      $elements['submit']['#disabled'] = TRUE;
  public function getQuestion() {
    return $this->t('Would you like to publish the contents of the %label workspace?', [
      '%label' => $this->workspace->label(),
    ]);
  }

    $elements['cancel'] = [
      '#type' => 'link',
      '#title' => $this->t('Cancel'),
      '#attributes' => ['class' => ['button']],
      '#url' => $this->entity->toUrl('collection'),
    ];
  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('Publish workspace contents.');
  }

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

  /**
   * Form submission handler; deploys the content to the workspace's target.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * {@inheritdoc}
   */
  public function deploy(array &$form, FormStateInterface $form_state) {
    $workspace = $this->entity;
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $workspace = $this->workspace;

    try {
      $workspace->publish();
      $this->messenger->addMessage($this->t('Successful deployment.'));
      $this->messenger()->addMessage($this->t('Successful publication.'));
    }
    catch (WorkspaceAccessException $e) {
      $this->messenger->addMessage($e->getMessage(), 'error');
      $this->messenger()->addMessage($e->getMessage(), 'error');
    }
    catch (\Exception $e) {
      $this->messenger->addMessage($this->t('Deployment failed. All errors have been logged.'), 'error');
      $this->messenger()->addMessage($this->t('Publication failed. All errors have been logged.'), 'error');
    }
  }

+13 −7
Original line number Diff line number Diff line
@@ -150,12 +150,15 @@ public function getDefaultOperations(EntityInterface $entity) {
    }

    if (!$entity->hasParent()) {
      $operations['deploy'] = [
        'title' => $this->t('Deploy content'),
        // The 'Deploy' operation should be the default one for the currently
      $operations['publish'] = [
        'title' => $this->t('Publish content'),
        // The 'Publish' operation should be the default one for the currently
        // active workspace.
        'weight' => ($active_workspace && $entity->id() == $active_workspace->id()) ? 0 : 20,
        'url' => $entity->toUrl('deploy-form', ['query' => ['destination' => $entity->toUrl('collection')->toString()]]),
        'url' => Url::fromRoute('entity.workspace.publish_form',
          ['workspace' => $entity->id()],
          ['query' => ['destination' => $entity->toUrl('collection')->toString()]]
        ),
      ];
    }
    else {
@@ -256,10 +259,13 @@ protected function offCanvasRender(array &$build) {
        ],
      ];
      if (!$active_workspace->hasParent()) {
        $build['active_workspace']['actions']['deploy'] = [
        $build['active_workspace']['actions']['publish'] = [
          '#type' => 'link',
          '#title' => $this->t('Deploy content'),
          '#url' => $active_workspace->toUrl('deploy-form', ['query' => ['destination' => $active_workspace->toUrl('collection')->toString()]]),
          '#title' => $this->t('Publish content'),
          '#url' => Url::fromRoute('entity.workspace.publish_form',
            ['workspace' => $active_workspace->id()],
            ['query' => ['destination' => $active_workspace->toUrl('collection')->toString()]]
          ),
          '#attributes' => [
            'class' => ['button', 'active-workspace__button'],
          ],
Loading