Filtering Magento CMS Pages for Store-Specific Content
In Magento, managing store-specific content is crucial for maintaining data integrity and security. One common requirement is to retrieve only the CMS pages that are unique to a specific store - i.e., those not assigned to any other stores.
Understanding the Current Approach
The current approach uses Mage::getModel('cms/page')->getCollection()->addStoreFilter($store_id). This method retrieves all CMS pages associated with the specified store. However, it includes pages that are also assigned to other stores, including the ‘All Store Views’ option.
The Challenge: Removing Cross-Assigned Pages
To achieve your goal of retrieving only unique, single-store CMS pages, you need to exclude those that have additional store assignments beyond the current one.
A Step-By-Step Solution
Let’s break down a solution using Magento’s collection filtering capabilities:
1. Retrieve All Relevant CMS Pages
First, we retrieve all CMS pages associated with the specified store ID. We include the stores join to access the cms_page_store table.
$collection = Mage::getModel('cms/page')
->getCollection()
->joinStoreTable();
2. Add Store Filter
Next, we add the initial filter for the specified store ID. This ensures all pages in our collection are associated with this store.
$collection->addStoreFilter($store_id);
3. Exclude Cross-Assigned Pages
Now, we need to exclude any page that has additional store assignments beyond our current store. We use ->where to add a condition:
$collection->getSelect()
->where('main_table.page_id NOT IN (
SELECT DISTINCT main_table.page_id FROM cms_page_store WHERE store_id != ?)', $store_id);
This subquery selects all pages that are assigned to stores other than the current one. The NOT IN clause excludes these from our collection.
4. Apply Final Filter
Finally, we ensure only single-store pages remain in the collection by filtering out any page with more than one store assignment:
$collection->getSelect()
->where('COUNT(main_table.page_id) = 1', null);
This COUNT function ensures only pages assigned to exactly one store (including our current store) are included.
Extending for Permissions Module
For your specific use case of extending the Aitoc permissions module, you can integrate this filter logic into your grid and form:
- Apply the collection filtering in your
Mage_Adminhtml_Cms_PageControllerbefore populating your grid. - Use similar logic to restrict form access only to pages that pass these filters.
Conclusion
Filtering Magento CMS pages for store-specific content requires a strategic approach to exclude cross-assigned items. By using standard Magento collection filtering and careful SQL construction, you can achieve this functionality effectively.
Remember to test your solution thoroughly across different scenarios, including edge cases where a page might be assigned only to the ‘All Store Views’ option.
For more advanced Magento store management strategies, consider referring to our comprehensive guides at Laravel Company.