I want to use new md2html Twig filter to easily transform Markdown but faced this error:
" Cannot autowire service "App\Twig\AppExtension": argument "$locales" of method "__construct()" is type-hinted "string", you should configure its value explicitly. "
How to solve this error?
Code:
// src/Twig/AppExtension.php
namespace App\Twig;
use App\Utils\Markdown;
use Symfony\Component\Intl\Locales;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* This Twig extension adds a new 'md2html' filter to easily transform Markdown
* contents into HTML contents inside Twig templates.
*
*/
class AppExtension extends AbstractExtension
{
private $parser;
private $localeCodes;
private $locales;
public function __construct(Markdown $parser, string $locales) // Error this line...
{
$this->parser = $parser;
$localeCodes = explode('|', $locales);
sort($localeCodes);
$this->localeCodes = $localeCodes;
}
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new TwigFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction('locales', [$this, 'getLocales']),
];
}
/**
* Transforms the given Markdown content into HTML content.
*/
public function markdownToHtml(string $content): string
{
return $this->parser->toHtml($content);
}
/**
* Takes the list of codes of the locales (languages) enabled in the
* application and returns an array with the name of each locale written
* in its own language (e.g. English, Français, Español, etc.).
*/
public function getLocales(): array
{
if (null !== $this->locales) {
return $this->locales;
}
$this->locales = [];
foreach ($this->localeCodes as $localeCode) {
$this->locales[] = ['code' => $localeCode, 'name' => Locales::getName($localeCode, $localeCode)];
}
return $this->locales;
}
}