Ok so Ive created static blocks in my CMS area, and Im trying to output them inside of a custom homepage template Ive built.
Every document I can find says to output the block as follows
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-block-identifier')->toHtml() ?>
That didnt work for me, so then I tried another way. --
<?php $block = Mage::getSingleton('core/layout')->createBlock('cms/block')->setBlockId('my-block-identifier');
echo $block->toHtml();
All the sites referencing this tell me to use the actual blocks identifier to get the block. So then I decide to manually lookup the block_id in my cms_block table and see if using the block_id number in place of the literal my-block-identifier name will work - and it did. So I am confused... Can anyone tell me how I can get the block by the actual identifier, or look up the blocks id by the identifier so that I can grab the block by block name?
Any help much appreciated.
Looking at the cms/block block source, those tutorials have mislead you, or you misinterpreted them.
#File: app/code/core/Mage/Cms/Block/Block.php
class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
if (!$this->_beforeToHtml()) {
return '';
}
$html = '';
if ($blockId = $this->getBlockId()) {
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load($blockId);
if (!$block->getIsActive()) {
$html = '';
} else {
$content = $block->getContent();
$processor = Mage::getModel('core/email_template_filter');
$html = $processor->filter($content);
}
}
return $html;
}
}
The block content is always loaded with ->load($blockId); -- load with one parameter always means loding by a database ID.
So, with no supported way of doing this built into the block, you'll need to look up the block ID.
$model = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier','footer_links')
->getFirstItem();
var_dump($model->getBlockId());
In the admin, when you are editing the contents of the static block, you will see a field called Identifier second from the top. Copy the value of that field, and insert it into your code. So if your Block is called contact-info in the admin, then your code will look like:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('contact-info')->toHtml() ?>
The value in that Identifier textbox in the admin is also what will be saved into the cms_block table, as you're worked out.
HTH,
JD
Related
Hi i have just start learning magento module development and for this i am following the pierefay tutorial and i found this very useful. However i am following his tutorial step by step in the fourth step
http://www.pierrefay.com/magento-model-database-tutorial-54#comment-1507
i got stuck at one place
in the block file i have following code and it is suggested it will return entries from database
<?php
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
//on initialize la variable
$retour='';
/* we are doing the query to select all elements of the pfay_test table (thanks to our model test/test and we sort them by id_pfay_test */
$collection = Mage::getModel('test/test')->getCollection()->setOrder('id_pfay_test','asc');
/* then, we check the result of the query and with the function getData() */
foreach($collection as $data)
{
echo $retour .= $data->getData('nom').' '.$data->getData('prenom')
.' '.$data->getData('telephone').'<br />';
}
//i return a success message to the user thanks to the Session.
Mage::getSingleton('core/session')->addSuccess($retour);
print_r($retour);
}
}
but it is not returning anything.
When i copy this code in my indexcontroller or phtml file it shows the list of entries from database.
Can anybody tell me how can i use this code in block file to return data
thanks
ok i got the solution by calling the
<?php
echo $this->methodblock();
?>
in phtml file
I am trying to display random testimonials, but due to magento cache the random is not working, i have to flush the cache each time to see the testimonials change, my code
public function getTestimonialsLast(){
$collection = Mage::getModel('testimonial/testimonial')->getCollection();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->addFieldToFilter('status',1);
$collection->setPageSize(5);
return $collection;
}
how can i make it work , how can i make it so that whenever the page is refreshed the collection is randomized.
Any help is greatly appreciated.
Thank you in advance,
One possibility is in the view file:
You can stop Magento from caching the block by adding a false parameter when you implement the block.
<?php echo $this->getChildHtml('testimonials', false) ?>
Because of
Method Summary
string getChildHtml ([string $name = ‘’], [boolean $useCache = true], [ $sorted = true])
Or you could add the cache lifetime to your testimonial class:
public function getCacheLifetime() { return null; }
Are you caching the block within the modules public function __construct()
It would have information on 'cache_lifetime'
Removing the cache block would prevent it from being cached and perform a fresh call each time.
isn't this the right method to get Name of logged in customer?
<?php echo Mage::helper('customer')->getCustomer()->getName(); ?>
I have a website with live chat functionality. Yesterday I have been asked to pass email address and the name of the logged into the user into the Javascript Tracking variable code placed in the head section of the website. So that the operators could see who is on the website and whom are they talking to without any need to ask about their information.
So I passed the information from Magento into the Javascript code but now I see this very strange thing happening. For example,
If I am logged in with credentials Name = John Email =
john12#yahoo.com
Then This name and email variable values are changing with the change of pages. For example if I click on any product page the variable values which I am passing changes to some other user's information.
Name becomes Ricky Email becomes ricky23#gmail.com
this variable values are kept on changing back to john and from john to something else with the change of pages. So operator does not have any idea whom are they talking because the values are kept on changing. Also, user ricky or who ever it changes to also exist in the database. so it is picking up random person from the database.
This is what i did to pass the code to javascript. Please let me know if that is not the right code to pass the information. Please check the php code I am using to fetch information from Magento. Roughly, I receive incorrect value once in 5 times. Please provide some assistance. Thanks in advance.
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$firstname = $customer->getFirstname();
$lastname= $customer->getLastname();
$name = $firstname . ' ' . $lastname;
?>
<script type="text/javascript">
if (typeof(lpMTagConfig) == "undefined"){ lpMTagConfig = {};}
if (typeof(lpMTagConfig.visitorVar) == "undefined"){ lpMTagConfig.visitorVar = [];}
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Email=<?php echo $email; ?>';
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Name=<?php echo $name; ?>';
</script>
I'm also attaching a snap shot
I'd be interested to hear how you're adding this code to the page? Is it in it's own block, or are you adding it to footer.phtml, or similar? If your adding to an existing block be sure to check the block caching settings of that template.
To confirm the caching hypothesis I'd ask the following:
Do you get the same name, all the time, on the same page? When you refresh the page, do you get the same name and email in the Javascript?
Does the problem persist with caching disabled?
This doesn't sound like a singleton problem at all. Each execution of the PHP script is isolated from the others, serving one page request. There's no chance of another customer's object moving between invokations of the script.
It is a matter of understanding the singleton pattern. If you call your code twice:
$customer_1 = Mage::helper('customer')->getCustomer()->getName();
$customer_2 = Mage::helper('customer')->getCustomer()->getName();
you get two different instances of the object. But... if one of them has already implemented a singleton pattern in its constructor or has implemented a singleton getInstance then both objects will actually point to the same thing.
Looking at the customer/helper/Data.php code you can see the function
public function getCustomer()
{
if (empty($this->_customer)) {
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
return $this->_customer;
}
That means that in one of the cases singleton is already implemented/called and in other one - not as the property is already set.
The correct way to work with quote/customer/cart in order to get always the correct data is always to use the singleton pattern.
So using this:
$customer = Mage::getSingleton('customer/session')->getCustomer();
always guarantee that you get the correct customer in that session. And as may be you know singleton pattern is based on registry pattern in app/Mage.php:
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}
and looking at app/Mage.php:
public static function register($key, $value, $graceful = false)
{
if (isset(self::$_registry[$key])) {
if ($graceful) {
return;
}
self::throwException('Mage registry key "'.$key.'" already exists');
}
self::$_registry[$key] = $value;
}
...
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
you can see that Magento checks is it is already set. If so, Magento will either throw an Exception, which is the default behavior or return null.
Hope this will help you to understand the issue you face.
I have sorted this out. I have moved the code from footer.phtml to head.phtml and it's working fine now.Values are not changing anymore. If anyone know the logic behind please post and I will change my answer. So far this is working.
I have a customer product page that literally lives beside the catalog/product/view.phtml page. It's basically identical to that page with a few small exceptions. It's basically a 'product of the day' type page so I can't combine it with the regular product page since I have to fetch the data from the DB and perform a load to get the product information
$_product = Mage::getModel('catalog/product')->load($row['productid']);
To make a long story short, everything works (including all children html blocks) with the singular exception of the related products.
After the load I save the product into the registry with
Mage::register('product', $_product);
and then attempt to load the related products with:
echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/list/related.phtml')->toHtml();`
All of which give back the error:
Fatal error: Call to a member function getSize() on a non-object in catalog/product/list/related.phtml on line 29`,
and line 29 is
<?php if($this->getItems()->getSize()): ?>`.
Any help getting the relateds to load would be appreicated.
I didn't quite follow what you're trying to do, but I know why you're getting your errors. You're creating a block whose class-alias/class is
catalog/product_view
Mage_Catalog_Block_Product_View
but you're setting this block's template as
catalog/product/list/related.phtml
The stock catalog/product/list/related.phtml template was built to be used with a catalog/product_list_related Block only, and not a catalog/product_view Block.
If you take a look at the class definition for a catalog/product_list_related Block (which is a Mage_Catalog_Block_Product_List_Related), you can see that there's a getItems() method.
public function getItems()
{
return $this->_itemCollection;
}
which returns a collection. The collection is set in the _prepareData method
protected function _prepareData()
{
$product = Mage::registry('product');
/* #var $product Mage_Catalog_Model_Product */
$this->_itemCollection = $product->getRelatedProductCollection()
...
This collection is never set with a catalog/product_view Block, which is why you're getting your errors.
In your code above, if you switch to creating a catalog/product_list_related block, your errors should go away.
public function relatedproductsAction(){
$this->loadLayout();
$relatedBlock = "";
$rec_prod_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$_product = Mage::getModel('catalog/product')->load($rec_prod_id);
Mage::register('product', $_product);
$relatedBlock = $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/related.phtml')->toHtml();
echo $relatedBlock;
exit;
}
Getting html of related block through ajax call, right after when product is added to cart. might be relatively helpful.
I wonder how to check that a particular CMS block is active or not.
So far I have found that CMS block are Mage_Cms_Block_Block class that inherits from Mage_Cms_Block_Abstract class
Mage::log(get_class(Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('promo_space')
Neither of the two classes have methods that would check wether the block is active or not. How do I do it?
Mage::getModel('cms/block')->load('static_block_identifier')->getIsActive()
Replace static_block_identifier with the identifier you assigned to your CMS static block.
Got this myself
I created a method isActive(Identifiere, Value) in helper "Block" in the Mage/Cms local Module.
This is how the method looks
public function isActive($attribute, $value){
$col = Mage::getModel('cms/block')->getCollection();
$col->addFieldToFilter($attribute, $value);
$item = $col->getFirstItem();
$id = $item->getData('is_active');
if($id == 1){
return true;
}else{
return false;
}
}
parameter $attribute is table(cms-block) field such as 'identifier' or 'title' and value can be the name of the static block or identifier itself. Both used to filter down the particular static block you are interested in
Here is how i call the helper
if(Mage::helper('cms/block')->isActive('identifier','promo_space')){
//do that
}
I have also updated the config.xml file for Cms block to read my new helper and the method.
I hope its useful.
This code works for me:
if ( $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR-BLOCK-ID')->toHtml() !== '' ) {}
Maybe this is old, but I use another method that works not only for cms blocks but for any other block loaded on the layout. If you need to check if a block has been loaded:
if($this->getLayout()->getBlock('your_block_name'))
//Do whatever you need here
It's pretty easy!
A better way to do this is to add observer to this event: controller_action_layout_generate_blocks_after which happens right after Magento has initialized and generated Block objects. You have access to the layout and action classes and to all generated blocks before HTML is rendered
//You can check if the block exists in the layout
$layout = $observer->getEvent()->getObserver();
$cmsBlock = $layout->getBlock($identifier);//Returns false if doesn't exist.
//You can check it in the database too:
$cmsModel = Mage::getModel('cms/page')->load($identifier);
if($cmsModel->getId() AND $cmsModel->getIsActive() == 1)
{
//CMS block is active
}