I've created a module that overrides an AdminProductController.php and make a new bulk_action.
<?php
class AdminProductsController extends AdminProductsControllerCore
{
public function __construct()
{
parent::__construct();
$this->bulk_actions['setprice'] = array(
'text' => $this->l('Set a price for selected'),
'icon' => 'icon-price',
);
}
}
Now I need to translate the action text and distribute that translation with module.
The problem is that I don't see the original text inside modules translation instead it is visible in back-office translations.
So, is there any way to add this string to module translations not to back-office translations?
You can do it by creating an instance of a module you want the translation to be in.
class AdminProductsController extends AdminProductsControllerCore
{
public function __construct()
{
parent::__construct();
$module = Module::getInstanceByName('modulename');
$this->bulk_actions['setprice'] = array(
'text' => $module->l('Set a price for selected'),
'icon' => 'icon-price',
);
}
}
The main problem description I've found here: How to get translation from other module in PrestaShop?
This is because translations controller scans for $this->l((.*)) inside module folder using regex and adds the translatable strings to a file
So we should in module do something like this:
class MyModule extends Module
{
public static $l = null;
public function __construct()
{
parent::__construct();
$this::$l = $this->l('Set a price for selected');
}
}
Than in controller we can do what was suggested by #TheDrot:
class AdminProductsController extends AdminProductsControllerCore
{
public function __construct()
{
parent::__construct();
$module = Module::getInstanceByName('modulename');
$this->bulk_actions['setprice'] = array(
'text' => $module->l('Set a price for selected'),
'icon' => 'icon-price',
);
}
}
Try using the following code in place of $this->l('Set a price for selected')
Translate::getModuleTranslation(YOUR_MODULE_NAME, 'Set a price for selected', FILE_NAME);
Related
I need interacts with a .tpl file in my adminController class, but when I try to do that, this error appears
Fatal error: Call to undefined method RiddlePageController::getCacheId() in /home/USER/public_html/prestashop/modules/RiddleModule/controllers/admin/RiddlePage.php on line 48
This is my admin controller code:
class RiddlePageController extends AdminController {
public function __construct()
{
$this->html = '';
$this->display = 'view';
$this->meta_title = $this->l('metatitle');
$this->module = "RiddleModule";
parent::__construct();
}
public function initContent()
{
$this->postProcess();
$this->show_toolbar = true;
$this->display = 'view';
$this->meta_title = $this->l('Modulo');
parent::initContent();
}
public function initToolBarTitle()
{
$this->toolbar_title = $this->l('Titulo');
}
public function initToolBar()
{
return true;
}
public function renderView() {
$this->context->smarty->assign(
array(
'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
'img2' => "http://www.all3dmodel.com/Images/39.jpg"
)
);
// in return have error "getCacheId"
return $this->display(__FILE__, 'content.tpl', $this->getCacheId());
// return "<b>This works fine!!</b>";
}
my tpl file have only {$img1} and {$img2} for testing.
Maybe I do all wrong, and this is not the best way to make in my own admin page.
Your error is because the AdminController class doesn't have the getCacheId method.
To answer to your question you have to made some little fix.
First (extends ModuleAdminController not AdminController):
class AdminRiddlePageController extends ModuleAdminController
{
}
Then if you want to view your custom tpl, place a view.tpl file in:
prestashop/modules/RiddleModule/views/templates/admin/riddlepage/helpers/view/
or
prestashop/modules/RiddleModule/views/templates/admin/riddle_page/helpers/view/ (I don't remember well if the underscore is necessary)
And your renderView method should be like this:
public function renderView()
{
/* Your code */
/* Use this snippet to assign vars to smarty */
$this->tpl_view_vars = array(
'myvar' => 1,
'secondvar' => true
)
return parent::renderView();
}
AdminController class has not an implementation of display method you use to render TPL.
You can use something like this after set module var:
$this->module->display(_PS_MODULE_DIR_.$this->module->name.DIRECTORY_SEPARATOR.$this->module->name.'.php', 'content.tpl')
Good luck.
As #TheDrot told us, the answer are in using $this->context->smarty->fetch(location), but not in renderList, but in the return statement of renderView is OK and prestashop get the tpl file and load correctly the smarty variables. Ex:
public function renderView(){
$this->context->smarty->assign(
array(
'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
'img2' => "http://www.all3dmodel.com/Images/39.jpg"
)
);
return $this->context->smarty->fetch(_PS_MODULE_DIR_ . "RiddleModule/controllers/front/prueba.tpl");
}
The file location isn't important to load the TPL file in this case
I'm writing a subscribe module to plugin to the SilverStripe Blog module. So far I have my yml as:
---
Name: subscription
After: 'framework/*','cms/*'
---
Blog:
extensions:
- Subscription
Page_Controller:
extensions:
- SubscriptionWidget
And my SubscriptionWidget.php:
<?php
class SubscriptionWidget extends DataExtension {
public function SubscriptionWidget() {
$controller = SubscriptionWidget_Controller::create();
$form = $controller->SubscriptionWidget();
return $form;
}
}
class SubscriptionWidget_Controller extends Controller {
private static $allowed_actions = array('SubscriptionWidget');
public function SubscriptionWidget () {
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('Email', 'Email'),
TextField::create('Name', 'Name')
),
FieldList::create(
FormAction::create('submit', 'Subscribe')
)
);//->setTemplate('SubscriptionWidget');
return $form;
}
public function submit($data, $form) {
return $this->redirect('/subscribed');
}
}
At the moment this works as intended however another plugin I use called BetterNavigator disappears from the screen. If I take out
Page_Controller:
extensions:
- SubscriptionWidget
from my yml it reappears. I've looked through both code bases which are quite simple and there are no conflicting functions. I've also tried using ContentController instead of Page_Controller and my template disappears until I disable BetterNavigator and then it reappears. I do have one or two pretty empty classes but all are called some variation of Subscriber while there is only one function in BetterNavigator called BetterNavigator.
Why would this be happening?
I see only one clash in your code that results in incorrect runtime behaviour. Your method SubscriptionWidget::SubscriptionWidget() is treated as legacy class constructor. So I suggest you thinking about better class and method names.
class SubscriptionWidget extends Extension
{
// explicitly defined constructor
public function __construct() {
parent::__construct();
}
// now this one is normal function
public function SubscriptionWidget() {
$controller = SubscriptionWidget_Controller::create();
$form = $controller->SubscriptionWidget();
return $form;
}
}
I've been trying to make an extension to add some function to the CMS. As it's a setting for the CMS I've added it to the settings tab. While I can take values and save them I needed an action on the page to synchronise a system and I can't get my action to be called, here is my code.
private static $db = array(
'Path' => 'Varchar(50)',
);
private static $allowed_actions = array (
'update',
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldsToTab('Root.Importer', array(
ImporterPathField::create('Path', 'Path')->setDescription('Path to area'),
FormAction::create('update', 'Synchronise')
));
}
public function update() {
SS_Log::add_writer(new SS_LogEmailWriter('test#example.com'), SS_Log::ERR);
}
It doesn't get called. If I need to add the function to the left nav rather than part of the settings I'm ok with that too but I also tried that with even less success. Is it possible to get the action called on button press?
You need to place the $allowed_actions and the update method in an extension for CMSSettingsController. Also you should probably put the FormAction into the CMSActions list.
Here's how I would do this:
SiteConfigExtension.php
class SiteConfigExtension extends DataExtension
{
private static $db = array(
'Path' => 'Varchar(50)',
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldsToTab('Root.Importer', array(
ImporterPathField::create('Path', 'Path')->setDescription('Path to area')
));
}
public function updateCMSActions(FieldList $actions)
{
$actions->push(
FormAction::create('update', 'Synchronise')
);
}
}
CMSSettingsControllerExtension.php
class CMSSettingsControllerExtension extends DataExtension
{
private static $allowed_actions = array (
'update',
);
public function update() {
SS_Log::add_writer(new SS_LogEmailWriter('test#example.com'), SS_Log::ERR);
}
}
I currently have a widget inside a module and I would like to create a messages directory so that I can translate module strings from inside that folder.
The issue I have is that I can't get the widget to get the translations from inside the module folder.
This is my structure
frontend/modules/comments
frontend/modules/comments/Module.php
frontend/modules/comments/widgets/CommentsWidget.php
frontend/modules/comments/widgets/views/_form.php
-Module.php
namespace frontend\modules\comments;
use Yii;
class Module extends \yii\base\Module {
public $controllerNamespace = 'frontend\modules\comments\controllers';
public function init() {
parent::init();
$this->registerTranslations();
}
public function registerTranslations() {
Yii::$app->i18n->translations['modules/comments/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '#frontend/modules/comments/messages',
'fileMap' => [
'modules/comments/comments' => 'comments.php',
]
];
}
public static function t($category, $message, $params = [], $language = null) {
return Yii::t('modules/comments/' . $category, $message, $params, $language);
}
}
_form.php
I use the following code to call the translation.
Module::t('comments', 'COMMENT_REPLY')
But it doesn't seem to work. Any ideas?
Thank you in advance!
Have same problem.
It's because you register translations is in init() method (when instance of module was created). You can change registerTranslations() method to static:
public function init()
{
// ...
self::registerTranslations();
}
public static function registerTranslations()
{
// ...
}
And call it Content::registerTranslations(); in your widget before use Module::t().
I made a new button in adminhtml by extending *Mage_Adminhtml_Block_Sales_Order* and now i want to have some functionality in it.
class Module_Parcel_Block_Adminhtml_Sales_Order extends Mage_Adminhtml_Block_Sales_Order
{
public function __construct() {
$this->_addButton('Parcel Sync', array(
'label' => Mage::helper('Sales')->__('Parcel Sync'),
'onclick' => 'window.open(\'/magento/app/code/local/Module/Parcel/Controller/Sync.php\')',
'class' => 'go'
), 0, 100, 'header', 'header');
parent::__construct();
}
}
You can see here how i implement my button, the onclick function is wrong because i can not acces my controller like that. How do i solve this?
EDIT this is my controller
class Module_Parcel_IntegerController extends Mage_Core_Controller_Front_Action
{
public function multiplyAction()
{
echo 'Works';
}
}
Do i need to add this controller to config.xml to get this code working? (thx to magik)
Mage::helper('adminhtml')->getUrl("Module_Parcel/adminhtml_controller/sync");
You can use following -
Mage::helper('adminhtml')->getUrl("modulename/adminhtml_controller/action");