diff --git a/index.php b/index.php deleted file mode 100644 index 56f118a..0000000 --- a/index.php +++ /dev/null @@ -1,6 +0,0 @@ - - - -

Welcome DANNANNDANN to my site.

- - diff --git a/scripts/curltest.sh b/scripts/curltest.sh new file mode 100644 index 0000000..14dcbbd --- /dev/null +++ b/scripts/curltest.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +filename=$PWD/web/sites/default/files/api-responses/characters.json +if [ ! -f "${filename}" ]; then + touch "${filename}" +fi +curl -s "https://dchadwick.ddev.site/api/character-list" > $PWD/web/sites/default/files/api-responses/characters.json diff --git a/web/modules/custom/dc_core/dc_core.module b/web/modules/custom/dc_core/dc_core.module index 7315802..3b1c71b 100644 --- a/web/modules/custom/dc_core/dc_core.module +++ b/web/modules/custom/dc_core/dc_core.module @@ -25,3 +25,12 @@ function dc_core_taxo_form_submit($form, &$form_state) { } +function dc_core_theme() { + return [ + 'lotr_characters' => [ + 'variables' => [ + 'characters' => [], + ], + ], + ]; +} diff --git a/web/modules/custom/dc_core/src/Attributes/LotrCharacterList.php b/web/modules/custom/dc_core/src/Attributes/LotrCharacterList.php index 308d2fd..25ea4c6 100644 --- a/web/modules/custom/dc_core/src/Attributes/LotrCharacterList.php +++ b/web/modules/custom/dc_core/src/Attributes/LotrCharacterList.php @@ -411,7 +411,23 @@ protected function getCharacterJson(): array { 'bio' => 'Gothmog was the Lieutenant of Morgul and a high-ranking commander of Sauron’s forces in the War of the Ring. As one of the more prominent Orc leaders, he was present during the Battle of the Pelennor Fields, leading the forces of Mordor. Gothmog’s grotesque appearance and ruthlessness made him a terrifying figure. He met his end in the Battle of the Black Gate, where his forces were destroyed and he himself was slain by the armies of Gondor and Rohan.', 'image_url' => 'https://vignette.wikia.nocookie.net/lotr/images/9/94/Gothmog_Lieutenant_of_Morgul.png/revision/latest/scale-to-width-down/350?cb=20190602142219', 'youtube_url' => 'https://www.youtube.com/watch?v=hegeHfK5Dh4' - ] + ], + [ + 'name' => 'Dan Chadwick', + 'species' => 'Human', + 'height' => 'Huge', + 'hair' => 'Brown', + 'eyes' => 'Blue', + 'birth' => 'Unknown', + 'death' => 'Unknown, last seen leaving Middle-earth', + 'title' => 'Drupalist', + 'occupation' => 'Strugglbus', + 'affiliation' => 'Twilight new moon', + 'bio' => 'I am not sure what to put here.', + 'image_url' => 'https://platform.polygon.com/wp-content/uploads/sites/2/chorus/uploads/chorus_asset/file/22263166/lotr3_movie_screencaps.com_10384.jpg', + 'youtube_url' => '' + ], + ]; return $characters; diff --git a/web/modules/custom/dc_core/src/Plugin/Block/CharactersBlock.php b/web/modules/custom/dc_core/src/Plugin/Block/CharactersBlock.php new file mode 100644 index 0000000..f0d9201 --- /dev/null +++ b/web/modules/custom/dc_core/src/Plugin/Block/CharactersBlock.php @@ -0,0 +1,33 @@ + 'lotr_characters', + '#characters' => $characters, + ]; + + return $build; + } + +} diff --git a/web/modules/custom/dc_core/templates/lotr-characters.html.twig b/web/modules/custom/dc_core/templates/lotr-characters.html.twig new file mode 100644 index 0000000..a395784 --- /dev/null +++ b/web/modules/custom/dc_core/templates/lotr-characters.html.twig @@ -0,0 +1,5 @@ +{% for character in characters %} +

{{ character.name }}

+

{{ character.title }}

+

{{ character.bio }}

+{% endfor %} diff --git a/web/modules/custom/ufc/src/Commands/UfcCommands.php b/web/modules/custom/ufc/src/Commands/UfcCommands.php index 3dbea05..f1bad18 100644 --- a/web/modules/custom/ufc/src/Commands/UfcCommands.php +++ b/web/modules/custom/ufc/src/Commands/UfcCommands.php @@ -21,7 +21,6 @@ class UfcCommands extends DrushCommands { public function importFighters(): void { // Fighter importer service. $fighter_importer = \Drupal::service('ufc.import_fighters'); - // First check for the item in cache. $fighter_list = \Drupal::cache()->get($this->cacheId); if (!$fighter_list) { @@ -32,22 +31,19 @@ public function importFighters(): void { $fighter_list = $fighter_list->data; } - // Add each division to a batch process. - $batch = new BatchBuilder(); - $batch->setTitle("Starting Complete Fighter Import") - ->setFinishCallback([UfcCommands::class, 'importFinished']) - ->setInitMessage("Importing..."); + $fighter_import_queue = \Drupal::queue('fighter_import'); foreach ($fighter_list as $division => $fighters) { - $args = [ - $division, - $fighters, - ]; - $batch->addOperation([FighterImporter::class, 'processDivision'], $args); + foreach ($fighters as $fighter) { + $queue_item = new \stdClass(); + $queue_item->first_name = $fighter['firstname']; + $queue_item->last_name = $fighter['lastname']; + $queue_item->image = $fighter['image']; + $queue_item->division = $division; + $queue_item->profile = $fighter['profile']; + $fighter_import_queue->createItem($queue_item); + } } - // Set and run the batch. - batch_set($batch->toArray()); - drush_backend_batch_process(); } /** diff --git a/web/modules/custom/ufc/src/Plugin/QueueWorker/FighterQueueWorker.php b/web/modules/custom/ufc/src/Plugin/QueueWorker/FighterQueueWorker.php new file mode 100644 index 0000000..f4b5f49 --- /dev/null +++ b/web/modules/custom/ufc/src/Plugin/QueueWorker/FighterQueueWorker.php @@ -0,0 +1,123 @@ +entityTypeManager = $entityTypeManager; + } + + /** + * Used to grab functionality from the container. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container. + * @param array $configuration + * Configuration array. + * @param mixed $plugin_id + * The plugin id. + * @param mixed $plugin_definition + * The plugin definition. + * @return static + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition + ) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('database'), + ); + } + + /** + * Processes an item in the queue. + * + * @param mixed $data + * The queue item data. + * + */ + public function processItem($data): void { + $this->doProcess($data); + } + + /** + * Do the actual processing of an item. + * + * @param $item + * The item to process. + */ + private function doProcess($item): bool { + $fighter = new Fighter(\Drupal::httpClient()); + $fighter->first_name = $item->first_name; + $fighter->last_name = $item->last_name; + $fighter->image = $item->image; + $fighter->class = $item->division; + if (!$fighter->scrapeDataFromFighterPage($item->profile)) { + \Drupal::logger('ufc')->alert("FAILED: $fighter->first_name $fighter->last_name to " . $item->profile); + } + // Check if node exists, by title. + $fighter->createMediaEntityFromImage(); + $title = $fighter->first_name . " " . $fighter->last_name; + $node_lookup = reset(\Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => $title])); + + if (!empty($node_lookup)) { + // Update instead of create. + $fighter->updatePlayerNode($node_lookup->id()); + \Drupal::logger('ufc')->notice("$title updated successfully."); + } + else { + \Drupal::logger('ufc')->warning("No existing player found for $title...creating"); + $fighter->createPlayerNode(); + } + return TRUE; + } + +} diff --git a/web/modules/custom/ufc/src/Services/FighterImporter.php b/web/modules/custom/ufc/src/Services/FighterImporter.php index 1fd7bbb..1561401 100644 --- a/web/modules/custom/ufc/src/Services/FighterImporter.php +++ b/web/modules/custom/ufc/src/Services/FighterImporter.php @@ -136,7 +136,6 @@ public static function processDivision($div, $fighters): void { $fighter->createPlayerNode(); } } - $context['results']['processed']++; } /**