0

I was trying to filter CMS pages for my multi-language website.

I was filtering CMS pages, because some of them are only for one store.

So i was trying to filter like this:

$name_page = Mage::app()->getFrontController()->getRequest()->getRouteName();
if ($name_page == 'cms') {
    $cms_page = Mage::getSingleton('cms/page')->getIdentifier();
    $active = Mage::getModel('cms/block')->load($cms_page)->getIsActive();
    if($active != 1){
        return null;
    }
}

As far as i know, $active returns 1 or 0 for positive or negative. Instead of these, my examples continue going to the stores with the cms in other languages.

Edit :

As first answer say:

  $name_page = Mage::app()->getFrontController()->getRequest()->getRouteName();
  if ($name_page == 'cms') {
        $cms_page = Mage::getSingleton('cms/page')->getIdentifier();
         $active = Mage::getModel('cms/block')->load($cms_page, 'identifier')->getIsActive();
        if(!$active){
            return null; 
        }
    }

I try this code, but my method is still returning the url.


My languages code:

<?php $_stores = $this->getAllStores(); ?>
<?php if (count($_stores) > 1): ?>
    <span class="div-list-lang">
    <?php echo ( $this->__('Switch language')); ?>
        <ul class="list-lang-ul">
        <?php foreach ($_stores as $_store): ?>
        <?php

        $_store->setTranslatedName(Mage::helper('catalog')->__($this->htmlEscape($_store->getName())));
        $country = strtolower(substr($this->htmlEscape($_store->getLocaleCode()), 3));

        $url = Mage::helper('youngdogs_alternate')->rewriteUrlForStore($_store->getId());
        if (!$url) {
            $url = $_store->getBaseUrl();
        }


        $identifier = Mage::getSingleton('cms/page')->getIdentifier();
        $pageId = Mage::getModel('cms/page')->checkIdentifier($identifier, $_store->getId());
        if($pageId){
            $url = $_store->getBaseUrl();
        }

        ?>

        <li>
            <a href="<?php echo $url ?>">
    <?php $store_id = $_store->getId();
    $logo_src = Mage::getStoreConfig('design/header/logo_src_small', $store_id)
    ?>
            <img class="mini-icon" src="<?php echo Mage::getBaseUrl('skin') ?>frontend/default/venauto/<?php echo $logo_src ?>" />
            <img class="mini-lang" src="<?php echo $this->getSkinUrl("images/flags/flag_$country.png") ?>" />
            <span class="span-lang" title="<?php echo $_store->getTranslatedName() ?>"><?php echo $_store->getTranslatedName() ?></span>
            </a>

        </li>
    <?php endforeach; ?>
        </ul>
    </span>
<?php endif; ?>

3 Answers 3

2

I don't really understand your question, but in case I got it right and your want to check if a page is available in a store and you have the page identifier you can do this:

$storeId = Mage::app()->getStore()->getId();
$pageId = Mage::getModel('cms/page')->checkIdentifier($identifier, $storeId);
if ($pageId) {
    //page is available
} else {
    //page does not exist or is not available
}

[EDIT]
If you want to get all the cms pages available for a certain store view, try this:

$collection = Mage::getModel('cms/page')->getCollection()
     ->addFieldToFilter('is_active', 1)
     ->addStoreFilter($storeId)

foreach ($collection as $page) {
    //do something with $page
}
6
  • I check your code, but dont works for me. I will explain better: I've made a language selector, but a lot of CMS of main page are not on other store views. This CMS pages are just selected on the main page, but not for the others stores views. I just want to filter in the selector menu language, that, when i open it to swap the language, if i am on CMS page that its only for the main page, the URL of the link have to be the BaseUrl for the other store views. Understood?
    – ntzz
    Aug 28, 2015 at 11:28
  • Seams a bit clearer now. The code I wrote should still work. Just replace $storeId = Mage::app()->getStore()->getId(); with what store id you need to check.
    – Marius
    Aug 28, 2015 at 11:42
  • yeah, i have done when you says. But the returned URL its the same still... I check again but i'm sure i do it perfectly :(
    – ntzz
    Aug 28, 2015 at 11:44
  • Checked again, it makes that filter the URL for CMS, but it makes for other categories too, so, i have to implement the part of my code to check if its a CMS page ? -> LIKE : $name_page = Mage::app()->getFrontController()->getRequest()->getRouteName(); if ($name_page == 'cms') { } ??? Or any other recomendation??
    – ntzz
    Aug 28, 2015 at 11:48
  • @ntzz. See my update to the answer. Maybe it helps.
    – Marius
    Aug 28, 2015 at 12:09
1

try this to load cms page by identifier

 $active = Mage::getModel('cms/page')->load($cms_page, 'identifier')->getIsActive();
10
  • and $active = 1 its the true return?
    – ntzz
    Aug 28, 2015 at 10:28
  • yes its true return... Aug 28, 2015 at 10:33
  • So, i edit the post, because still there the problem.
    – ntzz
    Aug 28, 2015 at 10:36
  • use this code for check .. if(!$active){ return null; } Aug 28, 2015 at 10:39
  • continue the error. Where could i miss anything? I check if the cms is perfectly on the store not?
    – ntzz
    Aug 28, 2015 at 10:42
0
<?php
$name_page = Mage::app()->getFrontController()->getRequest()->getRouteName();
if ($name_page == 'cms') {
    $_store = Mage::app()->getStore();
    $cms_page = Mage::getSingleton('cms/page')->getIdentifier();
    $active = Mage::getModel('cms/page')->setStoreId($_store->getId())->load($cms_page, 'identifier')->getIsActive();
    if($active != 1){
        return null;
    }
}

I changed a few points:

  • in the line $active = ..., you loaded the cms/block model. Instead, you need to use cms/page
  • you need to load the cms page for a specific store, because it is very well possible, that the cms page is active for another store that you're looking at

You could also check, after you make sure that the right cms page is loaded in a positive scenario, if you need to do a getIsActive()-check at all. I think, when you set the store before loading, the cms/page instance returned will not even have an ID because it does not exist in the specified store.

3
  • still with the problem. Could be other problem of my code ? Are you sure this code should works?
    – ntzz
    Aug 28, 2015 at 10:53
  • Hm, can you tell me the exact problem? For example, if you just call Mage::getModel('cms/page')->setStoreId($_store->getId())->load($cms_page, 'identifier'), does it return the correct cms page object? Aug 28, 2015 at 10:54
  • Edit: If i use this code, it returns the exactly URL of the page.
    – ntzz
    Aug 28, 2015 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.