Skip to content

Commit

Permalink
ADD: module
Browse files Browse the repository at this point in the history
  • Loading branch information
allrude committed Feb 17, 2024
1 parent 9c38ff5 commit 3ac78fd
Show file tree
Hide file tree
Showing 31 changed files with 673 additions and 220 deletions.
10 changes: 3 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{md,mdx}]
[*.md]
indent_size = 4
trim_trailing_whitespace = false
max_line_length = 80

[*.{css,less,sass,scss,js,ts,mjs}]
[*.{css,js,cjs}]
indent_size = 4
max_line_length = 80

[*.{html,vue,jsx,tsx}]
indent_size = 4
max_line_length = 80

[*.{xml,phtml,php}]
[*.{xml,html,phtml,php}]
indent_size = 4
max_line_length = 120
25 changes: 25 additions & 0 deletions Block/Adminhtml/Form/Field/Usps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/**
* @author Siteation (https://siteation.dev/)
* @copyright Copyright 2021 Siteation (https://siteation.dev/)
* @license MIT
*/

namespace Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

class Usps extends AbstractFieldArray
{
protected function _prepareToRender()
{
$this->addColumn('content', [
'label' => __('Content'),
'class' => 'required-entry'
]);

$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
}
8 changes: 3 additions & 5 deletions Observer/RegisterModuleForHyvaConfig.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php declare(strict_types=1);

namespace Siteation\StoreUsps\Observer;
namespace Siteation\StoreInfoUsps\Observer;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class RegisterModuleForHyvaConfig implements ObserverInterface
{
private $componentRegistrar;
private ComponentRegistrar $componentRegistrar;

public function __construct(ComponentRegistrar $componentRegistrar)
{
Expand All @@ -20,9 +20,7 @@ public function execute(Observer $event)
$config = $event->getData('config');
$extensions = $config->hasData('extensions') ? $config->getData('extensions') : [];

$moduleName = implode('_', array_slice(explode('\\', __CLASS__), 0, 2));

$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, 'Siteation_StoreInfoUsps');

// Only use the path relative to the Magento base dir
$extensions[] = ['src' => substr($path, strlen(BP) + 1)];
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Siteation - Magento 2 Store Usps

[![Packagist Version](https://img.shields.io/packagist/v/siteation/magento2-storeusps?style=for-the-badge)](https://packagist.org/packages/siteation/magento2-storeusps)
[![Packagist Version](https://img.shields.io/packagist/v/siteation/magento2-storeinfo-usps?style=for-the-badge)](https://packagist.org/packages/siteation/magento2-storeinfo-usps)
![Supported Magento Versions](https://img.shields.io/badge/magento-%202.3_|_2.4-brightgreen.svg?logo=magento&longCache=true&style=for-the-badge)
[![Hyvä Themes Supported](https://img.shields.io/badge/Hyva_Themes-Supported-3df0af.svg?longCache=true&style=for-the-badge)](https://hyva.io/)
![License](https://img.shields.io/github/license/Siteation/magento2-storeusps?color=%23234&style=for-the-badge)
![License](https://img.shields.io/github/license/Siteation/magento2-storeinfo-usps?color=%23234&style=for-the-badge)

<!-- INTRO -->

Expand All @@ -12,7 +12,7 @@
Install the package via;

```bash
composer require siteation/magento2-storeusps
composer require siteation/magento2-storeinfo-usps
bin/magento setup:upgrade
```

Expand Down
88 changes: 88 additions & 0 deletions ViewModel/StoreInfoUsps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types=1);

namespace Siteation\StoreInfoUsps\ViewModel;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\ScopeInterface;

class StoreInfoUsps implements ArgumentInterface
{
protected $scopeConfig;

public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

private function objectToArray(object $data): array
{
$result = [];
foreach ($data as $key => $value) {
$result[$key] = (is_array($value) || is_object($value))
? $this->objectToArray($value)
: $value;
}
return $result;
}

private function flattenArray(array $array): array
{
$flatArray = array_map(function($item) {
if (!is_array($item) || !array_key_exists('content', $item)) {
throw new \InvalidArgumentException('Each item in the array should be an array with a "content" key.');
}
return $item['content'];
}, $array);

return array_values($flatArray);
}

public function getStoreUsps(string $attribute): array
{
$path = sprintf('siteation_storeinfo_usps/%s/usps', $attribute);
$value = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE);

if (is_string($value)) {
return (array) $this->objectToArray(json_decode($value));
}

return (array) $value;
}

public function getHeaderUsps(): array
{
$usps = $this->getStoreUsps('header');
return !empty($usps) ? $this->flattenArray($usps) : [];
}

public function getFooterUsps()
{
$usps = $this->getStoreUsps('footer');
return !empty($usps) ? $this->flattenArray($usps) : [];
}

public function getCategoryUsps(): array
{
$usps = $this->getStoreUsps('category');
return !empty($usps) ? $this->flattenArray($usps) : [];
}

public function getProductUsps(): array
{
$usps = $this->getStoreUsps('product');
return !empty($usps) ? $this->flattenArray($usps) : [];
}

public function getCustom1Usps(): array
{
$usps = $this->getStoreUsps('custom_1');
return !empty($usps) ? $this->flattenArray($usps) : [];
}

public function getCustom2Usps(): array
{
$usps = $this->getStoreUsps('custom_2');
return !empty($usps) ? $this->flattenArray($usps) : [];
}
}
50 changes: 0 additions & 50 deletions ViewModel/StoreUsps.php

This file was deleted.

Binary file removed assets/preview-social.png
Binary file not shown.
Binary file removed assets/preview.png
Binary file not shown.
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"type": "magento2-module",
"description": "Get your store usps with ease",
"readme": "README.md",
"keywords": ["magento2", "magento2-module", "magento2-hyva", "usps"],
"keywords": [
"magento2",
"magento2-module",
"magento2-hyva",
"usps"
],
"authors": [
{
"name": "Siteation",
Expand All @@ -20,9 +25,11 @@
"hyva-themes/magento2-theme-module": "^1.1"
},
"autoload": {
"files": ["registration.php"],
"files": [
"registration.php"
],
"psr-4": {
"Siteation\\StoreUsps\\": ""
"Siteation\\StoreInfoUsps\\": ""
}
}
}
75 changes: 60 additions & 15 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"
>
<system>
<section id="general">
<group id="store_usps" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Store Usps</label>
<field id="usp_1" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Usps 1</label>
<tab id="siteation" sortOrder="999" translate="label">
<label>Siteation</label>
</tab>

<section id="siteation_storeinfo_usps" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10"
translate="label">
<label>Store Usps</label>
<tab>siteation</tab>
<resource>Magento_Backend::store</resource>

<group id="header" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Header</label>
<field id="usps" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Usps</label>
<frontend_model>Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field\Usps</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
<field id="usp_2" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Usps 2</label>
</group>

<group id="footer" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Footer</label>
<field id="usps" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Usps</label>
<frontend_model>Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field\Usps</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
<field id="usp_3" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Usps 3</label>
</group>

<group id="product" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Product Page</label>
<field id="usps" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Usps</label>
<frontend_model>Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field\Usps</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
<field id="usp_4" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Usps 4</label>
</group>

<group id="custom_1" translate="label" sortOrder="40" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Custom 1</label>
<field id="usps" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Usps</label>
<frontend_model>Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field\Usps</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
<field id="usp_5" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Usps 5</label>
</group>

<group id="custom_2" translate="label" sortOrder="41" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Custom 2</label>
<field id="usps" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Usps</label>
<frontend_model>Siteation\StoreInfoUsps\Block\Adminhtml\Form\Field\Usps</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
</group>
</section>
Expand Down
5 changes: 4 additions & 1 deletion etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"
>
<event name="hyva_config_generate_before">
<observer name="Siteation_StoreUsps" instance="Siteation\StoreUsps\Observer\RegisterModuleForHyvaConfig"/>
<observer
name="Siteation_StoreInfoUsps_Register_Hyva_Config"
instance="Siteation\StoreInfoUsps\Observer\RegisterModuleForHyvaConfig"
/>
</event>
</config>
4 changes: 2 additions & 2 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Siteation_StoreUsps">
<module name="Siteation_StoreInfoUsps">
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Catalog"/>
<module name="Magento_Store"/>
<module name="Hyva_Theme"/>
</sequence>
</module>

</config>
2 changes: 1 addition & 1 deletion registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Siteation_StoreUsps',
'Siteation_StoreInfoUsps',
__DIR__
);
Loading

0 comments on commit 3ac78fd

Please sign in to comment.