Skip to content

Commit

Permalink
Merge pull request #20 from quantcdn/feature/revision-history-fix
Browse files Browse the repository at this point in the history
Revision history fix
  • Loading branch information
steveworley authored Jul 19, 2021
2 parents afca05f + 7e23d39 commit 2f4c494
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Commands/QuantDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function prepare($options = ['reset' => 'true']) {
$dispatcher->dispatch(QuantCollectionEvents::REDIRECTS, $event);
}

if ($form_state->getValue('entity_node')) {
if ($form_state->getValue('entity_node') || $form_state->getValue('entity_node_revisions')) {
$event = new CollectEntitiesEvent($form_state);
$dispatcher->dispatch(QuantCollectionEvents::ENTITIES, $event);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Event/CollectEntitiesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function includeRevisions() {
return (bool) $this->getFormState()->getValue('entity_node_revisions');
}

/**
* Determine if should seed the latest revision.
*
* @return bool
* Include latest revision or not.
*/
public function includeLatest() {
return (bool) $this->getFormState()->getValue('entity_node');
}

}
19 changes: 12 additions & 7 deletions src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,14 @@ public function collectEntities(CollectEntitiesEvent $event) {
}

$entities = $query->execute();
$revisions = $event->includeRevisions();
$includeLatest = $event->includeLatest();
$includeRevisions = $event->includeRevisions();

// Add the latest node to the batch.
foreach ($entities as $vid => $nid) {
$filter = $event->getFormState()->getValue('entity_node_languages');
$event->queueItem([
'id' => $nid,
'vid' => $vid,
'lang_filter' => $filter,
]);

if ($revisions) {
if ($includeRevisions) {
$entity = Node::load($nid);
$vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($entity);
$vids = array_diff($vids, [$vid]);
Expand All @@ -99,6 +95,15 @@ public function collectEntities(CollectEntitiesEvent $event) {
}
$entity = NULL;
}

// Include latest revisions.
if ($includeLatest) {
$event->queueItem([
'id' => $nid,
'vid' => $vid,
'lang_filter' => $filter,
]);
}
}
}

Expand Down
74 changes: 58 additions & 16 deletions src/Form/SeedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,34 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'checkbox',
'#title' => $this->t('Nodes'),
'#description' => $this->t('Exports the latest revision of each node.'),
'#states' => [
'disabled' => [
':input[name="entity_node_revisions"]' => ['checked' => TRUE],
],
'unchecked' => [
':input[name="entity_node_revisions"]' => ['checked' => TRUE],
],
],
'#default_value' => $seed_config->get('entity_node'),
];

$form['entity_node_disabled_explainer'] = [
'#type' => 'container',
'#markup' => $this->t('Push revision history independently of published revisions for best results.'),
'#states' => [
'visible' => [
':input[name="entity_node_revisions"]' => ['checked' => TRUE],
],
],
];

$form['entity_node_revisions'] = [
'#type' => 'checkbox',
'#title' => $this->t('Nodes (revision history)'),
'#description' => $this->t('Exports the historic revision history for nodes. <em>Note: You should only perform this operation this once.</em>'),
'#default_value' => $seed_config->get('entity_node_revisions'),
];

// Seed by language.
// Only active if there are more than one active languages.
$languages = \Drupal::languageManager()->getLanguages();
Expand All @@ -125,7 +150,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#options' => $language_codes,
'#states' => [
'visible' => [
':input[name="entity_node"]' => ['checked' => TRUE],
[
':input[name="entity_node"]' => ['checked' => TRUE],
],
[
':input[name="entity_node_revisions"]' => ['checked' => TRUE],
],
],
],
'#default_value' => $seed_config->get('entity_node_languages') ?: [],
Expand All @@ -149,24 +179,17 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#options' => $content_types,
'#states' => [
'visible' => [
':input[name="entity_node"]' => ['checked' => TRUE],
[
':input[name="entity_node"]' => ['checked' => TRUE],
],
[
':input[name="entity_node_revisions"]' => ['checked' => TRUE],
],
],
],
'#default_value' => $seed_config->get('entity_node_bundles') ?: [],
];

$form['entity_node_revisions'] = [
'#type' => 'checkbox',
'#title' => $this->t('All revisions'),
'#description' => $this->t('Exports all historic revisions.'),
'#states' => [
'visible' => [
':input[name="entity_node"]' => ['checked' => TRUE],
],
],
'#default_value' => $seed_config->get('entity_node_revisions'),
];

$form['entity_taxonomy_term'] = [
'#type' => 'checkbox',
'#title' => $this->t('Taxonomy terms'),
Expand Down Expand Up @@ -244,9 +267,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'actions',
];

$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#op' => 'save',
'#attributes' => [
'class' => ['button--primary'],
],
];

$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Queue'),
'#value' => $this->t('Save and Queue'),
'#op' => 'queue',
'#attributes' => [
'class' => ['button--secondary'],
],
];

return $form;
Expand All @@ -263,6 +299,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory->getEditable('quant_api.settings');
$trigger = $form_state->getTriggeringElement();

$this->configFactory->getEditable(static::SETTINGS)
->set('entity_node', $form_state->getValue('entity_node'))
Expand All @@ -279,6 +316,11 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('lunr', $form_state->getValue('lunr'))
->save();

if (isset($trigger['#op']) && $trigger['#op'] == 'save') {
\Drupal::messenger()->addStatus(t('Successfully updated configuration.'));
return;
}

if ($config->get('api_token')) {
if (!$project = $this->client->ping()) {
\Drupal::messenger()->addError(t('Unable to connect to Quant API, check settings.'));
Expand Down Expand Up @@ -318,7 +360,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$this->dispatcher->dispatch(QuantCollectionEvents::REDIRECTS, $event);
}

if ($form_state->getValue('entity_node')) {
if ($form_state->getValue('entity_node') || $form_state->getValue('entity_node_revisions')) {
$event = new CollectEntitiesEvent($form_state);
$this->dispatcher->dispatch(QuantCollectionEvents::ENTITIES, $event);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Quant/Metadata/Published.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function build(EntityInterface $entity) : array {
// node. This should be used to make content non-viewable if it is
// unpublished.
// Return the published status of the revision.
return ['published' => $entity->isPublished()];
return ['published' => $entity->isPublished() && $entity->isDefaultRevision()];
}

}

0 comments on commit 2f4c494

Please sign in to comment.