The Magento backend allows you to disable module output per site. I've done some Google searches but can't figure out how to grab this value through my code. Basically when my module's output is disabled that works just fine. But I've discovered (the hard way) that Magento doesn't prevent the module from loading per-site.
Because I'm extending some core classes, some constructors are still being executed. My thought is to check if the module output is disabled. If so, have my constructor call the parent's constructor. If the module output is enabled, proceed with my custom code.
I just can't figure out how to grab this value for the current site (I am multi-sited, BTW). Ideally it would be something like this:
$isThisEnabled = Mage::app()->getCurrentStore()->isOutputEnabled('myModule');
Basically have a single line that fetches the current site's value (or the default, if not specified for the current site).
Any help would be greatly appreciated!
EDIT: I found the table core_config_data, which appears to store this information. I could manually query it if I had to, but I feel like Magento would have something built-in to return the current store's value, falling back to the default value.
It is a standard config setting, so accessing it should be no different than accessing any other config setting. You just need to know what is the path to this value. Analysing the DB I believe this should do the trick:
Mage::getStoreConfig('advanced/modules_disable_output/Namespace_Module');
The other option is that the Mage_Core_Helper_Abstract has an isModuleEnabled($moduleName = null) method, which means that you should be able to call:
Mage::helper('core/data')->isModuleEnabled('Namespace_Module')
There is also a isModuleOutputEnabled() method. Looking at the code, these don't seem to be filtered by store/view, whereas #silvo's method is.
<?php
public function mycontrollerAction()
{
$moduleName = 'Namespace_Modulename';//eg Mage_Cms
if(Mage::getConfig()->getModuleConfig($moduleName)->is('active', 'true'))
{
echo "Module Enable";
}
else
{
echo "Module Disable";
}
}
?>
Related
I am trying to give a shot to HMVC in Codeigniter. Here is my folder structure.
-ROOT
--APPLICATION
---MODULES
----Module_Email
-----Controllers
-----Models
-----Views
-----Assets
------JS
------CSS
------IMG
To render the Module i have to use
Module::run('Module_Email');
This method will output the rendered output, an example is given below
<script type="text/javascript" src="PATH/TO/EMAIL_MODULE/JS/JS_FILE.JS"></script>
<div data-module-name="Module_Email" class="Email_wrapper">
//RENDERED HTML CONTENT
</div>
Now here my problem start. Normally i would like to put all my resources to header. So when i call any module, its dependence need to be added in header instead of from where its get called.
I searched a lot but i couldn't find any good methods.
Please help.
Update
Currently i have a function on my header called get_assets() which will output predefined resources to header. But i cant say which modules is going to use in pages, so the system need to check which modules are used in this page, and if its used then its dependencies need to be added on header.
Seems like your main problem then is trying to figure out what modules were used.
Unfortunately as far as I can tell with the default Wiredesignz modular extension there is no way to access the module name unless you write some sort of hack to get at that data. The module being used is stored in the protected variable $module in the MX_Router class, however, there is no public method to allow you to get access to it. So your only choice would be to extend the class and create a public function.
Alternatively you could use a forked version of Wiredesignz implementation which I did which provides numerous other features including a public function to get at the $module variable. Using the forked version I wrote you could then use code such as this:
<?php $module_name = $this->router->fetch_module(); ?>
However, that will only record the last module you loaded, so you would still need to do work to store all the modules, and then have your function use this information to determine what assets to load. If I were doing something like you I would probably fork my version and then create an additional data structure to store every module that was loaded that you could then later get access to.
I don't think this is exactly what you were hoping for, but might be something to get you on the right track to finding a solution.
I added an array to the Module class to store the assets and two functions to store/retrieve the items. Here is the source (updated Modules.php)
# Register your assets
public static function register_asset( $asset )
{
if( in_array($asset,self::$assets) === FALSE )
{
self::$assets[] = $asset;
}
}
public static function assets()
{
return self::$assets;
}
and now you can register your assets like this inside your module
Modules::register_asset('myslider.js');
You can retrieve all your assets using
Modules:assets();
Which will return an array of assets that can be processed depending up on the situation.
I have a system.xml in my module, which starts with this:
<config>
<sections>
<dev>
<groups>
<my_module>
<label>...
I want to get the value of this label, from a different module. How do I do it? My first thought, was Mage::getConfig('sections/dev/groups/my_module/label'), but this doesn't work - it seems the <sections> area of the config is not accessible. I also can't figure out where magento is loading this value, which it must do at some point, or it wouldn't be able to display it.
To be completely clear: I am not trying to get the config data value as stored in the core_config_data table, that's no trouble. I want to be able to get the other attributes relating to it - like the group label, or the sort order of the fields, and to do that I need to be able to read the <sections> area of the config.
The system.xml files are never merged with the global configuration. They're only loaded when Magento builds the user interface for the
System -> Configuration
section of the backend admin application. Other than that the application has no use for them.
If you want to grab the label, you'll need to load the full system.xml configuration yourself. Something like this should work.
//load and merge `system.xml` files
$config = Mage::getConfig()->loadModulesConfiguration('system.xml');
//grab entire <sections/> node
var_dump($config->getNode('sections')->asXml());
//grab label from a specific option group as a string
var_dump((string)$config->getNode('sections/dev/groups/restrict/label'));
As mentioned in another answer in this thread, there's also an adminhtml/config model class which wraps some of this logic in a getSection method, so you could do something like this.
Mage::getSingleton('adminhtml/config')->getSection('dev')->groups->my_module->label
If you look at the source of getSection
#File: app/code/core/Mage/Adminhtml/Model/Config.php
public function getSections($sectionCode=null, $websiteCode=null, $storeCode=null)
{
if (empty($this->_sections)) {
$this->_initSectionsAndTabs();
}
return $this->_sections;
}
and follow the call stack through to _initSectionsAndTabs
#File: app/code/core/Mage/Adminhtml/Model/Config.php
protected function _initSectionsAndTabs()
{
$config = Mage::getConfig()->loadModulesConfiguration('system.xml')
->applyExtends();
Mage::dispatchEvent('adminhtml_init_system_config', array('config' => $config));
$this->_sections = $config->getNode('sections');
$this->_tabs = $config->getNode('tabs');
}
You'll see this wrapper method eventually calls the loadModulesConfiguration method itself. The additional applyExtends if an old bit of meta-programming in the configuration you can read about here, which is part of a longer series on configuration loading. (self-links, too long for a StackOverflow answer).
The reason I personally wouldn't use this to grab values out of the configuration is the event that's dispatched when you make this call
Mage::dispatchEvent('adminhtml_init_system_config', array('config' => $config));
This event may trigger code in your system that assumes you're loading the system configuration system in the backend admin console area. If you just want to read the XML tree. simply loading it yourself and reading the values seems the way to go. Your use case, of course, may vary.
As so often seems to be the case, I find the answer moments after posting the question...
This is how to get sections/dev/my_module/label:
Mage::getSingleton('adminhtml/config')->getSection('dev')->groups->my_module->label
As you can see, you need to use Mage::getSingleton('adminhtml/config')->getSection('dev') to get the backend config (you can also use ->getSections() to get all the sections to iterate over). This returns a Mage_Core_Model_Config_Element Object, which is the root of a tree of objects, accessible as shown. Just do a print_r at any stage and you'll see the rest of the tree, which print_r formats like an array, although it's not.
I'm making a custom module for PyroCMS, and I want to get the section menu working with regard to applying the current class. The CMS php, which I don't want to change looks like this:
<li class="<?php if ($name === $active_section) echo 'current' ?>">
When I'm viewing /admin/courses/ this is correct, and the first navigation element has the class, current.
$name is taken from the language file, as set up in details.php.
$active_section is taken from the view, and is equal to
$this->_ci_cached_vars['active_section']
However when I view /admin/courses/chapters/, 'courses' is still determined by the system to be the current section, so the navigation is confusing.
What I need is a way of changing the value of $active_session in the view acording to which function of the controller (index, chapters or pages) is being used.
I've tried changing the value of $this->_ci_cached_vars['active_section'] in each controller function, but that doesn't work. Any ideas?
I'm sure there's something basic I'm missing completely.
Got it.
I'm using multiple methods in one controller, and the 'protected $section = 'courses'; line, which happens before the index method, was setting the section for everything.
It couldn't be set a second time within another method, but there is a way to define a section within a method.
$this->template->active_section = 'section';
Starting my method as follows gave me what I wanted.
public function chapters(){
//Set active section
$this->template->active_section = 'chapters';
...
}
I'm working on my own module. I realize I constantly need to manually type my module name in different places. Most popular usage is with drupal_get_path($type, $name) function (I have more then 10 of these in my code). Where $name is theme or module name. During that time I need to already change my module name 3 times. As you can surmise I also need change all module names hard-coded in my project. So I thought it would be nice to have some convenient function to grab this name automatically.
How can I get machine module name programmatically?
For example if you have your module in following directory..
sites/all/modules/my_module/
..then you can grab it in this way
drupal_get_current_module_name(); // return my_module
Generally, you should know by convention - if you have: sites/all/modules/my_module/ then the machine name of the module should match the folder name - my_module.
Virtually all contributed modules follow this convention, and you should too.
It is possible to have your .info and .module file not match the name of the folder, but this isn't correct.
If you are already executing code inside your module, you should already know the machine name of the module by virtue of the name of the file you're editing - unless you're trying to do something that I'm not understanding.
Edit: Since we've determined you're just trying to call your module's theme function, you don't actually need to know the name.
If you have:
/** Implements theme_table **/
function my_really_long_module_name_table() {}
Your function might get called like this:
theme('table');
There is a little more to it than that, but the theme engine will make a determination about which theme functions get called based on what is implementing them.
It sounds like you may want to read up on some of the basics of the Drupal theme system.
Here's a good start for learning the Drupal 6 theme layer: http://drupal.org/node/165706
I figure out something like this:
function get_current_module_name() {
return array_shift(explode('.', end(explode(DIRECTORY_SEPARATOR, __FILE__))));
}
but don't know is't the best way to do it..
UPDATE:
I see now it's better to use basename
$name = basename(__FILE__, '.module');
UPDATE 2:
I think if this is needed across whole module then it could be accessible via constant defined in the very beginning of the module e.g.:
define('MODULE_NAME', basename(__FILE__, '.module'));
Then you could use all the time in all your function like this:
drupal_get_path('module', MODULE_NAME);
I am trying to get the ID of the product that was most recently added to a user’s cart. A quick google search revealed this function
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
which is also used in Mage_Checkout_Block_Cart_Crosssell. When I try calling the function in my own controller however, it returns nothing.
I have tried to instantiate a core session via
Mage::getSingleton('core/session', array('name'=>'frontend'))
however this approach does not seem to work. I also tried to create the Crossell block and making the protected method that wraps around getLastAddedProductId function public however that returns null just like it does when I try calling it on its own.
Is there something I have to call or instantiate in order to use this function? Here’s my source listing for reference.
class Mymodule_Addcartaftermath_ItemaddedController extends Mage_Core_Controller_Front_Action {
public function generatemessageAction() {
$parameters = $this->getRequest()->getParams();
if (isset($parameters['ajax_call'])) {
$latest_item_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$response = array('response' => $latest_item_id);
echo json_encode($response);
} else {
$this->_redirect('/');
}
}
}
I tried poking through the source code, particularly the checkout/model/session.php file in the core and I cannot seem to find the definition of the function. I also looked at it’s parent’s class definition but could not find it there either.
If this method is not available to me is there another way of retrieving the most recent item added? I know that the items are added sequentially and I could perhaps just get the last item of the list of items from the cart however this would not work in the case where the user adds the same item to the cart essentially increasing the quantity rather than actual item itself (e.g. the user adds a laptop the cart when there already is one)
The call to
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
Is actually clearing the session variable after it is read. Magento uses magic methods extensively. In this case you are using the __call magic method which in turn uses the getData() method. In Mage_Core_Model_Session_Abstract_Varien you will see that they override the default behaviour of getData() to expect the second parameter to be a boolean (The first parameter to getData is the key name for the value you are looking for). That boolean is a flag telling the session to clear the variable after reading.
You could always listen for the checkout_cart_product_add_after event and add the item to your own variable in the session. That event is actually fired on the line before setLastAddedProductId() is called.
try to grep the variable you are looking for. As they are coming from magic methods then its hard to find the exact function you are after so it's easier to see the places where data gets set than where it is used
grep '>setLastAddedProductId' app/code -rsn
to see where the product id gets set to that session variable
app/code/core/Mage/Checkout/Model/Cart.php:255: $this->getCheckoutSession()->setLastAddedProductId($product->getId());
and then you can ask this variable (if it is set else empty())
Mage::getSingleton('checkout/session')->getLastAddedProductId();
and you can see all the things that are in checkout/session and verify if the data is there.
var_dump(Mage::getSingleton('checkout/session'));
Haven't a clue why it works this way but it works for me in Magento 1.6...
<?php
require_once ( "app/Mage.php" );
umask(0);
Mage::app("default");
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$lastadded = $session->getData("last_added_product_id");
print_r($lastadded);
Apparently you have to instantiate the core/session and then the checkout/session. I've tried it every other way but this is the only way I've found it to work. Perhaps someone can explain why it works this way. Hope this helps you out!