From 8eb9e6ffad042bc240d6e27cf6741748498a6adf Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Sat, 17 Jul 2021 09:15:26 +1200 Subject: [PATCH 1/4] Split published vs. historic node seeding. Form improvements to ensure historic seeding happens first. --- src/Commands/QuantDrushCommands.php | 2 +- src/Event/CollectEntitiesEvent.php | 10 +++++ src/EventSubscriber/CollectionSubscriber.php | 19 +++++--- src/Form/SeedForm.php | 46 ++++++++++++++------ src/Plugin/Quant/Metadata/Published.php | 2 +- 5 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/Commands/QuantDrushCommands.php b/src/Commands/QuantDrushCommands.php index 68189d47..de10fcd2 100644 --- a/src/Commands/QuantDrushCommands.php +++ b/src/Commands/QuantDrushCommands.php @@ -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); } diff --git a/src/Event/CollectEntitiesEvent.php b/src/Event/CollectEntitiesEvent.php index 5c9e13f1..cb3d087e 100644 --- a/src/Event/CollectEntitiesEvent.php +++ b/src/Event/CollectEntitiesEvent.php @@ -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'); + } + } diff --git a/src/EventSubscriber/CollectionSubscriber.php b/src/EventSubscriber/CollectionSubscriber.php index e8f47121..7d21cea2 100644 --- a/src/EventSubscriber/CollectionSubscriber.php +++ b/src/EventSubscriber/CollectionSubscriber.php @@ -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]); @@ -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, + ]); + } } } diff --git a/src/Form/SeedForm.php b/src/Form/SeedForm.php index 37a3e296..4ee4c375 100644 --- a/src/Form/SeedForm.php +++ b/src/Form/SeedForm.php @@ -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. Note: You should only perform this operation this once.'), + '#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(); @@ -149,24 +174,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'), @@ -318,7 +336,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); } diff --git a/src/Plugin/Quant/Metadata/Published.php b/src/Plugin/Quant/Metadata/Published.php index 03f977e0..94cbedff 100644 --- a/src/Plugin/Quant/Metadata/Published.php +++ b/src/Plugin/Quant/Metadata/Published.php @@ -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()]; } } From 007160381207d852bbe4fa1efb19fcab172e5c72 Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Sat, 17 Jul 2021 10:17:09 +1200 Subject: [PATCH 2/4] Lint fix. --- src/Form/SeedForm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/SeedForm.php b/src/Form/SeedForm.php index 4ee4c375..13926a8d 100644 --- a/src/Form/SeedForm.php +++ b/src/Form/SeedForm.php @@ -108,7 +108,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], 'unchecked' => [ ':input[name="entity_node_revisions"]' => ['checked' => TRUE], - ] + ], ], '#default_value' => $seed_config->get('entity_node'), ]; @@ -179,7 +179,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], [ ':input[name="entity_node_revisions"]' => ['checked' => TRUE], - ] + ], ], ], '#default_value' => $seed_config->get('entity_node_bundles') ?: [], From 4c29aaaa0f41655d9cac7a810eaf92c42522a616 Mon Sep 17 00:00:00 2001 From: Steve Worley Date: Sun, 18 Jul 2021 10:30:19 +1000 Subject: [PATCH 3/4] Add save button for configuration. --- src/Form/SeedForm.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Form/SeedForm.php b/src/Form/SeedForm.php index 13926a8d..4c2ccc56 100644 --- a/src/Form/SeedForm.php +++ b/src/Form/SeedForm.php @@ -262,9 +262,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; @@ -281,6 +294,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')) @@ -297,6 +311,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.')); From 7e23d39e1ef6aee47a36e449d07da4897ccbd366 Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Mon, 19 Jul 2021 14:04:49 +1200 Subject: [PATCH 4/4] Show language filter for revision history. --- src/Form/SeedForm.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Form/SeedForm.php b/src/Form/SeedForm.php index 4c2ccc56..30df59b8 100644 --- a/src/Form/SeedForm.php +++ b/src/Form/SeedForm.php @@ -150,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') ?: [],