<?php declare(strict_types=1);
namespace IcebergStorefrontChildTheme;
use Shopware\Core\Framework\Plugin;
use Shopware\Storefront\Framework\ThemeInterface;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
class IcebergStorefrontChildTheme extends Plugin implements ThemeInterface
{
public function install(InstallContext $installContext): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($this->getCustomFieldSet(), $updateContext->getContext());
try {
}
catch (\Exception $ex) {
//
}
}
/**
* {@inheritDoc}
*
* @param UninstallContext $uninstallContext
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
$this->deleteCustomFieldSet($uninstallContext->getContext());
$this->deleteCustomFields($uninstallContext->getContext());
}
/**
*
* @param Context $context
*
* @throws InconsistentCriteriaIdsException
*/
protected function deleteCustomFieldSet($context): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', 'cc_product_downloads'),
new ContainsFilter('name', 'cc_product_ingredients')
);
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldSetId)) {
$customFieldSetRepository->delete(
[
[
'id' => $customFieldSetId
]
],
$context
);
}
}
/**
*
* @param Context $context
*
* @throws InconsistentCriteriaIdsException
*/
protected function deleteCustomFields($context): void
{
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldRepository = $this->container->get('custom_field.repository');
$criteria = new Criteria();
$criteria->addFilter(
new ContainsFilter('name', 'cc_product_download'),
new ContainsFilter('name', 'cc_product_ingredients')
);
$ids = $customFieldRepository->searchIds($criteria, $context)->getIds();
if (!empty($ids)) {
$data = [];
foreach ($ids as $id) {
$data[] = [
'id' => $id
];
}
$customFieldRepository->delete($data, $context);
}
}
/**
*
* @return array
*/
protected function getCustomFieldSet(): array
{
return [
[
'id' => Uuid::randomHex(),
'name' => 'cc_product_ingredients',
'config' => [
'label' => [
'de-DE' => 'Product Tab Ingredients',
'en-GB' => 'Product Tab Ingredients',
],
],
'customFields' => [
[
'name' => 'cc_product_ingredients_html',
'type' => 'html',
'config' => [
'customFieldPosition' => 1,
'componentName' => 'sw-field',
'customFieldType' => 'textEditor',
'placeholder' => [
'en-GB' => 'Paste the Html content here ...',
'de-DE' => 'Fügen Sie den HTML-Inhalt hier ein ...',
],
'label' => [
'en-GB' => 'Product Tab Ingredients Html',
'de-DE' => 'Produkt Tab Inhaltsstoffe Html',
],
],
],
],
'relations' => [
[
'entityName' => $this->container->get(ProductDefinition::class)->getEntityName()
]
]
]
];
}
}