Skip to content

Commit

Permalink
Issue #379: Allow doing a full review without an edit
Browse files Browse the repository at this point in the history
  • Loading branch information
lkmorlan committed Mar 8, 2024
1 parent 6c8ad09 commit ef2ad7e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 39 deletions.
106 changes: 67 additions & 39 deletions html/modules/custom/bc_dc/src/Form/BcDcWorkflowBlockForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,48 @@ public function buildForm(array $form, FormStateInterface $form_state, $args = [
return $form;
}

$isPublished = $args['node']->isPublished();

// Message if revision is published.
if ($args['node']->isPublished()) {
if ($isPublished) {
$form['note'] = [
'#markup' => $this->t('Latest revision is published.'),
];
return $form;
}

// Prevent publishing when required fields are empty.
$empty_required = [];
foreach ($args['node']->getFields() as $field) {
$fieldDefinition = $field->getFieldDefinition();
if ($field->isEmpty() && $fieldDefinition->isRequired()) {
$empty_required[] = $fieldDefinition->getLabel();
else {
// Prevent publishing when required fields are empty.
$empty_required = [];
foreach ($args['node']->getFields() as $field) {
$fieldDefinition = $field->getFieldDefinition();
if ($field->isEmpty() && $fieldDefinition->isRequired()) {
$empty_required[] = $fieldDefinition->getLabel();
}
}
if ($empty_required) {
$form['empty_required'] = [
'#theme' => 'item_list',
'#items' => $empty_required,
'#prefix' => '<p>' . $this->t('The following fields must be completed before publishing:') . '</p>',
];
return $form;
}
}
if ($empty_required) {
$form['empty_required'] = [
'#theme' => 'item_list',
'#items' => $empty_required,
'#prefix' => '<p>' . $this->t('The following fields must be completed before publishing:') . '</p>',
];
return $form;
}

// Publishing block.
$form['revision_log_message'] = [
'#type' => 'textarea',
'#title' => $this->t('Revision note'),
];
// Publishing block.
$form['revision_log_message'] = [
'#type' => 'textarea',
'#title' => $this->t('Revision note'),
];

$form['major_edit'] = [
'#type' => 'radios',
'#required' => TRUE,
'#title' => $this->t('Edit type'),
'#options' => [
$this->t('Minor edit'),
$this->t('Major edit (notify subscribers)'),
],
];
$form['major_edit'] = [
'#type' => 'radios',
'#required' => TRUE,
'#title' => $this->t('Edit type'),
'#options' => [
$this->t('Minor edit'),
$this->t('Major edit (notify subscribers)'),
],
];
}

$form['full_review'] = [
'#type' => 'checkbox',
Expand All @@ -76,9 +78,21 @@ public function buildForm(array $form, FormStateInterface $form_state, $args = [
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Publish'),
'#value' => $isPublished ? $this->t('Update') : $this->t('Publish'),
'#button_type' => 'primary',
];
if ($isPublished) {
$form['actions']['submit']['#states'] = [
'invisible' => [
'input[id="edit-full-review"]' => ['checked' => FALSE],
],
];
}

$form['isPublished'] = [
'#type' => 'value',
'#value' => (int) $isPublished,
];

return $form;
}
Expand All @@ -91,6 +105,7 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
$revision_log_message = ($revision_log_message === '') ? NULL : $revision_log_message;

$full_review = (bool) $form_state->getValue('full_review');
$isPublished = (bool) $form_state->getValue('isPublished');

// Get the node.
$buildInfo = $form_state->getBuildInfo();
Expand All @@ -111,13 +126,26 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
if (is_string($revision_log_message)) {
$node->setRevisionLogMessage($revision_log_message);
}
// Set revision author to current.
$node->setRevisionUserId($this->currentUser()->id());
// Set published.
$node->set('moderation_state', 'published');
$node->save();

$this->messenger()->addMessage($this->t('Metadata record published.'));
if ($isPublished) {
// If it is published, all that can be done is update
// field_last_review_date which is done via field_is_complete_review.
if ($full_review) {
$node->save();
$this->messenger()->addMessage($this->t('Metadata record review date updated.'));
}
else {
$this->messenger()->addMessage($this->t('No changes made.'));
}
}
else {
// Set revision author to current.
$node->setRevisionUserId($this->currentUser()->id());
// Set published.
$node->set('moderation_state', 'published');
$node->save();
$this->messenger()->addMessage($this->t('Metadata record published.'));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,24 @@ public function test(): void {
$this->assertSession()->elementExists('xpath', '//div[contains(@class, "field--name-field-related-document")]//em[@class = "field-optional"][text() = "Optional"]');
// On Build page, no workflow block when latest revision is published.
$this->assertSession()->elementExists('xpath', '//div[contains(@class, "block-bc-dc-workflow-block")]//*[contains(text(), "Latest revision is published")]');

// The data_set has never had a full review.
$this->assertSession()->elementExists('xpath', '//div[contains(@class, "field--label-inline")][contains(@class, "field--name-field-last-review-date")][div[@class = "field__label"][text() = "Last review date"]]/div/em[text() = "Never"]');
// Submit a full review.
$edit = [
'edit-full-review' => '1',
];
$this->submitForm($edit, 'Update');
// Return to the Build page and the review date is now today.
$this->clickLink('Build');
$args = [
':class' => 'field--name-field-last-review-date',
':label' => 'Last review date',
':text' => date('Y-m-d'),
];
$xpath = $this->assertSession()->buildXPathQuery('//div[contains(@class, "field--label-inline")][contains(@class, :class)][div[@class = "field__label"][text() = :label]]/div/time[text() = :text]', $args);
$this->assertSession()->elementExists('xpath', $xpath);

// Set node/2 as a data_set used by this data_set.
$this->click('a[aria-label = "Edit Section 3"]');
$edit = [
Expand Down

0 comments on commit ef2ad7e

Please sign in to comment.