drupalorg-1.0.x-dev/src/Twig/DrupalorgTwig.php
src/Twig/DrupalorgTwig.php
<?php
namespace Drupal\drupalorg\Twig;
use Drupal\drupalorg\ProjectService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
* Custom twig function to render a project link from a machine name.
*/
class DrupalorgTwig extends AbstractExtension {
/**
* Creates TwigExtension.
*
* @param \Drupal\drupalorg\ProjectService $projectService
* The project service.
*/
public function __construct(
protected ProjectService $projectService,
) {}
/**
* List of filters defined by this module.
*
* @return \Twig\TwigFilter[]
* List of filters and map to functions.
*/
public function getFilters() {
return [
new TwigFilter('drupalorg_project_link', [$this, 'projectLink']),
];
}
/**
* Generates a link to a project by the given machine name.
*
* @param string $text
* Machine name of the project.
*
* @return string
* Original text or link to the project if found.
*/
public function projectLink($text) {
return $this->projectService->getProjectLinkFromMachineName($text);
}
}
