0

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.

alexw
  • 8,468
  • 6
  • 54
  • 86
Jose Luis
  • 11
  • 1
  • How are you setting the value of `$client` initially, when you pass it into `$this->_app->login()`? – alexw Dec 26 '16 at 22:51
  • `$client = Client::where('client_id', $data['client_id']) ->where('userid', $user->id) ->first();` – Jose Luis Dec 26 '16 at 23:56
  • Can you confirm that that actually loads a `Client` object? Perhaps using `error_log($client->id)`? – alexw Dec 26 '16 at 23:59
  • `error_log($this->client);` just before `addGlobal` returns `{"id":3,"client_name":"Client2","client_id":2,"dbtable":"aaa","userid":3}` – Jose Luis Dec 27 '16 at 00:05
  • For sure that it's an object. The problem seems that it's not related to data because '$twig->addGlobal('test', 'Slim test');` doesn't work either – Jose Luis Dec 27 '16 at 12:22

1 Answers1

0

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.

alexw
  • 8,468
  • 6
  • 54
  • 86