Skip to content

Commit

Permalink
Added CollectTaxonomyTermsEvent and related code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 9, 2024
1 parent 61d0a10 commit 71c79ab
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 80 deletions.
56 changes: 22 additions & 34 deletions modules/quant_cron/quant_cron.module
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ function quant_cron_cron() {
}
}

// Add taxonomy terms.
if ($form_state->getValue('entity_taxonomy_term')) {
$query = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getQuery();

$terms = $query->accessCheck(TRUE)->execute();

foreach ($terms as $tid) {
$term = Term::load($tid);

foreach ($term->getTranslationLanguages() as $langcode => $language) {
Seed::seedTaxonomyTerm($term, $langcode);

\Drupal::logger('quant_cron')->notice("quant_cron sending term: tid: @tid, langcode: @lang",
[
'@tid' => $tid,
'@lang' => $langcode,
]
);
}
}
}

// Add custom routes.
if ($form_state->getValue('routes')) {
foreach (explode(PHP_EOL, $form_state->getValue('routes_textarea')) as $route) {
Expand Down Expand Up @@ -113,11 +135,6 @@ function quant_cron_cron() {
$routes = array_merge($routes, quant_cron_get_views_routes());
}

// Add taxonomy term routes.
if ($form_state->getValue('entity_taxonomy_term')) {
$routes = array_merge($routes, quant_cron_get_taxonomy_routes());
}

// Send files.
foreach ($files as $file) {
\Drupal::logger('quant_cron')->notice("quant_cron sending file: @route", ['@route' => $file]);
Expand All @@ -134,35 +151,6 @@ function quant_cron_cron() {

}

/**
* Helper: Get taxonomy routes.
*/
function quant_cron_get_taxonomy_routes() {

$paths = [];
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

foreach ($taxonomy_storage->loadMultiple() as $term) {
foreach ($term->getTranslationLanguages() as $langcode => $language) {
// Retrieve the translated version.
$term = $term->getTranslation($langcode);
$tid = $term->id();

$options = ['absolute' => FALSE];

if (!empty($langcode)) {
$language = \Drupal::languageManager()->getLanguage($langcode);
$options['language'] = $language;
}

$url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid], $options)->toString();
$paths[] = $url;
}
}

return $paths;
}

/**
* Helper: Get theme assets.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Commands/QuantDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\quant\Event\CollectFilesEvent;
use Drupal\quant\Event\CollectRedirectsEvent;
use Drupal\quant\Event\CollectRoutesEvent;
use Drupal\quant\Event\CollectTaxonomyTermsEvent;
use Drupal\quant\Event\QuantCollectionEvents;
use Drupal\quant\QuantQueueFactory;

Expand Down Expand Up @@ -211,6 +212,11 @@ public function prepare($options = ['reset' => 'true']) {
$dispatcher->dispatch($event, QuantCollectionEvents::ENTITIES);
}

if ($form_state->getValue('entity_taxonomy_term')) {
$event = new CollectTaxonomyTermsEvent($form_state);
$dispatcher->dispatch($event, QuantCollectionEvents::TAXONOMY_TERMS);
}

$event = new CollectRoutesEvent($form_state);
$dispatcher->dispatch($event, QuantCollectionEvents::ROUTES);

Expand Down
20 changes: 20 additions & 0 deletions src/Event/CollectTaxonomyTermsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\quant\Event;

use Drupal\quant\Plugin\QueueItem\TaxonomyTermItem;

/**
* Collect taxonomy terms event.
*
* This is triggered when we need to gather all taxonomy terms
* to export to Quant.
*/
class CollectTaxonomyTermsEvent extends ConfigFormEventBase {

/**
* {@inheritdoc}
*/
protected $queueItemClass = TaxonomyTermItem::class;

}
11 changes: 11 additions & 0 deletions src/Event/QuantCollectionEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ final class QuantCollectionEvents {
*/
const ENTITIES = 'quant.seed.entities';

/**
* Name of the event when collecting taxonomy terms.
*
* @Event
*
* @see Drupal\quant\Event\CollectTaxonomyTermsEvent
*
* @var string
*/
const TAXONOMY_TERMS = 'quant.seed.taxonomy_terms';

/**
* Name of the event when collecting files.
*
Expand Down
66 changes: 20 additions & 46 deletions src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\quant\Event\CollectFilesEvent;
use Drupal\quant\Event\CollectRedirectsEvent;
use Drupal\quant\Event\CollectRoutesEvent;
use Drupal\quant\Event\CollectTaxonomyTermsEvent;
use Drupal\quant\Event\QuantCollectionEvents;
use Drupal\quant\Plugin\QueueItem\RedirectItem;
use Drupal\user\Entity\User;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function __construct(EntityTypeManager $entity_type_manager, ConfigFactor
*/
public static function getSubscribedEvents() {
$events[QuantCollectionEvents::ENTITIES][] = ['collectEntities'];
$events[QuantCollectionEvents::TAXONOMY_TERMS][] = ['collectTaxonomyTerms'];
$events[QuantCollectionEvents::FILES][] = ['collectFiles'];
$events[QuantCollectionEvents::REDIRECTS][] = ['collectRedirects'];
$events[QuantCollectionEvents::ROUTES][] = ['collectRoutes'];
Expand All @@ -65,8 +67,6 @@ public static function getSubscribedEvents() {
*/
public function collectEntities(CollectEntitiesEvent $event) {
$query = $this->entityTypeManager->getStorage('node')->getQuery();
// @todo Skip unpublished content if disable_content_drafts is enabled.
$disable_drafts = $this->configFactory->get('quant.settings')->get('disable_content_drafts');

$bundles = $event->getFormState()->getValue('entity_node_bundles');

Expand Down Expand Up @@ -114,6 +114,20 @@ public function collectEntities(CollectEntitiesEvent $event) {
}
}

/**
* Collect taxonomy terms.
*/
public function collectTaxonomyTerms(CollectTaxonomyTermsEvent $event) {
$query = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery();
$terms = $query->accessCheck(TRUE)->execute();

foreach ($terms as $tid) {
$event->queueItem([
'tid' => $tid,
]);
}
}

/**
* Identify redirects.
*/
Expand Down Expand Up @@ -246,50 +260,6 @@ public function collectRoutes(CollectRoutesEvent $event) {
$event->queueItem(['route' => $page]);
}

// Add taxonomy term pages.
if ($event->getFormState()->getValue('entity_taxonomy_term')) {
$taxonomy_storage = $this->entityTypeManager->getStorage('taxonomy_term');

foreach ($taxonomy_storage->loadMultiple() as $term) {
if ($disable_drafts && !$term->isPublished()) {
continue;
}
foreach ($term->getTranslationLanguages() as $langcode => $language) {
// Retrieve the translated version.
$term = $term->getTranslation($langcode);
$tid = $term->id();

$options = ['absolute' => FALSE];

if (!empty($langcode)) {
$language = \Drupal::languageManager()->getLanguage($langcode);
$options['language'] = $language;
}

$url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid], $options)->toString();
$event->queueItem(['route' => $url]);

// Generate a redirection QueueItem from canonical path to URL.
// Use the default language alias in the event of multi-lang setup.
$queue_factory = QuantQueueFactory::getInstance();
$queue = $queue_factory->get('quant_seed_worker');

if ("/taxonomy/term/{$tid}" != $url) {
$defaultLanguage = \Drupal::languageManager()->getDefaultLanguage();
$defaultUrl = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid], ['language' => $defaultLanguage])->toString();

$redirectItem = new RedirectItem([
'source' => "/taxonomy/term/{$tid}",
'destination' => $defaultUrl,
'status_code' => 301,
]);

$queue->createItem($redirectItem);
}
}
}
}

// Add custom routes.
if ($event->getFormState()->getValue('routes')) {
foreach (explode(PHP_EOL, $event->getFormState()->getValue('routes_textarea')) as $route) {
Expand All @@ -312,6 +282,10 @@ public function collectRoutes(CollectRoutesEvent $event) {
$anon = User::getAnonymousUser();

foreach ($views_storage->loadMultiple() as $view) {
if ($view->get('id') == 'taxonomy_term') {
// Taxonomy terms are handled as entities.
continue;
}
$view = Views::getView($view->get('id'));

$paths = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Form/SeedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\quant\Event\CollectFilesEvent;
use Drupal\quant\Event\CollectRedirectsEvent;
use Drupal\quant\Event\CollectRoutesEvent;
use Drupal\quant\Event\CollectTaxonomyTermsEvent;
use Drupal\quant\Event\QuantCollectionEvents;
use Drupal\quant\QuantStaticTrait;
use Drupal\quant_api\Client\QuantClientInterface;
Expand Down Expand Up @@ -382,6 +383,11 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$this->dispatcher->dispatch($event, QuantCollectionEvents::ENTITIES);
}

if ($form_state->getValue('entity_taxonomy_term')) {
$event = new CollectTaxonomyTermsEvent($form_state);
$this->dispatcher->dispatch($event, QuantCollectionEvents::TAXONOMY_TERMS);
}

$event = new CollectRoutesEvent($form_state);
$this->dispatcher->dispatch($event, QuantCollectionEvents::ROUTES);

Expand Down
56 changes: 56 additions & 0 deletions src/Plugin/QueueItem/TaxonomyTermItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Drupal\quant\Plugin\QueueItem;

use Drupal\quant\Seed;

/**
* A taxonomy term queue item.
*
* @ingroup quant
*/
class TaxonomyTermItem implements QuantQueueItemInterface {

/**
* The taxonomy term id.
*
* @var int
*/
private $tid;

/**
* {@inheritdoc}
*/
public function __construct(array $data = []) {
$this->tid = $data['tid'];
}

/**
* {@inheritdoc}
*/
public function send() {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($this->tid);

foreach ($term->getTranslationLanguages() as $langcode => $language) {
Seed::seedTaxonomyTerm($term, $langcode);
}
}

/**
* {@inheritdoc}
*/
public function info() {
return [
'#type' => '#markup',
'#markup' => "<b>Term ID:</b> {$this->tid}",
];
}

/**
* {@inheritdoc}
*/
public function log($phase = 'start') {
return "[taxonomy_term_item] - tid: {$this->tid}";
}

}
3 changes: 3 additions & 0 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) {
$response = self::markupFromRoute($url);

if (empty($response)) {
// The markupFromRoute function works differently for unpublished terms
// versus nodes. If the response is empty, the term is unpublished.
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH);
return;
}

Expand Down

0 comments on commit 71c79ab

Please sign in to comment.