PHP code example of tmonier / sylius-command-palette-plugin
1. Go to this page and download the library: Download tmonier/sylius-command-palette-plugin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tmonier / sylius-command-palette-plugin example snippets
namespace App\CommandPalette;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Tmonier\SyliusCommandPalettePlugin\Search\LikePattern;
use Tmonier\SyliusCommandPalettePlugin\Search\SearchProviderInterface;
use Tmonier\SyliusCommandPalettePlugin\Search\SearchResult;
final class ShipmentSearchProvider implements SearchProviderInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private UrlGeneratorInterface $urlGenerator,
) {
}
public function getKey(): string
{
return 'shipments'; // group label: translation "tmonier_command_palette.group.shipments"
}
/** $query is trimmed and 2 to 100 characters long. */
public function search(string $query, int $limit): array
{
$rows = $this->entityManager->createQueryBuilder()
->select('s.id AS id', 's.tracking AS tracking', 'o.number AS number')
->from(\App\Entity\Shipping\Shipment::class, 's')
->innerJoin('s.order', 'o')
->andWhere(sprintf('LOWER(s.tracking) LIKE :prefix %s', LikePattern::DQL_ESCAPE))
->setParameter('prefix', LikePattern::prefix($query)) // escapes % and _
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return array_map(fn (array $row): SearchResult => new SearchResult(
title: $row['tracking'],
subtitle: '#' . $row['number'],
url: $this->urlGenerator->generate('sylius_admin_shipment_index'),
icon: 'tabler:truck',
), $rows);
}
}