<?php
namespace dev24\Opengraph;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
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 dev24OpenGraph extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext); // TODO: Change the autogenerated stub
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create([
[
'name' => 'dev24_open_graph_custom_fields',
'global' => false,
'config' => [
'label' => [
'de-DE' => 'Open Graph',
'en-GB' => 'Open Graph'
]
],
'relations' => [
[
'entityName' => 'product'
],
[
'entityName' => 'category'
]
],
'customFields' => [
[
'name' => 'dev24_og_title',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Titel',
'en-GB' => 'Title'
],
'customFieldPosition' => 1
]
],
[
'name' => 'dev24_og_description',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Beschreibung',
'en-GB' => 'Description'
],
'customFieldPosition' => 2
]
],
[
'name' => 'dev24_og_image',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Bild',
'en-GB' => 'Image'
],
'componentName' => "sw-media-field",
'customFieldType' => "media",
'customFieldPosition' => 3
]
]
]
]
], $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext); // TODO: Change the autogenerated stub
$criteria = new Criteria();
$criteria->addFilter(
new MultiFilter(
'OR',
[
new EqualsAnyFilter('name', $this->getCustomFields())
]
)
);
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$idsToDelete = $customFieldSetRepository->searchIds($criteria, $uninstallContext->getContext());
if ($idsToDelete->getTotal() === 0) {
return;
}
$keys = array_map(function ($id) {
return ['id' => $id];
}, $idsToDelete->getIds());
if($idsToDelete->getTotal() > 0) {
$customFieldSetRepository->delete($keys, $uninstallContext->getContext());
}
}
protected function getCustomFields(): array
{
return [
'dev24_open_graph_custom_fields'
];
}
}