Magento 如何检查CMS块是否处于活动状态?
我想知道如何检查特定CMS块是否处于活动状态 到目前为止,我发现CMS块是从Magento 如何检查CMS块是否处于活动状态?,magento,Magento,我想知道如何检查特定CMS块是否处于活动状态 到目前为止,我发现CMS块是从Mage\u CMS\u block\u Abstract类继承的Mage\u CMS\u block\u block\u类 Mage::log(get_类(Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('promo_space')) 这两个类都没有检查块是否处于活动状态的方法。我该怎么做?我自己得到了这个 我在Mage/Cms本地模块的he
Mage\u CMS\u block\u Abstract
类继承的Mage\u CMS\u block\u block\u类
Mage::log(get_类(Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('promo_space'))
这两个类都没有检查块是否处于活动状态的方法。我该怎么做?我自己得到了这个
我在Mage/Cms本地模块的helper“Block”中创建了一个方法isActive(Identifiere,Value)
这就是该方法的外观
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;
}
}
参数$attribute是表格(cms块)字段,如“标识符”或“标题”,值可以是静态块的名称或标识符本身。两者都用于筛选您感兴趣的特定静态块
下面是我如何称呼助手的
if(Mage::helper('cms/block')->isActive('identifier','promo_space')){
//do that
}
我还更新了Cms块的config.xml文件,以读取我的新助手和方法
我希望它有用。可能这是旧的,但我使用的另一种方法不仅适用于cms块,而且适用于版面上加载的任何其他块。如果您需要检查是否已加载块:
if($this->getLayout()->getBlock('your_block_name'))
//Do whatever you need here
这很简单!更好的方法是将观察者添加到此事件:控制器\u动作\u布局\u生成块\u之后,Magento初始化并生成块对象。在呈现HTML之前,您可以访问布局和动作类以及所有生成的块
//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
}
Mage::getModel('cms/block')->load('static\u block\u identifier')->getIsActive()
用分配给CMS静态块的标识符替换静态块标识符。此代码适用于我:
if ( $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR-BLOCK-ID')->toHtml() !== '' ) {}