Skip to content

Commit

Permalink
Merge pull request #238 from stooit/feature/d8-pagination
Browse files Browse the repository at this point in the history
Added early pagination support for D8.
  • Loading branch information
stooit authored Oct 9, 2020
2 parents 0cd37c5 + 2b978d5 commit bd52c60
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
35 changes: 35 additions & 0 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,41 @@ public function onOutput(QuantEvent $event) {
}
}

// Pagination support.
$document = new \DOMDocument();
@$document->loadHTML($content);
$xpath = new \DOMXPath($document);

/** @var \DOMElement $node */
$pager_operations = [];
// @todo: Make this xpath configurable.
// This supports the use case for core views output (mini and standard pager).
foreach ($xpath->query('//a[contains(@href,"page=") and (./span[contains(text(), "Next")])]') as $node) {
$original_href = $node->getAttribute('href');
if ($original_href[0] === '?') {
$new_href = strtok($path, '?') . $original_href;
}
else {
$new_href = $original_href;
}

$pager_operations[] = ['\Drupal\quant\Seed::exportRoute', [$new_href]];
}

if (!empty($pager_operations)) {
$batch = [
'title' => t('Exporting pagination page...'),
'init_message' => t('Commencing'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('An error occurred during processing'),
'finished' => '\Drupal\quant\Seed::finishedSeedCallback',
'operations' => $pager_operations,
];

batch_set($batch);
}


// @todo: Report on forms that need proxying (attachments.forms).
}

Expand Down
6 changes: 5 additions & 1 deletion modules/quant_cron/quant_cron.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ use Drupal\quant\Event\QuantCollectionEvents;

function quant_cron_cron() {

// Quant cron only supported via CLI.
if (PHP_SAPI != 'cli') {
return;
}

// Load the settings form.
$form_state = new FormState();
$form_state->setRebuild();
\Drupal::formBuilder()->buildForm('Drupal\quant_cron\Form\CronSettingsForm', $form_state);
$event_dispatcher = \Drupal::service('event_dispatcher');


$batch = [
'title' => t('Exporting to Quant...'),
'operations' => [],
Expand Down
10 changes: 9 additions & 1 deletion src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function collectRoutes(CollectRoutesEvent $event) {
}
}

if ($event->getFormState()->getValue('routes_textarea')) {
if ($event->getFormState()->getValue('routes')) {
foreach (explode(PHP_EOL, $event->getFormState()->getValue('routes_textarea')) as $route) {
if (strpos((trim($route)), '/') !== 0) {
continue;
Expand All @@ -218,12 +218,15 @@ public function collectRoutes(CollectRoutesEvent $event) {
$event->addRoute('/robots.txt');
}

$routes = [];
if ($event->getFormState()->getValue('views_pages')) {
$views_storage = $this->entityTypeManager->getStorage('view');
$anon = User::getAnonymousUser();

foreach ($views_storage->loadMultiple() as $view) {
$view = Views::getView($view->get('id'));

$paths = [];
$displays = array_keys($view->storage->get('display'));
foreach ($displays as $display) {
$view->setDisplay($display);
Expand All @@ -233,6 +236,11 @@ public function collectRoutes(CollectRoutesEvent $event) {
continue;
}

if (in_array($path, $paths)) {
continue;
}

$paths[] = $path;
$event->addRoute("/{$path}");

// Languge negotiation may also provide path prefixes.
Expand Down
14 changes: 3 additions & 11 deletions src/Form/SeedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'checkbox',
'#title' => $this->t('Custom routes'),
'#description' => $this->t('Exports custom list of routes.'),
'#default_value' => !empty($config->get('routes_export', '')),
'#default_value' => $config->get('routes'),
];

$form['routes_textarea'] = [
Expand All @@ -196,7 +196,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
':input[name="routes"]' => ['checked' => TRUE],
],
],
'#default_value' => $config->get('routes_export', ''),
'#default_value' => $config->get('routes_export'),
];

$form['robots'] = [
Expand Down Expand Up @@ -256,15 +256,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}

$config->set('routes_export', $form_state->getValue('routes_textarea'))->save();
if ($form_state->getValue('routes_textarea')) {
foreach (explode(PHP_EOL, $form_state->getValue('routes_textarea')) as $route) {
if (strpos((trim($route)), '/') !== 0) {
continue;
}

$routes[] = trim($route);
}
}
$config->set('routes', $form_state->getValue('routes'))->save();

$batch = [
'title' => t('Exporting to Quant...'),
Expand Down

0 comments on commit bd52c60

Please sign in to comment.