60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
|
|
<?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);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|