How to load module functions in custom Drupal theme - module

I'm not familiarly working with Drupal module. I installed,configured vegas module. There no problem in module. My actual question is how to i fetch module's PHP function in my custom.
For instance, vegas module only work on existing theme but it not working on my current theme (A known issue). I printed all images from inside vagas module and it gave me expected result.
print_r($backgrounds);
Array
(
[0] => Array
(
[src] => http://localhost/drupal/sites/default/files/vegas/slider1.jpg
)
[1] => Array
(
[src] => http://localhost/drupal/sites/default/files/vegas/slider1_0.jpg
)
[2] => Array
(
[src] => http://localhost/drupal/sites/default/files/vegas/slider1_1.jpg
)
)
My bad part is i cannot print that array inside my custom theme. How to get it.

Only the functions defined in module's .module file can be invoked from the theme. Please check if the function you are trying to use is in any other file of module.

Related

How to pass parameters to script file under shopify using php?

I want to pass parameters to script.js file that is included in the script_tags.json file of shopify.
I have mentioned below the code to add script file in script_tags.json file.
$script_array = array(
'script_tag' => array(
'event' => 'onload',
'src' => 'https://www.softwareprive.com/shopifyapps/ngdesk/js/script.js'
)
);
$scriptTag = shopify_call($token, $shop, "/admin/api/2019-10/script_tags.json", $script_array, 'POST');
You don't pass parameters to scripts. They execute after the DOM has loaded. So your script code can access data that was prepared for your script to use. That would mean providing some Liquid and/or other theme code. Otherwise, you are left with doing scripting by introspection of the DOM and hoping you sniff out the gold you are looking for.

Prestashop 1.6 Create Module to Display Carrier Filter

My Prestashop-based site is currently having an override for AdminOrdersController.php, I have placed it in override folder.
From the link provided below, it is perfectly working fine to add a Carrier filter which is not available in Prestashop 1.6 now. I have tried the solution and it is working perfectly.
Reference: Adding carrier filter in Orders page.
Unfortunately, for production site, I have no access to core files and unable to implement as such. Thus, I will need to create a custom module. Do take note that I already have an override in place for AdminOrdersController.php. I would like to tap on this override and insert the filter.
I have managed to create a module and tried placing an override (with the code provided in the URL) in mymodule/override/controller/admin/AdminOrdersController.php with the carrier filter feature.
There has been no changes/effect, I am baffled. Do I need to generate or copy any .tpl file?
Any guidance is greatly appreciated.
Thank you.
While the answer in the linked question works fine the same thing can be achieved with a module alone (no overrides needed).
Admin controllers have a hook for list fields modifications. There are two with the same name however they have different data in their params array.
actionControllernameListingFieldsModifier executes before a filter is applied to list.
actionControllernameListingFieldsModifier executes before data is pulled from DB and list is rendered.
So you can add fields to existing controller list definition like this in your module file:
public function hookActionAdminOrdersListingFieldsModifier($params) {
if (isset($params['select'])) {
$params['select'] .= ', cr.name';
$params['join'] .= ' LEFT JOIN `'._DB_PREFIX_.'carrier` cr ON (cr.`id_carrier` = a.`id_carrier`)';
}
$params['fields']['carrier'] = array(
'title' => $this->l('Carrier'),
'align' => 'text-center',
'filter_key' => 'cr!name'
);
}
Because array data is being passed into $params array by reference you can modify them in your hook and changes persist back to controller. This will append carrier column at the end of list.
It is prestashop best practice to try and solve problems through module hooks and only if there is really no way to do it with hooks, then do it with overrides.
Did you delete /cache/class_index.php ? You have to if you want your override to take effect.
If it still does not work, maybe you can process with the hook called in the AdminOrderControllers method with your new module.

using yii rest api with default URL GET format rather than PATH format

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.
So in my config/main.php i have the following setting
'urlManager' => array (
'urlFormat' => 'path',
'rules' => array (
'student/<id:\d+>/<title:.*?>' => 'student/view',
'students/<tag:.*?>' => 'student/index',
array (
'apistudent/register',
'pattern' => 'api/<model:\w+>',
'verb' => 'POST'
),
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
)
),
I also have a controller named ApiStudentController with a method called actionRegister().
So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.
The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?
I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file
$app = Yii::createWebApplication($env->configWeb); //store the app
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one.
if (isset($_GET['r'])) {
Yii::app()->urlManager->setUrlFormat('get');
}
$app->run(); //run the app
I dont know whether this solves your problem. But this can give you an idea. Happy Coding!

app can't find module?

I am working on a project. It was fine until I installed new windows on my PC. But now project is same and when I access
Yii::app()->controller->module
It returns null. It also returns null for: Yii::app()->controller->module->id. When I viewed config file it had admin module in it. I don't know why is it's returning null. Can't find a way out.
Module in config file is like:
'modules' => array(
// uncomment the following to enable the Gii tool
'admin',
'gii' => array(
'generatorPaths' => array(
'bootstrap.gii'
),
'class' => 'system.gii.GiiModule',
'password' => '1234',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters' => array('127.0.0.1', '::1'),
),
),
When you access Yii::app()->controller->module then it will return the module that current controller belongs to. It returns null if the controller does not belong to any module. Please make sure the current controller you accessed which is belong to any module you configured.
You can see this link: http://www.yiiframework.com/doc/guide/1.1/en/basics.module to work with Module.
If you want to see which modules are loaded then use:
print_r(Yii::app()->getModules());
You are not 'in' a module and want to get a specific module (like admin module): you can do
Yii::app()->getModule('admin');

How to create a smarty variable in prestashop 1.5

I am working on a button that switches view with onClick. I wish to store the last/default position in a variable in order to prevent switching to the default view state on each page refresh or navigation.
I read that I can do the following in a php file:
$myVar= -1;
$smarty->assign('myVar', $myVar);
and then use $myVar in the tpl file. But it does not work for me.
The tpl file I am working on is not part of a module and has no .php file in the prestashop root folder.
Can anyone educate me a little on smarty/php and how to create variables and use them to store button's state?
Thanks
Smarty is a PHP template engine for PHP, which facilitates the separation of presentation (XHTML/CSS) from the PrestaShop's core functions/controllers.
A template file (usually with a .tpl extension in PrestaShop) is always called by a PHP controller file (it can be a Front-end core controller or a module controller).
Example: /prestashop/controllers/front/ContactController.php
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
We can see that this file is retrieving information from the database and assigning it to Smarty.
Then, the 'contact-form.tpl' template will display it to the visitors.
The syntax is pretty similar for modules,
example:/prestashop/modules/blocklink/blocklink.php
public function hookLeftColumn($params)
{
$this->smarty->assign('blocklink_links', $this->getLinks());
return $this->display(__FILE__, 'blocklink.tpl');
}
Also, to store values in Smarty variables, you can use the 'assign' function in two ways:
$this->context->smarty->assign('my_smarty_variable_name', $my_value);
or if you have several variables:
$this->context->smarty->assign(array('my_smarty_variable_name1' => $my_value1), ('my_smarty_variable_name2' => $my_value2));
And then in the Smarty template:
The value of my variable is {$my_smarty_variable_name|escape:'htmlall':'UTF-8'}.
The 'escape' modifier is used to avoid XSS security issues.
In order to use variables in your smarty file, you need to use for example :
$this->context->smarty->assign(
array(
'myVar' => $myvar,
'otherVar' => $otherVar
)
);
Then to use it in your tpl file you simply need to use :
<div>my var = {$myVar}</div>
To use a variable in your smarty you need to write it inside {}.