PHP code example of markshust / magento2-module-simpledata

1. Go to this page and download the library: Download markshust/magento2-module-simpledata 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/ */

    

markshust / magento2-module-simpledata example snippets


/**
 * Delete a block from a given identifier and optional store id.
 * @param string $identifier
 * @param int $storeId
 */
public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void

/**
 * If the CMS block identifier is found, attempt to update the record.
 * If it is not found, attempt to create a new record.
 * @param array $data
 */
public function save(array $data): void

/**
 * Delete a page from a given identifier and optional store id.
 * @param string $identifier
 * @param int $storeId
 */
public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void

/**
 * If the CMS page identifier is found, attempt to update the record.
 * If it is not found, attempt to create a new record.
 * @param array $data
 */
public function save(array $data): void


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class BlockFooBarCreate extends SimpleDataPatch
{
    public function apply(): self
    {
        $this->block->save([
            'identifier' => 'foo_bar',
            'title' => 'Foo bar',
            'content' => <<<CONTENT
<div class="foo-bar">
    Foo bar
</div>
CONTENT,
        ]);
    }
}


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class BlockFooBarDelete extends SimpleDataPatch
{
    public function apply()
    {
        $this->block->delete('foo_bar');
    }
}


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class BlockFooBarUpdate extends SimpleDataPatch
{
    public function apply()
    {
        $this->block->save([
            'identifier' => 'foo_bar',
            'title' => 'Foo bar 1',
        ]);
    }
}


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class PageFooBarCreate extends SimpleDataPatch
{
    public function apply()
    {
        $this->page->save([
            'identifier' => 'foo_bar',
            'title' => 'Foo bar',
            'content' => <<<CONTENT
<div class="foo-bar">
    Foo bar
</div>
CONTENT,
        ]);
    }
}


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class MyDataPatch extends SimpleDataPatch
{
    public function apply()
    {
        $this->page->save([
            'identifier' => 'foo_bar',
            'title' => 'Foo bar 1',
        ]);
    }
}


declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class PageFooBarDelete extends SimpleDataPatch
{
    public function apply()
    {
        $this->page->delete('foo_bar');
    }
}



namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class ConfigFooBarCreate extends SimpleDataPatch
{
    public function apply()
    {
        $this->config->save('foo/bar', 'baz');
    }
}




namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class ConfigFooBarDelete extends SimpleDataPatch
{
    public function apply()
    {
        $this->config->delete('foo/bar');
    }
}


declare(strict_types = 1);

namespace MarkShust\SimpleData;

use MarkShust\SimpleData\Api\Cms\SimpleBlock;

class MyClass
{
    /** @var SimpleBlock */
    protected $block;

    /**
     * SimpleDataPatch constructor.
     * @param SimpleBlock $simpleBlock
     */
    public function __construct(
        SimpleBlock $simpleBlock
    ) {
        $this->block = $simpleBlock;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(): void
    {
        $this->block->save([
            'identifier' => 'foo_bar',
            'title' => 'Foo bar',
            'content' => <<<CONTENT
<div class="foo-bar">
    Foo bar
</div>
CONTENT,
        ]);

        // Carry out other actions...
    }
}