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

WebView in widgets #188

Closed
Gerych1984 opened this issue Oct 14, 2021 · 2 comments
Closed

WebView in widgets #188

Gerych1984 opened this issue Oct 14, 2021 · 2 comments

Comments

@Gerych1984
Copy link
Contributor

Gerych1984 commented Oct 14, 2021

Доброго дня. Столкнулся с такой проблемой/ошибкой. Сильно упрощенная версия моего виджета, который должен отрендерить форму:

<?php
declare(strict_types=1);

namespace App\Front\Source\Widget\Data;

use Yiisoft\View\ViewContextInterface;
use Yiisoft\View\WebView;
use Yiisoft\Assets\AssetManager;
use Yiisoft\Widget\Widget;

final class SearchFormWidget extends Widget implements ViewContextInterface
{
       private WebView $view;
       private AssetManager $assetManager;

       public function __construct(WebView $view, AssetManager $assetManager)
       {
              $this->view = $view;
              $this->assetManager = $assetManager;
       }

       public function getViewPath(): string
       {
              return __DIR__ . DIRECTORY_SEPARATOR . 'views'; //Вьюха лежит рядом с виджетом
       }

      protected function run(): string
      {
               return $this->view->withContext($this)->render('search-form');
      }

       protected function afterRun(string $result): string
       {
              // $this->assetManager->register([Какой-то-ассет::class]);
              $this->view->registerJs('Какой-то js код' . PHP_EOL);
              
              return parent::afterRun($result);
       }
}

Собственно проблема следующая:

  1. Если вызвать виджет так echo SearchFormWidget::widget(); (т.е. WebView попадает через DI), то форма рендерится нормально (метод run), но вызовы registerJs ничего не выводят в лэйауте
  2. Если принудительно вызвать с текущим WebView
echo SearchFormWidget::widget([
    '__construct()' => [
        'view' => $this
    ]
]);

то игнорируется указанный контекст и вью-файл ищется относительно директории указанной в настройках.

На данный момент закостылил таким образом

 public function __construct(WebView $view, WebView $innerView, AssetManager $assetManager)

соответственно

  1. $view - указываю я для вызовов registerJs
  2. $innerView - через DI, для рендера формы в указанном контексте.

Заранее благодарен

@Gerych1984
Copy link
Contributor Author

Прикладываю полную версию виджета. Закомментировал только неважный для теста параметр и поменял вью-файл

SearchForm.zip

@samdark samdark changed the title WebView в виджетах WebView in widgets Oct 19, 2021
@samdark
Copy link
Member

samdark commented Oct 25, 2021

Should be fixed in latest version. Going to tag it soon.

@samdark samdark closed this as completed Oct 25, 2021
samdark pushed a commit that referenced this issue Dec 14, 2021
… Improve fluent interface + Fix configs (#195)

- Fix #193: Add method `ViewInterface::withContextPath()` that set view context path
- Fix #194: Add method `ViewInterface::addToParameter()` that add value(s) to end of specified array parameter
- Fix #195: Add method `ViewInterface::withClearedState()` that cleared state of view (parameters, blocks, etc.)
- Fix #195: Mutable method `ViewInterface::setPlaceholderSalt()` replaced to immutable `withPlaceholderSalt()`
- Fix #195: Methods `removeParameter()` and `removeBlock()` of `ViewInterface` returns self
- Fix #195: Methods of `WebView` returns self: `registerMeta()`, `registerMetaTag()`, `registerLink()`,
  `registerLinkTag()`, `registerCss()`, ` registerCssFromFile()`, `  registerStyleTag()`, ` registerCssFile()`,
  `addCssFiles()`, `addCssStrings()`, `registerJs()`, `registerScriptTag()`, `registerJsFile()`, `registerJsVar()`,
  `addJsFiles()`, `addJsStrings()`, `addJsVars()`
- Fix #188: Use common state for cloned instances of `View` and `WebView`
- Fix #195: Fix configuration: set parameters after reset `View` and `WebView`
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