Jose Luis
Jose Luis

Reputation: 11

Adding a new global variable in twig

I'm trying to get data for a new field added in login page. What I've done:

  1. Modify AccountController.php login function adding new parameter: $this->_app->login($user, $client, !empty($data['rememberme']))
  2. In Userfrosting.php login function i've set it in application: $this->client = $client;
  3. In setupTwigUserVariables funtion added twig global: $twig->addGlobal("client", $this->client);

The problem is that in a template, {{client.id}} returns nothing. Any help will be appreciated.

Upvotes: 0

Views: 519

Answers (1)

alexw
alexw

Reputation: 8688

In UserFrosting 4, you should create a Twig extension in your Sprinkle's src/Twig/ directory, and add the variable to the return value for getGlobals.

Your situation is a little tricky, since I'm not sure how client can be a global variable but at the same time depend on $data['client_id'] - which appears to be a request parameter. For now, I'll assume that you're submitting this parameter with any requests that require the client variable.

<?php
/**
 * Stack Overflow
 *
 * @link      https://stackoverflow.com
 */
namespace UserFrosting\Sprinkle\Site\Twig;

use Interop\Container\ContainerInterface;
use UserFrosting\Sprinkle\Site\Database\Models\Client;

/**
 * Extends Twig functionality for the Site sprinkle.
 *
 * @author Jose Luis
 */
class Extension extends \Twig_Extension
{

    protected $services;
    protected $config;

    public function __construct(ContainerInterface $services)
    {
        $this->services = $services;
        $this->config = $services->config;
    }

    public function getName()
    {
        return 'myproject';
    }

    public function getGlobals()
    {
        try {
            $currentUser = $this->services->currentUser;

            // Assumes the client_id is being submitted as a query string (url) parameter
            $clientId = $this->services->request->getQueryParam('client_id');

            $client = Client::where('client_id', clientId)->where('userid', $currentUser->id)->first();
        } catch (\Exception $e) {
            $client = null;
        }

        return [
            'client' => $client
        ];
    }
}

You will then need to register this extension in your Sprinkle's service provider class:

<?php
/**
 * Stack Overflow
 *
 * @link      https://stackoverflow.com
 */
namespace UserFrosting\Sprinkle\Site\ServicesProvider;

use UserFrosting\Sprinkle\Site\Twig\Extension as JoseExtension;

/**
 * Services provider for the Site sprinkle.
 *
 * @author Jose Luis
 */
class ServicesProvider
{
    /**
     * Register extended user fields services.
     *
     * @param Container $container A DI container implementing ArrayAccess and container-interop.
     */
    public function register($container)
    {
        /**
         * Extends the 'view' service with Jose's Twig Extension.
         */
        $container->extend('view', function ($view, $c) {
            $twig = $view->getEnvironment();
            $extension = new JoseExtension($c);
            $twig->addExtension($extension);

            return $view;
        });
    }
}

Yes, I know that there is a lot of boilerplate here. However once you set these up the first time, it is easy to add new variables/functions/filters to the Twig environment and new services to your Sprinkle in the future.

Upvotes: 0

Related Questions