Skip to content

Commit

Permalink
Merge pull request #493 from UN-OCHA/rbaumbach/HPC-8842
Browse files Browse the repository at this point in the history
rbaumbach/HPC 8842
  • Loading branch information
berliner authored Nov 23, 2022
2 parents 7cd54c6 + 93e2c7b commit facede1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
38 changes: 38 additions & 0 deletions html/modules/custom/ghi_content/ghi_content.module
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ function ghi_content_form_node_article_edit_form_alter(array &$form, FormStateIn
],
]);
$form['remote_article_info']['link']['#weight'] = 4;

// Add a way to reset the article.
$form['remote_article_reset'] = [
'#type' => 'details',
'#title' => t('Reset article'),
'#description' => t('Reset the content paragraphs to the initial state. This will remove any customizations made to this article page and re-import all content paragraphs in the order defined on the remote system.'),
'#open' => FALSE,
'#group' => 'advanced',
];
$form['remote_article_reset']['reset'] = [
'#type' => 'submit',
'#value' => t('Reset now'),
'#submit' => ['ghi_content_form_node_article_edit_form_reset_submit'],
'#weight' => 2,
];
}
$form['#attached']['library'][] = 'ghi_content/admin.article_edit';
}
Expand Down Expand Up @@ -257,6 +272,29 @@ function ghi_content_form_node_article_edit_form_apply_changes_submit($form, For
$form_state->setRebuild();
}

/**
* Form submit handler for the "Reset article" button on article forms.
*/
function ghi_content_form_node_article_edit_form_reset_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\node\NodeForm $form_object */
$form_object = $form_state->getFormObject();
$node = $form_object->getEntity();

/** @var \Drupal\ghi_content\ContentManager\ArticleManager $article_manager */
$article_manager = \Drupal::getContainer()->get('ghi_content.manager.article');
$article_manager->updateNodeFromRemote($node, FALSE, TRUE);

// Save the node.
$node->save();

// The next thing we need to do after that is to update the migration state,
// so that this article is not wrongly treated as changed on the next
// migration run.
$article_manager->updateMigrationState($node);

$form_state->setRebuild();
}

/**
* Implements hook_ENTITY_TYPE_presave() for node entities.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,13 @@ private function removeMigrationMapEntries(NodeInterface $node) {
* The node object.
* @param bool $dry_run
* Whether the update should actually modify data.
* @param bool $reset
* Whether article node should be reset to it's original state (as if it
* would be created right now based on the configuration on the remote).
*
* @see ghi_content_node_presave()
*/
public function updateNodeFromRemote(NodeInterface $node, $dry_run = FALSE) {
public function updateNodeFromRemote(NodeInterface $node, $dry_run = FALSE, $reset = FALSE) {
$remote_field = self::REMOTE_ARTICLE_FIELD;
$article = $this->loadArticleForNode($node, TRUE);
if (!$article) {
Expand All @@ -514,7 +517,8 @@ public function updateNodeFromRemote(NodeInterface $node, $dry_run = FALSE) {
$article_id = $node->get($remote_field)->article_id;
$remote_source_original = $node->original ? $node->original->get($remote_field)->remote_source : NULL;
$article_id_original = $node->original ? $node->original->get($remote_field)->article_id : NULL;
$cleanup = $remote_source_original && $article_id_original && ($remote_source != $remote_source_original || $article_id != $article_id_original);
$changed_article = $remote_source_original && $article_id_original && ($remote_source != $remote_source_original || $article_id != $article_id_original);
$cleanup = $reset || $changed_article;

// Set the base properties.
$node->setTitle($article->getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ public function getDisaggregatedData($reporting_period = 'latest', $filter_empty
// No, so we need to do it now. Extract the base metrics and base data.
$base_metrics = $this->getBaseMetricTotals($reporting_period);
$base_data = $this->getBaseData($reporting_period);
if (empty($base_data)) {
return [];
}

// Remove the first row (full country totals, always empty for some reason
// and not part of the locations array anyway).
Expand Down

0 comments on commit facede1

Please sign in to comment.