In my ZF2 app I want to change the Template directory at runtime.
The default path is set in module.config.php:
'template_path_stack' => array(
__DIR__ . '/../view',
),
I would like to change that to the equivalent of
'template_path_stack' => array(
__DIR__ . '/../view-alternate',
),
at runtime, so I can dynamically change the theme of the page based on the user preference which is stored in the DB.
Template name resolving is a heavy process. It is better to provide the template name manually.
I recommend you to use 'template_map' instead of 'template_path_stack'. You can create some kind of naming convention to differentiate between the two themes, it can be a prefix that you can manage dynamically.
There is a tool to transform the stack into a template map: https://github.com/zendframework/zf2/blob/master/bin/templatemap_generator.php
The solution I ended up with is to use https://github.com/ZendExperts/ZeTheme.
Combined with the option too save the theme in the session I was able to change the theme because it's not at runtime. A page reload was necessary.
It's a workaround. Not the answer.
Related
I'm using Zend Framework 2 (v2.1.4) and I have a partials template that I want to use in several Modules. According to the documentation I can access partials in other Modules, but
That said, it’s likely a better practice to put re-usable partials in shared view script paths.
This sounds like a good idea, but I can't find any documentation on this "share view script path". Where do I find it, and when I've set it, how do I tell the partial() helper to use them?
In your module.config.php
'view_manager' => array(
'template_path_stack' => array(
'mymodule' => __DIR__ . '/../view',
),
'template_map' => array(
'snippets/mypartial' => __DIR__ . '/../view/snippets/mypartial.phtml',
),
),
And use like that
<?php echo $this->partial("snippets/mypartial"); ?>
I am looking for a tutorial on the preferred method of handling localization in Zend 2, but so far I haven't been able to find any. The best I could find is this page, which doesn't explain the practical process of implementing localization (specifically, application messages) in detail, or this question, which was asked before the release of Zend 2 and is now outdated.
If given a choice presented on that page, say I pick GNU Gettext as a translation format. Is there any tutorial on localizing a ZF2 application in that case?
Or, say I store the text of the pages on my site in a database table, for example
CREATE TABLE `page` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`body` blob,
`locale` int(11) NOT NULL,
`creator` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `pagecreatorfk_idx` (`creator`),
CONSTRAINT `pagecreatorfk` FOREIGN KEY (`creator`)
REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
How would I go with providing localized messages then?
Alright, for starters you'll need a translation program such as http://www.poedit.net/
Here is how I have my setup:
In your Module folder create another folder called "language".
Open your module.config.php and add the following:
'translator' => array(
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
'text_domain' => 'account'
)
)
)
What makes things easy for me is to create a language.php file and add lines like this:
echo _('text_to_translate_here');
When you configure your PoEdit you'll scan this file which will add the translation id's to PoEdit. Now you can add the real text that you want to be displayed and upon save it will output 2 files like account.mo and account.po. The only file you need to upload to your language folder is the account.mo
Since I use translations all over my application I've added the factory to my global.php file:
return array(
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
)
)
);
In my view.phtml file I'll now be able translate:
<?php echo $this->translate('text_to_translate_here', 'account'); ?>
Now if you don't want to use textdomains as I have shown in my example of the module.config.php then all translations will by default use the "default" textdomain. So you could do this instead:
<?php echo $this->translate('text_to_translate_here'); ?>
This will also work for FormLabels: (This will output Priroity as the label name)
<?php echo $this->formLabel($this->form->get('prio')); ?>
If you want to translate Form objects using a textdomain other than "default" then you could add this to your view.phtml
$this->formSubmit()->setTranslatorTextDomain('account');
Now your Submit FormLabels will use that textdomain instead of the default textdomain. The same goes for any other type of Form Object. Just replace formSubmit with the element type.
Let me know if this helps out or if I'm missing anything.
I've been using ZF2 since alpha so I'm not entirely new. However not to disturb my current project I setup a new SkeletonApp and it's working. I've added my Translations I needed for both English and German and they are working as expected. However, the Form Error Messages are not being translated to German, or any other language.
I've read the manual but it only covers how to setup Translations based on Factories and it has no details regarding how to set up inside of the module.config.php. Just like in ZF1, ZF2 also have a resources/languages folder which contains all the Form Validation Error Messages already translated. I'd like to use those! And this is where my problem is. I have no idea how to add the configuration to make this work with all my forms.
In my module.config.php file based on the SkeletonApp it already had support for translations, so I took that configuration and added another array, assuming this would have worked.
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo'
),
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../resources/languages',
'pattern' => '%s.php'
)
)
),
However to no avail has this worked. In my Controller I have an eventManager which is where I set my locale like this:
$self->getServiceLocator()->get('translator')->setLocale('en_US'); // change to de_DE for German
Doing this as I mentioned above does translate my text from English to German and so on.
What am I missing to make my Form Errors Translate? Thank you for any guidance you can share on this situation.
Both sources use the same text domain, with the gettext source having the higher priority. This means that it will load your locale from the gettext source and then stop, since the locale + text domain combination is already loaded.
Solution: Use different text domains for each source.
I am using zf2 and I have created a few plugins that I can call from any controller in any plugin (so, no problem with that).
To do so, in the controller where the plugin is, I have added this to the module.config.php
'controler_plugins' => array(
'invokables' => array(
'myplugin' => 'MyModule\Controller\Plugin\MyPlygin'
),
),
So, with that my plugin is accessible from every controller with juste $this->myplugin().
My problem is that I can't call a plugin from a plugin in another module.
Any idea how to do that?
Thanks
Every plugin contains a reference to the controller it was called from, so you should be able to call any registered plugin from within another plugin like so:
$this->getController()->myplugin();
I want to insert image in page module. Anybody have idea how to extend page in contao ?
See screen for more clarification.
http://screencast.com/t/JXk5thjlvHv
See attached screen .. my idea is like this.
You could do that by giving the pages specific CSS classes in their settings. This CSS class will also be used in the regular navigation modules. This way you can define the page's icon in your own stylesheets.
That actually depends how much you want to do.. the easy way: define classes... But thats not the most flexible one.
Lets say you have a custom icon font. Or maybe just FontAwesome. And you want to use these Icons to be shown. In this case, I would install the IconPicker Module by Rocksolid and write a custom module:
/system/modules/z-customs/dca/tl_page.php
$GLOBALS['TL_LANG']['tl_page']['icon']=array('Pageicon', 'Set an Icon for the Page');
$GLOBALS['TL_DCA']['tl_page']['fields']['icon'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_page']['icon'],
'exclude' => true,
'inputType' => 'rocksolid_icon_picker',
'eval' => array(
'fieldType'=>'radio',
'tl_class'=>'w100 clr',
'iconFont' => 'files/fonts/fontawesome-webfont.svg',
),
'sql' => "varchar(100) NOT NULL default ''",
);
/templates/nav_default.html5
<?php if($item['icon']): ?>data-icon="&#x<?= $item['icon']; ?>"<?php endif; ?>
This part can be added to the <li> or the <strong> or a. You may also add a class to the element, to make sure only the elements that have an Icon get the appropriate style.
To get the Icon into the CSS you would just do something like this:
a:before {
content: attr(data-icon);
font-family: "FontAwesome";
}