Thursday, 9 July 2015

Magento: Enable/disable template/block Hints Using MySQL



Enable/disable template/block Hints Using MySQL

When doing frontend development with Magento, there are many times when I want to turn on template/block hints for just one page load. I don’t like having to wait on the Magento admin to load up the System > Configuration > Developer page, switch to Website view, and then turn on template/path hints. I have written a couple of MySQL snippets that allow me to enable/disable hints with one query.
I wrote the scripts so that for clean Magento installs, they will insert the necessary records into the core_config_data table. For Magento installs that have had the hints enabled/disabled, the record will already be present in the config table, so the script will just update that record.

Enable template/block hints for the first* website

-- Enable template hints
SET @template_hints = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

Disable template/block hints for the first* website

-- Enable template hints
SET @template_hints = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;
As I covered in this post, it is possible to turn on hints at a global level, which activates hints for all website and for the admin panel. This is useful if you’re making customizations to the admin panel, or if you have multiple websites for which you’re wanting to activate/deactivate hints at the same time. The following two snippets modify hints at a global level:

Enable template/block hints for the admin panel and ALL websites

-- Enable template hints
SET @template_hints = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

Disable template/block hints for the admin panel and ALL websites

-- Enable template hints
SET @template_hints = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;
* If you have multiple websites, you’ll need to update the ‘1’ value set for the website field to reflect the ID of your website. If you have multiple websites, you can just turn on hints globally using the last two SQL snippets.

Magento: Default admin url


  • http://localhost/index.php/admin

What is the Magento Admin Url?

The Magento Admin Url can be different for every installation, but we’ll get you logged into the admin area quickly. The admin url is a field that is defined when Magento is first installed. The default url is just “admin”, but go ahead and append the following urls onto your domain name. We should be able to find the Magento Admin Login quick.
  • /index.php/admin
  • /index.php/administrator
If I were running Magento on this domain I would have tried http://magentosupport.help/index.php/admin. If that didn’t work for you, then you have a customized url. Customized urls are more secure and it’s advised that you use them.
Here’s how to find your Magento Admin Url:
1. Log in to your hosting system
You will want to log into the file system on your server or hosting plan. Unfortunately, I cannot give you the instructions on how to do that, because it’s different for every hosting provider.
If you need assistance locating your administrative url you can always hire us, Magento Support Packages.
2. Locate your WEBROOT
Once you’re able to view the files on your server, you’ll want to navigate to the WEBROOT. The WEBROOT is the directory that holds all of your Magento files. In my case, after I log into my server, the webroot is located in /public_html.
3. Navigate to the local.xml file
Now that you see your Magento installation files you can navigate to the local.xml file which holds all of your sensitive data. The file path to your local.xml is as follows:
/app/etc/local.xml
This is the file path to your local.xml file
This is the file path to your local.xml file
4. Open the local.xml file and grab your magento admin url
Once you have the file open, around line 61 you will see a tag called frontName. Within this tag will be the variable that we need to create your administrative url. You can see in the picture below that my magento admin url is still set to the default value admin. Copy your admin variable from this file, we’ll need it in the next step.
Locating the Magento Admin Url
Locating the Magento Admin Url
5. Create your admin url
We’re basically going to repeat the first step that I asked you to try. Go ahead and append your variable onto the end of your domain like this:
http://example.com/index.php/admin
You should now see that Magento Admin Login page. If you had any problems along the way, or didn’t understand any of the steps, we’re only a phone call away.

Magento: ENABLE TEMPLATE/BLOCK HINTS IN ADMIN PANEL

Enable Template/Block Hints in Admin Panel

Anyone that has developed a theme in Magento knows how helpful template/block hints are. They help quickly identify which files are being loaded for a specific page.
Magento’s admin panel uses the exact same design pattern as the frontend (layouts + blocks + templates). If you’ve ever done any modifications to the Magento admin panel, you’ve probably tried to turn on template/block hints for the admin panel. The only problem is, Magento doesn’t have built-in support for this. I did some digging around and found out how to enable this feature in the admin panel.

Step 1 – Connect to database

Using your favorite database administration tool, connect to your Magento database. These are tools I’ve used and recommend:Navicat ($129), MySQL Query Browser (Free), Sequel Pro (Mac Only – Free), or phpMyAdmin (free).

Step 2 – Enter values into ‘core_config_data’ table

Run the following query on the Magento database:
INSERT INTO core_config_data (scope, scope_id, path, value)
VALUES ('default', 0, 'dev/debug/template_hints', 1),
('default', 0, 'dev/debug/template_hints_blocks', 1);

Step 3 – Test in admin panel

Once you make this addition to the core_config_data database, template/block hints should show up in the admin panel. Here is a screenshot of the CMS Page with hints turned on:


Magento Admin – Hints Turned On

Disabling Hints

When you’ve finished development want to turn off template hints in the admin panel, open the core_config_data table and change the ‘value’ column of the two row you inserted to “0”.

Why does this work?

For those of you who are like me and want to know the “why” as well as the “how”, I’m going to go over why this works. Here is the Magento code that checks to see if template/blocks hints are enabled:
File: /app/code/core/Mage/Core/Block/Template.php (Magento 1.3.2)
public function getShowTemplateHints()
{
    if (is_null(self::$_showTemplateHints)) {
        self::$_showTemplateHints = Mage::getStoreConfig('dev/debug/template_hints')
            && Mage::helper('core')->isDevAllowed();
        self::$_showTemplateHintsBlocks = Mage::getStoreConfig('dev/debug/template_hints_blocks')
            && Mage::helper('core')->isDevAllowed();
    }
    return self::$_showTemplateHints;
}
The “Mage::getStoreConfig” method checks for config values that are set in the current scope (ie, default, website, store, store view). In the admin panel, only values set in the “Default Scope” are loaded.
Magento only allows you to turn on hints when you’re in the configuration scope of a Website or Store View. This means that when the code above tries to load the configuration, it returns “null” because that config value isn’t set in the “Default Config” scope. Running the MySQL query above adds the hint config values to the “Default Config” scope. Here is a screenshot showing the settings for turning on the hints – notice that the “Main Website” configuration scope is selected.

Magento Config Scope Selection

Conclusion

I hope this helps you in your development of Magento modules that integrate with the admin panel. I considering writing a module to enable the hints to be turned on in the “Default Scope”, but decided against it, due to the simplicity of the method outlined in this post.

MAGENTO DEBUGGING – HOW TO DEBUG TEMPLATE PATHS, LOGGING AND DISPLAY ERRORS

MAGENTO DEBUGGING – HOW TO DEBUG TEMPLATE PATHS, LOGGING AND DISPLAY ERRORS

Developing in Magento without these debugging tips can be challenging! Here is how you can debug magento:
Turn on Template Path Hints
Turn on error/system logging
Turn on SQL logging
Display PHP errors
Turn on Template Path Hints
Would you like to see the path of the file that is producing the HTML for each block on the page?
1. Log into the admin
2. Goto -> System ->Configuration, and on the bottom of the list select “Developer”
3. click on Debug – you will only see “Profiler”. Here is the trick – you have to switch the “Current Configuration Scope:” [in the upper left] to a website instead of default. Select “Main Website”.
4. Now you will see the selection for Template Path Hints – check yes. This will display the path of the template for each block of the page so you can find stuff! You can also choose to “Add Block Names to Hints” to see the corresponding model class for the block.
Turn on error/system logging
1. In the admin Goto -> System ->Configuration, and on the bottom of the list select “Developer”
2. Select the Log Settings tab and put Enabled = Yes
3. *to avoid any permissions issues, create folder/file
/var/log/system.log
Turn on SQL logging
To enable SQL Debugging find the file
/lib/Varien/Db/Adapter/Pdo/Mysql.php
change line 45
protected $_debug = false;
to
protected $_debug = true;
It will then generate the file var/debug/sql.txt.
*If you have any issues create this file manually first…
Display PHP errors
Frustrated you can’t see your PHP errors? This is how you turn them on – for development use only, don’t do this on a production site!
In Index.php change
Mage::setIsDeveloperMode(true);
ini_set(‘display_errors’, 1);

Error: Magento block template file not loaded

Shelter Job log: Error- Magento template - SQLSTATE[28000] [1045] Access denied for user 'magento'@'localhost'


  1. Error: SQLSTATE[28000] [1045] Access denied for user 'magento'@'localhost'
    1. Adding a user in http://visstudiodev2/phpmyadmin
    2. Magento database
    3. Adding a new user to the Privilege user settings 
    4. I deleted everything inside the var/cach. And it worked for me. "So all i did was rename /magento-path-here/var/cache folder to /magento-path-here/var/cache-old and refreshed the page and all was fine." ^Fixed the problem first go. Thanks mate you're a legend!
  2. Error: ( ! ) Notice: Undefined index: HTTP_X_FORWARDED_FOR 
    1. Changing $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; to $ip = $_SERVER['REMOTE_ADDR'];
  3. Error: frontend\default\shelter\template\magazento/homepage/data.phtml. Basically it was showing the template path hints so just disable the path hints
    1. Changing the magento db, core_config_data table, changing where path=template_hints, and path=template_hints_block value from 1 to 0. 
      1. SELECT * FROM `core_config_data`where path='dev/debug/template_hints'; 
      2. SELECT * FROM `core_config_data`where path='dev/debug/template_hints_blocks'
    2. clear the var/cache