<?php declare(strict_types=1);
namespace Moorl\ProductVideo;
use MoorlFoundation\Core\PluginFoundation;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class MoorlProductVideo extends Plugin
{
private function refreshPluginData(Context $context, $justDelete = null): void
{
/* @var $foundation PluginFoundation */
$foundation = $this->container->get(PluginFoundation::class);
$foundation->setContext($context);
if ($justDelete) {
$foundation->removeCustomFields('moorl_video');
$foundation->removeCmsSlots(['moorl_form_builder']);
$foundation->removeCmsBlocks(['moorl_form_builder']);
return;
}
$data = [[
'name' => 'moorl_video',
'config' => [
'label' => [
'en-GB' => 'Product video',
'de-DE' => 'Produktvideo',
],
],
'relations' => [['entityName' => 'media']],
'customFields' => [
[
'name' => 'moorl_video_src',
'type' => 'text',
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'URL to video',
'de-DE' => 'URL zum Video',
],
'placeholder' => [
'en-GB' => 'https://www.youtube.com/watch?v=AnyID',
'de-DE' => 'https://www.youtube.com/watch?v=IrgendeineID',
],
]
],
[
'name' => 'moorl_video_bg',
'type' => 'colorpicker',
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'colorpicker',
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'Background color',
'de-DE' => 'Hintergundfarbe',
],
]
],
[
'name' => 'moorl_video_cover',
'type' => 'media',
'config' => [
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'customFieldPosition' => 3,
'label' => [
'en-GB' => 'Cover',
'de-DE' => 'Cover',
],
]
],
]
]];
$foundation->updateCustomFields($data, 'moorl_video');
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->refreshPluginData($activateContext->getContext());
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->refreshPluginData($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->refreshPluginData($updateContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->refreshPluginData($uninstallContext->getContext(), true);
}
}