new updates

This commit is contained in:
Dan Chadwick
2024-04-09 12:08:12 -07:00
parent 24f432858d
commit 1f84123528
25 changed files with 373 additions and 151 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace Drupal\ufc\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\node\Entity\Node;
use Drupal\Core\Cache\CacheableJsonResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RecentFightsController extends ControllerBase {
protected $entityTypeManager;
public function __construct(
EntityTypeManager $entityTypeManager
) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
);
}
/**
* Gets recent fights.
*/
public function getRecentFights() {
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->accessCheck(TRUE);
$query->condition('type', 'fight')->sort('created', 'DESC');
$query->range(0, 12);
$nids = $query->execute();
$all_fights = Node::loadMultiple($nids);
$fights = [];
foreach ($all_fights as $fight) {
$f1 = Node::load($fight->field_fighter_one->target_id);
$f2 = Node::load($fight->field_fighter_two->target_id);
$result = Node::load($fight->field_result->target_id);
$alias = \Drupal::service('path_alias.manager')->getAliasByPath("/" . $fight->toUrl()->getInternalPath());
if ($f1 && $f2) {
$fights[] = [
'fighter_one' => $f1->getTitle(),
'fighter_two' => $f2->getTitle(),
'result' => $result->getTitle(),
'url' => $alias
];
}
}
return new CacheableJsonResponse($fights);
}
}

View File

@@ -1,4 +1,11 @@
## These are routes that serve rendered HTML for ingestion.
ufc.recent_fights:
path: '/api/v1/recent-fights'
defaults:
_controller: '\Drupal\ufc\Controller\RecentFightsController::getRecentFights'
_title: 'Recent Fights Endpoint'
requirements:
_permission: 'access content'
ufc.fighters_for_dcjs:
path: '/dcjs/all-fighters'
defaults: