I need to override a class and a template. To achieve this, I choose to create a new module in order to not be blocked when Prestashop will update. For overriding class, I don't have problem.
The worry is in overriding template, fields are in English whereas in the rest of my website, they are in French (when I uninstall the module, they come back in French).
In my module, I override the template with this way :
public function hookDisplayOverrideTemplate($params)
{
$controllerName = get_class($params['controller']);
$tpl = $this->local_path . 'override/tpl/' . $controllerName . '.tpl';
if (file_exists($tpl))
{
return $tpl;
}
return false;
}
In my .tpl file, the fields which are not translate are like this :
{l s='Your addresses'}
So, I tried to insert the fr.php file in /my_module/translations/fr.php, and I edited it :
<?php
global $_MODULE;
$_MODULE = array();
$_MODULE['<{customaddresses}prestashop>customaddresses_3908e1afa0ff22fbf112aff3c5ba55c1'] = 'Vos adresses';
Then, I modified the .tpl file :
{l s='Your addresses' mod='customaddresses'}
My fields are still in English.
Someone has a solution to suggest ?
After a great deal of research, I found my problem.
My solution is : $_MODULE['<{customaddresses}prestashop>address_0f68b904e33d9ac04605aecc958bcf52'] = 'Informations supplémentaires';
I replaced "customaddresses_" by "address_" (my overriding template).
I kept :
{l s='Your addresses' mod='customaddresses'}
And to be more proper, I changed a little bit my method hookDisplayOverrideTemplate :
public function hookDisplayOverrideTemplate($params)
{
$controllerName = get_class($params['controller']);
$tpl = $this->local_path . 'override/tpl/' . strtolower(str_replace("Controller", "", $controllerName)) . '.tpl';
if (file_exists($tpl))
{
return $tpl;
}
return false;
}
Related
The name is quite bad, but I really don't know what else to call it.
I'm trying to make a extendable and modular plugin system for my website. I need to be able to access plugin php files that exist in a plugin directory and get access to their classes to call functions such as getting the html content that the plugin should show and more.
Below is a semi-pseudo code example of what I am trying to achieve, but how to actually arbitrarily load the plugins is where I am stuck (PluginLoader.php).
-Max
//BasePlugin.php
abstract class BasePlugin
{
public function displayContent()
{
print "<p>Base Plugin</p>";
}
};
//ExamplePlugin.php -> In specific plugin directory.
require('../BasePlugin.php');
class ExamplePlugin extends BasePlugin
{
public static function Instance()
{
static $inst = null;
if ($inst === null) {
$inst = new ExamplePlugin();
}
return $inst;
}
public function displayContent()
{
print "<p>Example Plugin</p>";
}
}
//PluginLoader.php
foreach($pluginFile : PluginFilesInDirectory) { // Iterate over plugin php files in plugin directory
$plugin = GetPlugin($pluginFile); // Somehow get instance of plugin.
echo plugin->displayContent();
}
I'm guessing here, but it seems to me that you need to:
get a list of the plugins in the desired directory.
include or require the plugin's class file.
create an instance of the class.
call the plugin's displayContent() method.
So, you probably want to do something like
$pluginDir = 'your/plugin/directory/' ;
$plugins = glob($pluginDir . '*.php') ;
foreach($plugins as $plugin) {
// include the plugin file
include_once($plugin) ;
// grab the class name from the plugin's file name
// this finds the last occurrence of a '/' and gets the file name without the .php
$className = substr($plugin,strrpos($plugin,'/') + 1, -4) ;
// create the instance and display your test
$aPlugin = $className::Instance() ;
$aPlugin->displayContent() ;
}
There's probably a cleaner way to do it, but that will ready your directory, get the plugins' code, and instantiate each one. How you manage/reference them afterwards depends on how your plugins register with your application.
I am attempting to override a module in Prestashop but the changes are just not appearing.
I have overridden a template file and a controller so I have added the following files:
\override\modules\blockwishlist\views\templates\front\mywishlist.tpl
\override\modules\blockwishlist\controllers\front\mywishlist.php
These are very simple changes where I add another column to a table that contains a button. When the button is clicked the controller generates a CSV file.
Any idea why these changes are just not being shown?
Note: I have turned on 'Force Compile' and turned of caching.
Edit:
Re overridding a controller is it:
class BlockWishListMyWishListModuleFrontController extends BlockWishListMyWishListModuleFrontControllerCore // extends ModuleFrontController
or
class BlockWishListMyWishListModuleFrontControllerOverride extends BlockWishListMyWishListModuleFrontController
okay, I did some code research (maybe thre is exists easiest way, not sure), so:
in code Dispatcher class we have
// Dispatch module controller for front office
case self::FC_MODULE :
$module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
$module = Module::getInstanceByName($module_name);
$controller_class = 'PageNotFoundController';
if (Validate::isLoadedObject($module) && $module->active) {
$controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$module_name.'/controllers/front/');
if (isset($controllers[strtolower($this->controller)])) {
include_once(_PS_MODULE_DIR_.$module_name.'/controllers/front/'.$this->controller.'.php');
$controller_class = $module_name.$this->controller.'ModuleFrontController';
}
}
$params_hook_action_dispatcher = array('controller_type' => self::FC_FRONT, 'controller_class' => $controller_class, 'is_module' => 1);
break;
where we see that controllers loaded without overrides, but from other side below in code we see hook execution:
// Loading controller
$controller = Controller::getController($controller_class);
// Execute hook dispatcher
if (isset($params_hook_action_dispatcher)) {
Hook::exec('actionDispatcher', $params_hook_action_dispatcher);
}
so one of possible solution (without overriding core class) :
how to override module and hope you have core version >= 1.6.0.11
in blockwishlist.php in install() method add
this->registerHook('actionDispatcher')
to condition with other hooks, so it will looks like ... !this->registerHook('actionDispatcher') || ... because this hook not registered by default and we can't just place module there.
create method (can't beautify code here)
public function hookActionDispatcher($params)
{
if ('blockwishlistmywishlistModuleFrontController' == $params['controller_class']) {
include_once(_PS_OVERRIDE_DIR_ . 'modules/' . $this->name . '/controllers/front/mywishlist.php');
$controller = Controller::getController('BlockWishListMyWishListModuleFrontControllerOverride');
}
}
you already have override/modules/blockwishlist/controllers/front/mywishlist.php file by this path
reinstall module.
it works!
more about overriding some behaviors in docs
Turns out that to override a module you dont place your files in:
~/overrides/module/MODULE_NAME
Instead you place it in:
~/themes/MY_THEME/modules/MODULE_NAME
Now the changes are exhibiting themselves.
Does anyone know if/when the module is auto-updated, will my changes get lost/overrwritten?
I'm trying to integrate Fotolia Api with Prestashop 1.6.0.9.
I already make module with custom tab, but I have no idea how set view from module folder for this tab. Sorry to say, but "documentation for developers" SUCKS.
I can't find any working solution.
public function install() {
if (!parent::install()
|| !$this->registerHook('backOfficeHeader')
|| !$this->registerHook('header')
) return false;
$tab = new Tab();
$tab->class_name = 'AdminFotoliaSelector';
$tab->id_parent = 0;
$tab->module = $this->name;
$tab->name[(int)(Configuration::get('PS_LANG_DEFAULT'))] = 'Fotolia Selector';
$tab->add();
return true;
}
I had big problem with make proper controller, and now I just can't load anything/ I have no idea how do this.
<?php
if (!defined('_PS_VERSION_'))
exit;
class AdminFotoliaSelectorController extends ModuleAdminController {
public $name;
public function __construct() {
$this->lang = (!isset($this->context->cookie) || !is_object($this->context->cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($this->context->cookie->id_lang);
parent::__construct();
}
public function initContent() {
parent::initContent();
$this->renderForm();
}
public function renderForm() {
$path = _MODULE_DIR_."fotoliaselector";
$more = $this->module->display($path, 'views/templates/admin/fotoliaselector.tpl');
return $more.parent::renderForm();
}
When I try die($more) it gives me content of .tpl, anyway when I click tab in back office it's still empty.
I have debug options on, compiling on, cache off.
So just enlight me please, how am I supose to show ANYTHING there?
I think the problem is that you don't display tab's content at all.
I don't know what module->display method does, but I think you should change in initContent() method line:
$this->renderForm();
into
echo $this->renderForm();
If it won't help you should look at this documentation and try to do it without external classes - only try to use Smarty to display simple content without using Tab class or AdminFotoliaSelector
Well i know it will sounds weird but you need to take some similar modules, and read his code and will see some methods names are the same in each module.
Then copy that, install and play with some changes etc.
Imho you miss standard method getContent() form where you need to pass some data for smarty:
public function getContent()
{
global $smarty, $cookie;
......
//some code
......
$this->_html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';
$this->_html .= '<h1>My module title or stuff</h1>';
$this->_html .= $this->getMyCoolFormOrConfig();
$smarty->assign('errors', $this->errors);
$smarty->assign('message', $this->message);
$this->_html .= $this->display(__FILE__, 'name_of_tpl_file.tpl');
return $this->_html;
}
to simple add tab in BackOffice code like this:
$id_tab=Tab::getIdFromClassName('AdminPayment');
$newtab=new Tab();
$newtab->id_parent=$id_tab;
$newtab->module=$this->name;
$newtab->class_name='MyClassName'; //will be same like MyClassName.php in folder of you module where you need to create you class and extend the AdminTab and from there with function you need to echo you name module
$newtab->position=Tab::getNbTabs($id_tab)+1;
$newtab->name[$cookie->id_lang]=$this->l("Name of you stuff");
$newtab->name[Configuration::get('PS_LANG_DEFAULT')]=$this->l("Name of you stuff");
$newtab->add();
Study this file there /controllers/admin/AdminModulesController.php
and you see what methods are using in each module
Take a look greater feature to generate you module structure (register requeired) https://validator.prestashop.com/generator
i'm trying to turn off the layout for some controllers in zfc-admin. Unfortunately all the methods i've found does the exact opposite. turns off the views and loads the layout.
Eg.
$viewModel = new ViewModel();
$viewModel->setTerminal(true);
return $viewModel;
Is something in the configuration of ZfcAdmin that disturbs the usual functionality of setTerminal() method ?
As another temporary solution, you can edit ZfcAdmin\Module.php to fix this bug. Change like this:
public function selectLayoutBasedOnRoute(MvcEvent $e)
{
$app = $e->getParam('application');
$sm = $app->getServiceManager();
$config = $sm->get('config');
if (false === $config['zfcadmin']['use_admin_layout']) {
return;
}
$match = $e->getRouteMatch();
if (!$match instanceof RouteMatch || 0 !== strpos($match->getMatchedRouteName(), 'zfcadmin')) {
return;
}
$layout = $config['zfcadmin']['admin_layout_template'];
$controller = $e->getTarget();
if( ! $controller->getEvent()->getResult()->terminate() ) // Add by Vinicius Garcia, to fix ->setTerminal() bug (https://github.com/ZF-Commons/ZfcAdmin/issues/8)
$controller->layout($layout);
}
Just add the if( ! $controller->getEvent()->getResult()->terminate() ) before set the layout will solve the problem.
of course, It's a bad practice change code of a third-party module, but I guess it's better than include extra code in all your views that need this...
When ZF-Commons fix the bug you can just override the module, using their solution.
The code you provide in your question disables layout rendering and only outputs the view of the action.
Can you clarify your question..?
As a temporary solution, following Jurian Sluiman comment, when you want to disable layout you can simply add a get parameter to the ( /?disableLayout=true) when you call a needed action, and in the layout
if (isset($_GET['disableLayout']) && $_GET['disableLayout'] == 'true') die();
or something similar (adjust to your needs)
I am creating custom content pages using this function in my template file
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type)) {
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}
Then I am creating a custom page--"content_name".tpl.php file for my content. However this also overrides the edit, moderate, track pages for that content as well. I only want it to override the main content page. Is there an easy way to do this.
Change your code to be something like:
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type) && is_null(arg(2))) { // the edited line
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}