<?php declare(strict_types=1);
namespace EWHeaderFooter;
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\System\CustomField\CustomFieldTypes;
class EWHeaderFooter extends Plugin
{
public function install(InstallContext $installContext): void
{
$this->createCustomFields($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeCustomFields($uninstallContext->getContext());
}
private function createCustomFields($context){
$customFields = [
[
'name' => 'ew_header_footer_fields',
'active' => true,
'global' => true,
'config' => [
'label' => [
'en-GB' => 'Header and Footer',
'de-DE' => 'Header und Footer'
],
],
'customFields' => [
[
'name' => 'ew-hide-header',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'bordered' => false,
'label' => [
'en-GB' => 'Hide Header',
'de-DE' => 'Header ausblenden',
],
'customFieldPosition' => 1
]
],
[
'name' => 'ew-minimal-footer',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'bordered' => false,
'label' => [
'en-GB' => 'Second Footer (Own Footer for LP)',
'de-DE' => 'Zweiter Footer (Eigener Footer für LP)',
],
'customFieldPosition' => 1
]
],
],
'relations' => [
['entityName' => 'category']
]
],
];
$repo = $this->container->get('custom_field_set.repository');
$customFieldSetEntry = $repo->search((new Criteria())->addFilter(new EqualsFilter('name', 'ew_header_footer_fields')), $context)->first();
if(!$customFieldSetEntry) {
foreach ($customFields as $customFieldSet) {
$repo->upsert([$customFieldSet], $context);
}
}
}
private function removeCustomFields($context){
$cfsRepo = $this->container->get('custom_field_set.repository');
$cfRepo = $this->container->get('custom_field.repository');
$cfsId = $cfsRepo->search((new Criteria())->addFilter(new EqualsFilter('name', 'ew_header_footer_fields')), $context)->first();
$cfsRepo->delete([['id' => $cfsId->getId()]], $context);
$cfIds = $cfRepo->search((new Criteria())->addFilter(new EqualsFilter('customFieldSetId', $cfsId->getId())), $context)->getIds();
$ids = [];
foreach($cfIds as $id){
$ids[] = ['id' => $id];
}
$cfRepo->delete($ids, $context);
}
}