Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not pass variable to Widget (via Config Array) #151

Closed
FatihKoz opened this issue Jul 15, 2021 · 5 comments
Closed

Can not pass variable to Widget (via Config Array) #151

FatihKoz opened this issue Jul 15, 2021 · 5 comments
Assignees

Comments

@FatihKoz
Copy link

Been using laravel-widgets for months, always used config arrays and had no problems until today ...

class MyWidget extends Widget
{
    protected $config = [
        'selection' => 'DEFAULT'
    ];
}

@widget('Modules\MyModule\Widgets\MyWidget', ['selection' => 'USERSELECTION'])

$this->config['selection'] is still 'DEFAULT'

Using 3.13.1 with Laravel 8.46.0 / Apache 2.4.46 with php 7.4.19

Everything else works just fine, controller returns data to blade as expected without errors, only the config is not being changed and always the default is used.

I even tried to get the user selection and apply some php string functions to see if the problem was upper/lower case but that did not worked too.

Any ideas ?

@arrilot arrilot self-assigned this Jul 16, 2021
@FatihKoz
Copy link
Author

FatihKoz commented Aug 21, 2021

I may have some more info on this @arrilot , check below example please and hope it helps.
(pls do not mind the stupidity in the example, it is just there to explain the output)

namespace App\Widgets;

use App\Contracts\Widget;
use App\Repositories\NewsRepository;

class LatestNews extends Widget
{
  protected $config = ['count' => 5];

  public function run()
  {
    $newsRepo = app(NewsRepository::class);

    return view('widgets.latest_news', [
      'config' => $this->config,
      'news'   => $newsRepo->recent($this->config['count']),
    ]);
  }
}

Works just fine and config/count gets updated. However something like below makes config array changes impossible with widget calls.

namespace App\Widgets;

use App\Contracts\Widget;
use App\Repositories\NewsRepository;
use Illuminate\Support\Facades\Auth;

class LatestNews extends Widget
{
  protected $config = ['count' => 5];

  public function GetUser() {
    $user = Auth::user();
    return isset($user) ? $user->name_private : 'Dear Visitor';
  }

  public function run()  {
    $newsRepo = app(NewsRepository::class);
    $user = $this->GetUser();

    return view('widgets.latest_news', [
      'config' => $this->config,
      'user'    => $user,
      'news'   => $newsRepo->recent($this->config['count']),
    ]);
  }
}

So in simple terms, if I define anything more than public function run() in widget controller and use them, config array remains unchanged by widget calls and always the defaults are being returned.

If I move all the additional functions inside run() , config array starts acting normal.

@arrilot
Copy link
Owner

arrilot commented Aug 21, 2021

Unfortunately I don't see any possible reasons why such behavior can happen.
I also cannot reproduce the issue on a fresh Laravel install.
You can try to reproduce it yourself and upload to a public github repo.

Wild guess: maybe you actually run your app on Laravel Octane instead of Apache.

@arrilot
Copy link
Owner

arrilot commented Aug 21, 2021

I also noticed that you used your own widget class in extends Widget
Please make sure that you do not do any bad stuff there.

If you override AbstractWidget constructor you should call parent::__construct($config) in the end of the new __construct

@FatihKoz
Copy link
Author

Thanks for the reply, the main app is not using Laravel Octane and this is the Widget class we have in the app.

namespace App\Contracts;

use Arrilot\Widgets\AbstractWidget;

abstract class Widget extends AbstractWidget
{
    public $cacheTime = 0;

    public function view(string $template, array $vars = [])
    {
        return view($template, $vars);
    }
}

Will try using AbstractWidget to see if the Widget class is causing this behavior.

@FatihKoz
Copy link
Author

FatihKoz commented Aug 21, 2021

It was the __construct , thanks for pointing me out to the right direction... When I was defining additional functions, I was always using construct :)

  public function __construct(
    AirportRepository $airportRepo,
    array $config = []
  ) {
    $this->airportRepo = $airportRepo;
    parent::__construct($config);
  }

Closing this now, thanks again for your time and replies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants