Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/markshust/magento2-module-simpledata
The SimpleData module simplifies calling Magento data structures.
https://github.com/markshust/magento2-module-simpledata
magento magento2 magento2-module
Last synced: 2 days ago
JSON representation
The SimpleData module simplifies calling Magento data structures.
- Host: GitHub
- URL: https://github.com/markshust/magento2-module-simpledata
- Owner: markshust
- License: mit
- Created: 2020-05-20T16:41:56.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-22T15:24:47.000Z (about 1 year ago)
- Last Synced: 2024-10-03T06:07:57.368Z (about 1 month ago)
- Topics: magento, magento2, magento2-module
- Language: PHP
- Homepage:
- Size: 1.74 MB
- Stars: 94
- Watchers: 6
- Forks: 7
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
MarkShust_SimpleData
## Table of contents
- [Summary](#summary)
- [Installation](#installation)
- [Usage](#usage)
- [License](#license)## Summary
Calling Magento data structures can be confusing. There are many classes available, and knowing which to call and when can be confusing & overwhelming.
This module aims to simplify calling these data structures. All classes are prefixed with `Simple` so they are easy to lookup within IDEs. They also follow a pretty standard naming convention which aligns with Magento's way of naming modules. It also provides a `SimpleDataPatch` class which greatly simplifies writing data patch scripts.
For example, here is a data patch script to update the content of a CMS page with and without `SimpleData`:
![Demo](https://raw.githubusercontent.com/markshust/magento2-module-simpledata/master/docs/demo.png)
## Installation
```sh
composer require markshust/magento2-module-simpledata
bin/magento module:enable MarkShust_SimpleData
bin/magento setup:upgrade
```## Usage
Here are the signatures of the simplified data structures classes:
`MarkShust\SimpleData\Api\Data\Cms\SimpleBlock`
```php
/**
* 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
````MarkShust\SimpleData\Api\Data\Cms\SimplePage`
```php
/**
* 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
```### Examples using SimpleDataPatch
#### Create block
```php
block->save([
'identifier' => 'foo_bar',
'title' => 'Foo bar',
'content' => <<
Foo barCONTENT,
]);
}
}
```#### Delete block
```php
block->delete('foo_bar');
}
}
```#### Update block
```php
block->save([
'identifier' => 'foo_bar',
'title' => 'Foo bar 1',
]);
}
}
```#### Create page
```php
page->save([
'identifier' => 'foo_bar',
'title' => 'Foo bar',
'content' => <<
Foo barCONTENT,
]);
}
}
```#### Update page
```php
page->save([
'identifier' => 'foo_bar',
'title' => 'Foo bar 1',
]);
}
}
```#### Delete page
```php
page->delete('foo_bar');
}
}
```#### Create or update config
```php
config->save('foo/bar', 'baz');
}
}```
#### Delete config
```php
config->delete('foo/bar');
}
}
```### Example using dependency injection
```php
block = $simpleBlock;
}/**
* {@inheritdoc}
*/
public function execute(): void
{
$this->block->save([
'identifier' => 'foo_bar',
'title' => 'Foo bar',
'content' => <<
Foo barCONTENT,
]);// Carry out other actions...
}
}
```## License
[MIT](https://opensource.org/licenses/MIT)