PHP code example of sifuen / module-upgradable-content
1. Go to this page and download the library: Download sifuen/module-upgradable-content library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sifuen / module-upgradable-content example snippets
use Sifuen\UpgradableContent\Model\ContentUpgrader;
class UpgradeData implements UpgradeDataInterface
{
/**
* @var ContentUpgrader
*/
private $contentUpgrader;
/**
* UpgradeData constructor.
* @param ContentUpgrader $contentUpgrader
*/
public function __construct(
ContentUpgrader $contentUpgrader
)
{
$this->contentUpgrader = $contentUpgrader;
// Set the current module we are in so the content upgrader
// can find our content files
$this->contentUpgrader->setContentModule('Sifuen_CmsTest');
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
/**
* In this instance, we are creating a new CMS page with the identifier
* 'example-page-test'. The array of page data is added to the Page model
* using addData()
*/
$this->contentUpgrader->upgradePages('1.0.1', [
'examplepage-test' => [
'title' => 'Example Page',
'page_layout' => '1column',
'content_heading' => 'Example Page'
]
]);
}
if (version_compare($context->getVersion(), '1.0.2', '<')) {
/**
* Now we're upgrading an existing page named 'examplepage-test'. Only
* the content will be updated to what the HTML file contains.
*/
$this->contentUpgrader->upgradePages('1.0.2', ['examplepage-test']);
}
if (version_compare($context->getVersion(), '1.0.3', '<')) {
/**
* This will create a new CMS block named 'exampleblock-test' with the title
* 'Example Block Title'.
*/
$this->contentUpgrader->upgradeBlocks('1.0.3', [
'exampleblock-test' => [
'title' => 'Example Block Title'
]
]);
}
if (version_compare($context->getVersion(), '1.0.4', '<')) {
/**
* This will update the CMS block 'exampleblock-test' with the newest content from the HTML file
*/
$this->contentUpgrader->upgradeBlocks('1.0.4', ['exampleblock-test']);
}
}
/**
* UpgradeData constructor.
* @param ContentUpgrader $contentUpgrader
*/
public function __construct(
ContentUpgrader $contentUpgrader
)
{
$this->contentUpgrader = $contentUpgrader;
// The full path where you can find your CMS content files
$this->contentUpgrader->setContentDirectory('/var/somewhere/else/completely');
// The module name where to find your CMS content files
$this->contentUpgrader->setContentModule('Sifuen_CmsTest');
// Where in the module it can find your CMS content files
$this->contentUpgrader->setModuleContentFolder('Setup/content');
// The file extension your CMS content files will have
$this->contentUpgrader->setContentFileExtension('.html');
}