How to make Modules use Application View Helpers? - zend-framework

Since, adding some extra modules to the zend framework application, a lot of errors are popping out. By default, all the extra modules tend to use the same layout file to render a html, but for view helpes, it searches it's own folder.
In my case, I made a view helper, to load some template's css file. I named it LoadTemplate and placed it inside APPLICATION_PATH."/view/helpers"
It works perfectly, until I browse to a module. Then it gives a error saying
Plugin by name 'LoadTemplate' was not found in the registry; used paths: Custom_View_Helper_: x/x/application/modules/custom/views\helpers/ Zend_View_Helper_: Zend/View/Helper/
It is searching in the wrong folder.
Can't we tell it to search its folder first, and if not found go and find the helper from the default or Application's View Helper?

If your helper is in that directory, make sure it is called Zend_View_Helper_LoadTemplate, the function is called loadTemplate and the file name is LoadTemplate.php
Edit -
Also check out this blog post by Rob Allen: http://akrabat.com/zend-framework/view-helpers-in-modules/

Related

Laminas: Different Layout Files for each Module

i would like to use a different layout file for each module which is set under view_manager- template_mapin the module.config.php
But for some reason both modules are always using the same Layout File. Is it not possible to use different layout files for each module ?
I have not worked on this question since ZF2 (2014) but at the time I wrote a module to answer your question. It is published on github at: https://github.com/dafap/DafapLayout
It will have to be adapted to Laminas (ZF3). To do this, replace the namespaces and edit the DafapLayout\Service\ConfigServiceFactory by replacing the createService() methods with __invoke().
Be careful, this module is in PSR0. Be sure to specify it in your composer.json otherwise it will not work.

TYPO3 extension - f:uri.resource returns wrong path

I'm writing a backend module for my own TYPO3 extension, where I'm wrapping the content into a be.container including a CSS and a JS file:
<f:be.container
includeCssFiles="{0: '{f:uri.resource(path:\'CSS/backend.css\')}'}"
includeJsFiles="{0: '{f:uri.resource(path:\'JS/backend.js\')}'}"
>
<!-- module content -->
</f:be.container>
None of those resources gets included though. When I'm rendering f:uri.resource directly into the HTML, it - no matter which path - resolves to
/typo3/
which makes no sense to me. In my project, /typo3/ is the path where the TYPO3 core is symlinked to, relative to my web path.
I've assured that my resource files are placed in EXT:my_extension/Resources/Public/CSS/backend.css and EXT:my_extension/Resources/Public/JS/backend.js respectively. I had a similiar issue including resources in the frontend, where it would resolve to just a / - in the end I decided to go with inclusion via Typoscript instead. I'm not sure if I'll be able to do this for the backend module as well. Any idea how to solve this? Maybe I missed something in the configuration which messes up path resolution for f:uri.resource. Any help would be appreciated.
For a minimal test case, try putting the plain output into your fluid template:
{f:uri.resource(path:'CSS/backend.css')}
If that yields the same result, it seems like the ViewHelper is not aware of the current extension. You can pass it the extension key as argument, see https://docs.typo3.org/other/typo3/view-helper-reference/9.5/en-us/typo3/fluid/latest/Uri/Resource.html.

TypoScript: Check if JS/CSS File is already included

anybody knows how to check if a js/css file is already included with typoscript?
Example
[Template_A.ts]
page.includeJS {
jsfile = fileadmin/Template/js/jquery-1.10.2.min.js
}
now if i got an extension with the same include e.g.
[Extension_A.ts]
page.includeJS {
jsfile = fileadmin/Template/js/jquery-1.10.2.min.js
}
Is there a way to prevent this kind of double code injection? Maybe i got another Template e.g. Template_B.ts where jquery is not included - than the Extension_A.ts has to include jquery by itself.
Kinldy
You can use the same key inside includeJS such it just gets overridden if you include the file twice.
Other than that you should put jQuery into includeJSlibs, such that it is loaded before the other JS files.
Other than that, the TS should be unique for each page. Therefore you always to make sure anyway that all resources are included in-order.
You should not include JS libs with the automatic extension TS setups. Use your documentation to tell the integrator what needs to be included and what not.
The various and independent inclusion of scripts by plugins and templates is one of the tricky points in TYPO3. As far as I know, this point cannot be managed at one single point.
There is a plugin "t3jquery" that offers to build, compress and share a common jQuery library. It also has a service to analyze other plugins for their dependencies. But this doesn't solve the problem in general, as many plugins don't check for libraries already loaded.
The most stable way is to remove all plugin's references to libraries manually in your TypoSkript. This gives you some simple additional TypoSkript lines. I use lines like these:
plugin.tx_imagecycle_pi1.file.jQueryLibrary >
plugin.tx_imagecycle_pi1.jQueryLibrary >
plugin.tx_multicontent_pi1.file.jQueryLibrary >
plugin.tx_multicontent_pi1.jQueryLibrary >
# Fluid
page.headerData.998 >
You can find the matching TypoSkript descriptors by searching for the library name in the TypoSkript browser or by greping in the plugin's source code. You will also need this if you wish to add libraries as part of content that was get by AJAX, thus separating the libs from the page content.
Here's a tut (in German): http://jweiland.net/typo3/typoscript/javascript-manuell-entfernen-oder-einbinden.html
Futher possibilities you can check:
Some plugins are written in good structure and offer to keep back their Javascript in the settings.
Some script inclusions may come rather from the static template but from a plugin, so don't forget to have a look there.

layout initialisation peculiarity in Zend Framework

I've noticed something interesting about Zend Framework's bootstrap. I created a new project and then used
zf enable layout
to enable the layout engine. It worked out of the box, woo!
But then I tried creating a function called _initLayout in the bootstrap to set some options. Interestingly, this seems to disable the layout again, even if the function body is actually empty. No errors are thrown, but the layout script is not used anymore (exception being the case when I actually set the options again and manually call Zend_Layout::startMvc()).
Renaming the function to anything else, like _initFoo makes the layout work again.
So, my question is: is this a function name that is somehow recognised by Zend Framework and extra actions are applied to it, such as cancelling the layout config from application.ini? Are there other cases where I should avoid certain _init* function names in the bootstrap?
The main purpose of the Bootstrap is to setup resources that the application uses. These can either be setup by lines in the config file (resources.resourcename.foo) or by methods in the bootstrap class (_initResourceName()). I assume zf enable layout has added some resources.layout.* lines to the application.ini. By adding an _initLayout method to the bootstrap, ZF will use this to setup the layout resource instead of the configuration lines.
Are there other cases where I should avoid certain _init* function names in the bootstrap?
The resource plugins are detailed in the manual: http://framework.zend.com/manual/en/zend.application.available-resources.html, _init<resourcename>() will always override any corresponding lines in the config.

zend framework under document root in subdir

I developed a application with Zend Framework and now I want to be able to place the app in an subdirectory of a Documentroot.
e.g. http://www.example.com/myapp/
I read quite a lot of Docu how this could work, but all in all these solutions don´t fit my needs. Is there a trivial way to do the subdir thing, without adding the concrete path to any file which generates the pages.
There are some examples in the net, where a basePath is set in the application enviroment and so there is a method call bevor each "form" creation which prepends the path before the link.
$form->setAction($this->_request->getBaseUrl() . $this->_helper->url('sign'));
This was from: http://johnmee.com/2008/11/zend-framework-quickstart-tutorial-deploy-to-a-subdirectory-instead-of-web-root/
But this is only works for small examples, I have tons of forms, tons of views and tons of scripts. I can´t belive this (lets call it hack :) ) is the only solution to do this.
Any ideas?
You don't have to do anything special. See my tutorial at http://akrabat.com/Zend-framework-tutorial which is developed entirely within a sub-directory.
As they say on the web page:
I’m told this last issue has been
lodged has a defect and not necessary
from releases “1.7″ and beyond. The
helper->url will henceforth prepend
the baseUrl to its result.
So you should be fine. Do you actually use the $form->setAction() method on every form already? Because if you use it in combination with the url helper, the baseUrl will already be included.