fisharebest / webtrees

Online genealogy
https://webtrees.net
GNU General Public License v3.0
490 stars 301 forks source link

V2 Custom module help needed #2577

Closed ddrury closed 5 years ago

ddrury commented 5 years ago

Trying to write a custom module; so far have

return new class extends AbstractModule implements ModuleCustomInterface, ModuleConfigInterface {
    use ModuleConfigTrait;
    use ModuleCustomTrait;

bla bla

    public function resourcesFolder(): string
    {
        return __DIR__ . '/resources/';
    }

However, when I click on the spanner icon on the control panel page I get the following page full of an error message

View file not found: …\app/../resources/views//views/admin.phtml …\app\View.php:267
#0 …\app\View.php(191): Fisharebest\Webtrees\View->getFilenameForView('/views/admin')
#1 …\app\View.php(287): Fisharebest\Webtrees\View->render()
#2 …\app\Helpers\functions.php(180): Fisharebest\Webtrees\View::make('/views/admin', Array)
#3 …\app\Http\ViewResponseTrait.php(46): view('/views/admin', Array)
plus a lot more

Guidance welcomed Ta

fisharebest commented 5 years ago

I'm guessing the answer is in the bit of code bla bla bla.

Your error message shows the view duplicated.

View file not found: …\app/../resources/views//views/admin.phtml

This code hasn't been used extensively. What are you trying to do - create a new view or overwrite an existing one? Maybe post the full code?

ddrury commented 5 years ago

I'm using a new view (modules_v4/my_module/resources/views/admin.phtml)

namespace is DDrury\WebtreesAddOns\CustomModule;

The relevant bit of module.php is below. the title, description, customModuleAuthorName, customModuleVersion, customModuleLatestVersionUrl. customModuleSupportUrl, __construct & boot functions are still the defaults from the example module


    /**
     * Main entry point
     *
     * @param ServerRequestInterface $request
     * @param UserInterface          $user
     * @param Tree|null              $tree
     *
     * @return ResponseInterface
     */
    public function getAdminAction(ServerRequestInterface $request, UserInterface $user, Tree $tree = null): ResponseInterface
    {
        // We need a tree to work with.
        if ($tree === null) {
            throw new NotFoundHttpException();
        }

        // Admins only.
        if (!Auth::isAdmin($user)) {
            throw new AccessDeniedHttpException();
        }

        return $this->viewResponse('/views/admin', [
            'module_title' => $this->title(),
            'email_title'  => $this->getPreference('email_title', 'webtrees utility report'),
            'manifest'     => $this->getPreference('manifest_file_name', 'manifest'),
            'prefix'       => $this->getPreference('media_file_prefix', 'WT_Media'),
        ]);
    }
fisharebest commented 5 years ago

return $this->viewResponse('/views/admin'

This should probably be

return $this->viewResponse('admin'

If you look at the existing code, views are referenced relative to the resources/views folder, not relative to the resources folder.

ddrury commented 5 years ago

Now get this

View file not found: …\app/../resources/views/admin.phtml …\app\View.php:267
#0 …\app\View.php(191): Fisharebest\Webtrees\View->getFilenameForView('admin')
#1 …\app\View.php(287): Fisharebest\Webtrees\View->render()
#2 …\app\Helpers\functions.php(180): Fisharebest\Webtrees\View::make('admin', Array)
#3 …\app\Http\ViewResponseTrait.php(46): view('admin', Array)
fisharebest commented 5 years ago

Are you trying to replace the existing admin.phtml (i.e. the control panel), or add an admin page to your own module?

If the latter, then you probably need to specify your module's namespace. (You did register the namespace in the boot() function?)

return $this->viewResponse('MyNameSpace::admin', $data);

BTW - I copied this notation from Laravel. It is possible that webtrees 3 will be a laravel application. At the moment, I'm just keeping my options open).

ddrury commented 5 years ago

Adding the line View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/'); to boot and changing the view call to

$this->viewResponse($this->name() . '::admin', [
parameters
]

fixed it.

Thanks Greg