PHP code example of rocketweb / module-content-update

1. Go to this page and download the library: Download rocketweb/module-content-update 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/ */

    

rocketweb / module-content-update example snippets



\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'ProjectNamespace_ContentUpdate',
    __DIR__
);

public function createNewCmsPage($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');
    $stores = [$storeId]; // Or $stores = = [$storeId, .. ]; to assign the page to more than one store

    $content = <<<EOD
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
EOD;

    $layoutUpdateXml = <<<EOD
<referenceContainer name="content"></referenceContainer>
EOD;

    $data = [
        'title'             => 'Lorem Ipsum',
        'page_layout'       => '1column',
        'meta_title'        => '',
        'meta_keywords'     => '',
        'meta_description'  => '',
        //'identifier'      => 'other-lorem-ipsum', // Only to update old identifier if we want to
        'content_heading'   => '',
        'content'           => $content,
        'is_active'         => Page::STATUS_ENABLED,
        'sort_order'        => 0,
        'layout_update_xml' => $layoutUpdateXml
    ];

    $helperSetup->createCmsPage('lorem-ipsum', $data, $stores);
}

public function updateExistingCmsPage($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');

    $data = [
        'title'             => 'Lorem Ipsum Changed Title',
        'identifier'        => 'other-lorem-ipsum', // This time we change identifier
        'layout_update_xml' => '',
    ];

    $helperSetup->updateCmsPage('lorem-ipsum', $data, $storeId);
}

public function deleteExistingCmsPage($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');

    $helperSetup->deleteCmsPage('other-lorem-ipsum', $storeId);
}

public function createNewCmsBlock($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');
    $stores = [$storeId]; // Or $stores = = [$storeId, .. ]; to assign the block to more than one store

    $content = <<<EOD
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
EOD;

    $data = [
        'title'        => 'Lorem Ipsum',
        'content'      => $content,
        'is_active'    => Block::STATUS_ENABLED
    ];

    $helperSetup->createCmsBlock('dolor-sit-amet', $data, $stores);
}

public function updateExistingCmsBlock($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');

    $data = [
        'is_active'  => Block::STATUS_DISABLED,
        'identifier' => 'dolor-sit-amet-other', // This time we change identifier
    ];

    $helperSetup->updateCmsBlock('dolor-sit-amet', $data, $storeId);
}

public function deleteExistingCmsBlock($helperSetup)
{
    $storeId = $helperSetup->getStoreId('admin');

    $helperSetup->deleteCmsBlock('dolor-sit-amet-other', $storeId);
}

public function createConfiguration($helperSetup)
{
    // Set value to default Scope, to admin store implicitly
    $helperSetup->saveConfigValue(
        'rw_lorem_ipsum/general/hello_world',
        'Hello!'
    );

    // Set value to Scope 'store', to store with id 1
    $helperSetup->saveConfigValue(
        'rw_lorem_ipsum/general/hello_usa',
        'Hi!',
        ScopeInterface::SCOPE_STORE,
        Store::DISTRO_STORE_ID
    );

    // Can use to get store id
    // $storeId = $helperSetup->getStoreId('my store code');

    $websiteId = $helperSetup->getWebsiteId('base');
    if ($websiteId) {
        // Set value to Scope 'website', to website with id $websiteId
        $helperSetup->saveConfigValue(
            'rw_lorem_ipsum/general/we_said_hello',
            '1',
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
    }
}

public function updateConfiguration($helperSetup)
{
    // Get value to Scope 'store', to store with id 1
    $usa = $helperSetup->getConfigValue(
        'rw_lorem_ipsum/general/hello_usa',
        ScopeInterface::SCOPE_STORE,
        $helperSetup->getStoreCode(Store::DISTRO_STORE_ID)
    );
    $usa .= " Lorem Ipsum";
    // Set value to Scope 'store', to store with id 1
    $helperSetup->saveConfigValue(
        'rw_lorem_ipsum/general/hello_usa',
        $usa,
        ScopeInterface::SCOPE_STORE,
        Store::DISTRO_STORE_ID
    );

    $websiteId = $helperSetup->getWebsiteId('base');
    if ($websiteId) {
        // Set value to Scope 'website', to website with id $websiteId
        /*$is = $helperSetup->isConfigSetFlag(
            'rw_lorem_ipsum/general/we_said_hello',
            ScopeInterface::SCOPE_WEBSITES,
            $helperSetup->getWebsiteCode($websiteId)
        );*/

        $helperSetup->saveConfigValue(
            'rw_lorem_ipsum/general/we_said_hello',
            '0',
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
    }
}

public function deleteConfiguration($helperSetup)
{
    // Delete value of implicitly default Scope, of implicitly admin store
    $helperSetup->deleteConfigValue('rw_lorem_ipsum/general/hello_world');

    // Delete value of Scope 'store', of store with id 1
    $helperSetup->deleteConfigValue(
        'rw_lorem_ipsum/general/hello_usa',
        ScopeInterface::SCOPE_STORE,
        Store::DISTRO_STORE_ID
    );

    $websiteId = $helperSetup->getWebsiteId('base');
    if ($websiteId) {
        // Delete value of Scope 'website', of website with id $websiteId
        $helperSetup->deleteConfigValue(
            'rw_lorem_ipsum/general/we_said_hello',
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
    }
}

public function createNewWidget($helperSetup)
{
    // This is an example function
    return;

    $content = <<<EOD
<div>Lorem Ipsum</div>
EOD;
    $data = [
        'title'        => 'Lorem Ipsum',
        'content'      => $content,
        'is_active'    => Block::STATUS_ENABLED
    ];
    $helperSetup->updateCmsBlock('lorem_ipsum', $data, $helperSetup->getStoreId('admin'));

    $params = [
        'title'         => 'A brand new widget',
        'sort_order'    => '100',
        'type_code'     => 'cms_static_block',
        'theme_path'    => 'frontend/RocketWeb/mytheme',
        'page_group'    => 'virtual_products',
        'group_data' => [
            'block'         => 'parent_block_name_here',
            'for'           => 'specific',
            'layout_handle' => 'catalog_product_view_type_virtual', // any handler
            'entities'      => 999999 // Some product id
        ],
        'widget' => [
            'block_id' => 'lorem_ipsum'
        ]
    ];

    $helperSetup->addWidget(
        [$helperSetup->getStoreId('admin')],
        $params
    );
}

if (version_compare($context->getVersion(), '1.0.9') < 0) {
            $this->someFunction($helperSetup);
        }