<?php declare(strict_types=1);
namespace LoyxxFullWidthProductListing;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class LoyxxFullWidthProductListing extends Plugin
{
/**
* Bundle Name
* @var string
*/
public const BUNDLE_NAME = 'LoyxxFullWidthProductListing';
/**
* Author Name
* @var string
*/
public const AUTHOR = 'Loy GmbH';
/**
* CustomFieldSet Name
* @var string
*/
public const PRODUCT_CUSTOM_FIELD_GROUP = 'loyxx_product_list';
/**
* Product Benefits custom field
* @var string
*/
public const PRODUCT_CUSTOM_FIELD = 'loyxx_product_list_benefits';
/**
*
*/
public const PRODUCT_CUSTOM_FIELD_BENEFITS_LIMIT = 5;
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->setupCustomField($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->setupCustomField($updateContext->getContext());
}
private function setupCustomField(Context $context)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
//check if the custom field set is defined
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
$criteria->addAssociation('customFields');
/** @var CustomFieldSetEntity $customFieldSetEntity */
$customFieldSetEntity = $customFieldSetRepository->search($criteria, $context)->first();
$customFields = [];
$customFieldSet = [
'name' => static::PRODUCT_CUSTOM_FIELD_GROUP,
];
for ($i = 1; $i <= static::PRODUCT_CUSTOM_FIELD_BENEFITS_LIMIT; $i++) {
$customFields[] = [
'name' => static::PRODUCT_CUSTOM_FIELD . '_' . $i,
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'en-GB' => 'Product Benefit ' . $i,
'de-DE' => 'Produktvorteile ' . $i
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => $i,
'translated' => true
]
];
}
if ($customFieldSetEntity === null) {
$customFieldSet = array_merge($customFieldSet, [
'customFields' => $customFields,
'relations' => [
['entityName' => 'product']
],
'config' => [
'description' => [
'en-GB' => 'Product Benefits',
'de-DE' => 'Produktvorteile'
],
'label' => [
'en-GB' => 'Product Benefits',
'de-DE' => 'Produktvorteile'
],
'translated' => true
]
]);
} else {
/** @var CustomFieldEntity $item */
foreach ($customFieldSetEntity->getCustomFields() as $item) {
$key = array_search($item->getName(), array_column($customFields, 'name'));
if ($key !== FALSE) {
$customFields[$key]['id'] = $item->getId();
}
}
$customFieldSet = array_merge($customFieldSet, [
'id' => $customFieldSetEntity->getId(),
'customFields' => $customFields
]);
}
$customFieldSetRepository->upsert(
[$customFieldSet],
$context
);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeSeminarCustomField($uninstallContext);
}
private function removeSeminarCustomField(UninstallContext $uninstallContext)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$context = $uninstallContext->getContext();
//check if the custom field set is defined
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
$customFieldSetEntity = $customFieldSetRepository->searchIds($criteria, $context);
if ($customFieldSetEntity->getTotal()) {
$customFieldSetRepository->delete(array_values($customFieldSetEntity->getData()), $context);
}
}
}