BynderTagSearchService.php in Bynder 4.0.x
File
src/Controller/BynderTagSearchService.php
View source
<?php
namespace Drupal\bynder\Controller;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Exception\TagSearchException;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class BynderTagSearchService extends ControllerBase {
const TAG_LIST_LIMIT = 25;
protected $bynder;
protected $logger;
public function __construct(BynderApiInterface $bynder, LoggerChannelFactoryInterface $logger) {
$this->bynder = $bynder;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('bynder_api'), $container
->get('logger.factory'));
}
public function searchTags(Request $request) {
$results = [];
$keyword = $request
->get('term');
try {
$results = array_map(function ($tag) {
return [
'id' => $tag['tag'],
'text' => $tag['tag'],
];
}, $this->bynder
->getTags([
'limit' => self::TAG_LIST_LIMIT,
'keyword' => $keyword,
'minCount' => 1,
]));
} catch (\Exception $e) {
(new TagSearchException($e
->getMessage()))
->logException()
->displayMessage();
return FALSE;
}
usort($results, function ($first, $second) {
return $first['text'] < $second['text'] ? -1 : 1;
});
$response['results'] = $results;
return new JsonResponse($response);
}
}