During the install of a module, I need to assign a View to a Region, and unassign a Block from that region. It's something that would take 3 seconds in the UI, but this has to be done programmatically.
The view is called 'legal-footer' and it needs to be assigned to the region 'footer'.
Similarly, I have a block called 'footer-logos' that's currently in the 'footer' region but needs to be removed.
I think I want hook_block_info_alter, but I'm not sure if it works on a View, and there's a note in the API docs that it can't be used to unassign a block...
I'm new to Drupal, and I can whatever I want in the UI pretty easily, but I'm having a hard time with the API.
to assign block to a region use 'region' key in the array returned info that contain the name of the region that this block should be assigned to
E.g
function module_block_info() {
$blocks = array();
$blocks[0] = array(
'info' => t('Block Title'),
'region' => 'name-of-the-region', // here is the name of the region
'status' => 1, // 1 if you want the block to be enabled by default
);
return $blocks;
}
and you can Disable exist block by using such query
db_update('block')->fields(array('region' => '', 'status' => 0))->condition('bid', $block_id)->execute();
replace $block_id with the id of the block that you want to disable
UPDATE:
you can use hook_block_info_alter to disable exist block
function hook_block_info_alter(&$blocks, $theme, $code_blocks) {
// Disable the login block.
$blocks['defining_module']['delta']['status'] = 0;
}
good luck
Related
I am looking to add a global variable in smarty.
I added css styles with {$urls.css_url} in stylesheep.tpl
<link rel="stylesheet" type="text/css" href="{$urls.css_url}my.css">
It works very well, except that if I put the shop on debug mode, I have this error:
ContextErrorException in smarty_internal_templatebase.php(157) : eval()'d code line 393: Notice: Undefined index: css_url
What does that mean? that this variable will not be available in smarty?, so if I put the cache, it will not be loaded?
How to make variable {$ urls.css_url} available with smarty? everywhere on the site?
Which code should I put and where to put it so that the variable (url_css) remains available in debug mode?
Thanks for your help
Thank you for your help, you have a high level compared to me.
In fact the variable is already defined and is available everywhere.
In frontcontroler.php, I had this:
$assign_array = array(
'img_ps_url' => _PS_IMG_,
'img_cat_url' => _THEME_CAT_DIR_,
'img_lang_url' => _THEME_LANG_DIR_,
'img_prod_url' => _THEME_PROD_DIR_,
'img_manu_url' => _THEME_MANU_DIR_,
'img_sup_url' => _THEME_SUP_DIR_,
'img_ship_url' => _THEME_SHIP_DIR_,
'img_store_url' => _THEME_STORE_DIR_,
'img_col_url' => _THEME_COL_DIR_,
'img_url' => _THEME_IMG_DIR_,
'css_url' => _THEME_CSS_DIR_,
'js_url' => _THEME_JS_DIR_,
'pic_url' => _THEME_PROD_PIC_DIR_,
);
I then added the next line below to assign it in smarty.
$this->context->smarty->assign(array('urls' => $urls));
The variable is displayed, no problem but if I put the shop in debug mode:
/* Debug only */
if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', true);
}
I always have this:
ContextErrorException in smarty_internal_templatebase.php(157) : eval()'d code line 393: Notice: Undefined index: css_url
Does this indicate that there is no value? Why does its value disappear? (when I activate PS_MODE_DEV)
Thanks again.
{$urls.css_url} means to retrieve the the css_url value from the urls array.
Also the Notice you receive means the variable has no value, so it can't work at all, so maybe what you think is correct, but what you see comes from other variable / hook.
How to set a Smarty Variable
In case you want to set a smarty variable you will have first to assign it.
You can do it through a module, controller or a TPL.
From a module or a controller it will be available as soon as you declare it. To print or to debug it.
So, for example if you add it in the hookDisplayHeader or in actionFrontControllerSetMedia hooks the variable will available where you want to use it.
Also, to assign the value in a module or controller you will have to use a code like this:
$urls = array (
'css_url' => 'The URL to your CSS'
...
);
$this->context->smarty->assign(array('urls' => $urls));
Then you will be able to access it using {$urls.css_url}
How to know the already defined variables in Smarty
Enable the debug mode of your shop and then just add {debug} in any of your TPLs, this will show a Pop Up (remember to allow popups from your URL) with all the variables assigned to Smarty.
Maybe this is what you are looking for:
Version: Prestashop 1.7.2.4
We are going to do this overriding a class, in this case the following file: classes/controller/FrontController.php, this file assigns global variables for Prestashop.
Create a file: override/classes/controller/FrontController.php
Copy all the contents from: classes/controller/FrontController.php to our recently created file.
Check the following line: 1444, there is a function called getTemplateVarUrls(), inside this function there is already a variable css_url that points to theme css default dir : /public_html/themes/theme_name/assets/css
If you want to create a custom url variable write the code there, i.e:
public function getTemplateVarUrls()
{
$http = Tools::getCurrentUrlProtocolPrefix();
$base_url = $this->context->shop->getBaseURL(true, true);
//custom css url var points to a folder called "my_custom_css" in the root of the project
$customCssFolderUrl = $base_url.'/my_customs_css';
$urls = array(
'base_url' => $base_url,
'current_url' => $this->context->shop->getBaseURL(true, false).$_SERVER['REQUEST_URI'],
'shop_domain_url' => $this->context->shop->getBaseURL(true, false),
'custom_css_url' => $customCssFolderUrl ,
);
After we finish all this steps we proceed to delete public_html/app/cache/prod/class_index.php file, this to prevent Prestashop from ignoring our new file, to read more about this: http://doc.prestashop.com/display/PS16/Overriding+default+behaviors
Use your new variable: {$urls.custom_css_url} on any .tpl file
Hope this helps.
Given a page retrieved at for example:
http://myapp.dev/path/subfolder?param=abc
Whenever the additional GET parameter called param is present it should be added automatically to all subsequent links in my navigation as constructed in the .volt template. For example:
Go to subfolder 2
I.e. based on this .volt link the the goal is to generate:
Go to subfolder 2
If you want to append Query string parameters only for given links you can go with Luke's solution.
However I think you want to achieve something a bit different and it involves custom logic. For this to happen we should create a custom Volt function.
Custom function definition:
public static function urlFor($params, $queryStringParams = [])
{
$di = \Phalcon\DI::getDefault();
if ($di->getRequest()->has('param')) {
$queryStringParams['param'] = $di->getRequest()->get('param');
}
return $di->getUrl()->get($params, $queryStringParams);
}
The above function acts the same as url() function in Phalcon, it just allows us to write a bit of custom logic before passing the parameters to url().
In your case we check if URL contains desired query param and we add it to every URL generated on the current request. In my case the above function is in Helper file so I can use it anywhere I need to.
This is our View service definition:
$di->set('view', function() use ($di) {
$view = new \Phalcon\Mvc\View();
...
$view->registerEngines([
'.phtml' => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$options = [
'compiledPath' => $di->getConfig()->site->path->cache . 'volt/frontend/',
'compiledExtension' => '.php',
'compileAlways' => $di->getConfig()->debug,
];
$volt->setOptions($options);
...
// IMPORTANT PART: Overwriting default url() function in Volt
$compiler = $volt->getCompiler();
$compiler->addFunction('url', function($resolvedArgs, $exprArgs){
return 'Helpers\Common::urlFor(' . $resolvedArgs . ')';
});
return $volt;
}
]);
return $view;
});
Please note the IMPORTANT PART comment in the above code block.
Let us finish with example:
User is on this address:
http://myapp.dev/path/subfolder?param=abc
But somewhere in your code you want to generate a link to News page:
News
Our code will catch the param in the URL and will generate the following address:
http://myapp.dev/news/list?param=abc
I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.
I'm modifying the IterationSummary app from the 2.0RC3 SDK, and adding more iteration info to it. For some reason, I am not able to retrieve the 'Theme' for the iteration, although I am able to query for other fields from the iteration object. Starting with the sample, I simply added the following lines #192
{
cls: 'theme',
html: iteration.get('Theme')
},
I can get 'Name' but I can't get the 'Theme' value even though it is clearly set on the iteration, and I verified that value using the REST API to query that same iteration. And querying other fields such as 'Name' works well. Any idea why 'Theme' is not being returned?
Do you fetch 'Theme' ?
You may see a general example that builds a grid of iterations that fall within a release (selected in the releasecombobox) that has Theme column populated as long as an iteration has a theme entered is in this github repo.
This example is different from the IterationSummary app because in my example I explicitly create Rally.data.wsapi.Store for Iteration object and fetch Theme.
Customizing the IterationSummary app will still require explicit fetching of Theme field, but you are correct that it is not obvious from the IterationSummary app how other fields are being fetched, e.g. State of iteration. The iteration object in that app is returned from this.getContext().getTimeboxScope().getRecord() and if you print out that object in the console, theme will be empty. The fields that exist on this.getContext().getTimeboxScope().getRecord() are limited and cannot be customized for performance reasons.
In order to modify this app to display Theme, the Iteration model has to be accessed and Theme fetched explicitly. Here are the steps I took to modify the app:
added getTheme function:
getTheme: function(){
var iteration = this.getContext().getTimeboxScope().getRecord();
return iteration.self.load(iteration.getId(), {
fetch: ['Theme']
});
}
In rc3 every time when we have a record, .self will give its model, so there is no need to do this manually:
Rally.data.ModelFactory.getModel({
type: 'Iteration',
//...
Note fetch: ['Theme']
Next, inside _addContent method getTheme() is called
return Deft.Promise.all([this.getTheme(), this.calculateTimeboxInfo()]).then({
success: function(results) {
var theme = results[0].get('Theme');
and then finally theme variable's value is passed to:
{
cls: 'theme',
html: theme
}
The full code is available in this github repo.
I'm trying to learn the syntax for DropDownListFor.
Given the following in a for loop:
#Html.DropDownListFor(
model => model.SalutationID, Model.AvailableSalutations.Select(option => new SelectListItem
{
Selected = (option.ID == staff.SalutationID),
Text = option.Desc.ToString(),
Value = option.ID.ToString()
}
),
"Choose...")
... and given that staff.SalutationID does echo correct values (when, for example, I just use #Html.ValueFor(model => staff.SalutationID)), why does every dropdown echoed in my loop default to "Choose..." and not the Selected option?
What decides what is the selected option in the drop down list is the value of the model property specified by the lambda in the first parameter in the Html.DropDownListFor. In your case model => model.SalutationID.
Verify that model.SalutationID has the expected value in the model. If not, specify it in the controller's action, before rendering the view.