Guten Tag,
Ich habe wie in der Doku beschrieben mein Product mit einer weiteren Tabelle in der Datenbank um ShortDescription erweitert. Dazu habe ich das Entity, die EntityExtension und die EntityExtensionDefinition erstellt. Außerdem eine Migration für die Datenbank. Meine Frage ist nun wie ich ShortDescription von der Administration befüllen kann. Ich habe mir dort schon ein neues Textfeld für die ShortDescription hinzugefügt. Erstmal frage ich mich ob man wirklich so viel aufwand betreiben muss nur um ein weiteres Textfeld hinzuzufügen und wie ich jetzt von der Administration die Daten in ShortDescription speichere
Hier der Code
Entity:
<?php declare(strict_types=1);
namespace AlphaAllpaxCms\Extension\Content\ProductShortDescription;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
class ProductShortDescriptionEntity extends Entity
{
/**
* @var string
*/
protected $id;
/**
* @var string
*/
protected $productId;
/**
* @var string|null
*/
protected $shortDescription;
// Getter und Setter Methoden
public function getId(): string
{
return $this->id;
}
public function getProductId(): string
{
return $this->productId;
}
public function getShortDescription(): ?string
{
return $this->shortDescription;
}
public function setProductId(string $productId): void
{
$this->productId = $productId;
}
public function setShortDescription(?string $shortDescription): void
{
$this->shortDescription = $shortDescription;
}
}
EntityExtension:
<?php declare(strict_types=1);
namespace AlphaAllpaxCms\Extension\Content\Product;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
class ProductShortDescriptionExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new OneToOneAssociationField('short_description', 'id', 'product_id', ProductShortDescriptionExtensionDefinition::class, true)
);
}
public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}
EntityExtensionDefinition:
<?php declare(strict_types=1);
namespace AlphaAllpaxCms\Extension\Content\Product;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use AlphaAllpaxCms\Extension\Content\ProductShortDescription\ProductShortDescriptionEntity;
class ProductShortDescriptionExtensionDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'alphaallpaxcms_product_shortdescription';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
public function getEntityClass(): string
{
return ProductShortDescriptionEntity::class;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
new FkField('product_id', 'productId', ProductDefinition::class),
(new StringField('short_description', 'shortDescription')),
new OneToOneAssociationField('product', 'product_id', 'id', ProductDefinition::class, false)
]);
}
}
Migration:
<?php declare(strict_types=1);
namespace AlphaAllpaxCms\Migration;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;
class Migration1709804943ProductShortDescription extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1709804943;
}
public function update(Connection $connection): void
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `alphaallpaxcms_product_shortdescription` (
`id` BINARY(16) NOT NULL,
`product_id` BINARY(16) NULL,
`short_description` VARCHAR(255) NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id`),
KEY `fk.alphaallpaxcms_product_shortdescription.product_id` (`product_id`),
CONSTRAINT `fk.alphaallpaxcms_product_shortdescription.product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$connection->executeStatement($sql);
}
public function updateDestructive(Connection $connection): void
{
$connection->executeStatement('DROP TABLE IF EXISTS `alphaallpaxcms_product_shortdescription`');
}
}
Und die Erweiterung der Administration wenn man ein Produkt erstellt oder bearbeitet:
import template from './sw-product-basic-form.html.twig';
Shopware.Component.override('sw-product-basic-form', {
inject: [
'repositoryFactory'
],
template
});
{% block sw_product_basic_form_description_field %}
{% parent %}
{% set shortDescription = entity.shortDescription ?? '' %}
<div class="sw-form-group">
<label for="shortDescriptionField" class="sw-label"><!-- {{ 'admin.product.cardShortDescription.label'|trans }} -->Kurzbeschreibung</label>
<sw-text-editor v-model="product.shortDescription" id="shortDescriptionField"></sw-text-editor>
</div>
{% endblock %}