Last active
December 12, 2015 06:49
-
-
Save dspe/4732313 to your computer and use it in GitHub Desktop.
How to get an user name on twig template ? {{ content.contentInfo.contentType.creatorId|user_name }}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Twig; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class AcmeExtension extends \Twig_Extension | |
{ | |
private $container; | |
public function __construct( ContainerInterface $container ) | |
{ | |
$this->container = $container; | |
} | |
public function getFilters() | |
{ | |
return array( | |
'user_name' => new \Twig_Filter_Method($this, 'userNameFilter'), | |
); | |
} | |
public function userNameFilter($userId = 10) | |
{ | |
$userService = $this->container->get('ezpublish.api.repository')->getUserService(); | |
$user = $userService->loadUser( $userId ); | |
return $user->content->contentInfo->name; | |
} | |
public function getName() | |
{ | |
return 'Acme'; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
Acme.twig_extension: | |
class: %acme.twig_extension.class% | |
arguments: [@service_container] | |
tags: | |
- { name: twig.extension } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dspe : Did you know that your gist can have multiple files ? 😉