# Artikel-Bewertung Controller "überschreiben"

**URL:** <https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846>\
**Category:** Programmierung\
**Created:** [11. Juli 2017 um 19:13 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846 "2017-07-11T19:13:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxf5](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@maxf5](https://forum.shopware.com/u/maxf5)\
**Post date:** [11. Juli 2017 um 19:13 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/1 "2017-07-11T19:13:37Z")

</div>

Guten Abend zusammen

ich möchte gerne die Artikel-Bewertungen&nbsp;um ein paar Felder zu reduzieren. Ich habe per Overwrite die Smarty Blöcke leergelassen, aber Shopware prüft trotzdem die nicht vorhandenen Pflichtfelder.

Die Validierung erfolgt hier:  
[shopware/Detail.php at 5.2 · shopware/shopware · GitHub](https://github.com/shopware/shopware/blob/5.2/engine/Shopware/Controllers/Frontend/Detail.php#L209)

Kann mir jemand erklären wie ich diesen Controller überschreiben kann mit einem eigenen Plugin?  
zB:  
if (empty(Shopware()-\>System()-\>\_POST[‘sVoteName’])) {  
$sErrorFlag[‘sVoteName’] = **false** ;  
}

Vielen Danke im Voraus.

---

<div class="post-metadata">

**Author:** ![Thomas](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/thomas/32/8167_2.png) [@Thomas](https://forum.shopware.com/u/Thomas)\
**Post date:** [11. Juli 2017 um 20:26 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/2 "2017-07-11T20:26:25Z")

</div>

```
 'replaceRatingAction'
        ];
    }

    public function replaceRatingAction(\Enlight_Hook_HookArgs $args)
    {
        /** @var \Shopware_Controllers_Frontend_Detail $subject */
        $subject = $args->getSubject();

        $id = (int) $subject->Request()->sArticle;
        if (empty($id)) {
            return $subject->forward('error');
        }

        $article = Shopware()->Modules()->Articles()->sGetArticleNameByArticleId($id);
        if (empty($article)) {
            return $subject->forward('error');
        }

        $voteConfirmed = false;

        if ($hash = $subject->Request()->sConfirmation) {
            $getVote = Shopware()->Db()->fetchRow('
                SELECT * FROM s_core_optin WHERE hash = ?
            ', [$hash]);
            if (!empty($getVote['data'])) {
                Shopware()->System()->_POST = unserialize($getVote['data']);
                $voteConfirmed = true;
                Shopware()->Db()->query('DELETE FROM s_core_optin WHERE hash = ?', [$hash]);
            }
        }

        if (empty(Shopware()->System()->_POST['sVoteSummary'])) {
            $sErrorFlag['sVoteSummary'] = true;
        }

        if (!empty(Shopware()->Config()->CaptchaColor) && !$voteConfirmed) {
            $captcha = str_replace(' ', '', strtolower($subject->Request()->sCaptcha));
            $rand = $subject->Request()->getPost('sRand');
            if (empty($rand) || $captcha != substr(md5($rand), 0, 5)) {
                $sErrorFlag['sCaptcha'] = true;
            }
        }
        $validator = $this->container->get('validator.email');
        if (!empty(Shopware()->Config()->sOPTINVOTE)
            && (empty(Shopware()->System()->_POST['sVoteMail'])
                || !$validator->isValid(Shopware()->System()->_POST['sVoteMail']))
        ) {
            $sErrorFlag['sVoteMail'] = true;
        }

        if (empty($sErrorFlag)) {
            if (!empty(Shopware()->Config()->sOPTINVOTE)
                && !$voteConfirmed && empty(Shopware()->Session()->sUserId)
            ) {
                $hash = \Shopware\Components\Random::getAlphanumericString(32);
                $sql = '
                    INSERT INTO s_core_optin (datum, hash, data)
                    VALUES (NOW(), ?, ?)
                ';
                Shopware()->Db()->query($sql, [
                    $hash, serialize(Shopware()->System()->_POST->toArray()),
                ]);

                $link = $subject->Front()->Router()->assemble([
                    'sViewport' => 'detail',
                    'action' => 'rating',
                    'sArticle' => $id,
                    'sConfirmation' => $hash,
                ]);

                $context = [
                    'sConfirmLink' => $link,
                    'sArticle' => ['articleName' => $article],
                ];

                $mail = Shopware()->TemplateMail()->createMail('sOPTINVOTE', $context);
                $mail->addTo($subject->Request()->getParam('sVoteMail'));
                $mail->send();
            } else {
                unset(Shopware()->Config()->sOPTINVOTE);
                Shopware()->Modules()->Articles()->sSaveComment($id);
            }
        } else {
            $subject->View()->sFormData = Shopware()->System()->_POST->toArray();
            $subject->View()->sErrorFlag = $sErrorFlag;
        }

        $subject->View()->sAction = 'ratingAction';

        $subject->forward('index');
    }
}

```

Keine Gewähr&nbsp;- hab es nicht getestet.

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [11. Juli 2017 um 20:39 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/3 "2017-07-11T20:39:25Z")

</div>

Ich würde die benötigten Elemente verstecken und standardmäßig mit irgendetwas füllen. So brauchst du keine 200 Zeilen in deinen replace hook zu kopieren.

Viele Grüße

---

<div class="post-metadata">

**Author:** ![Thomas](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/thomas/32/8167_2.png) [@Thomas](https://forum.shopware.com/u/Thomas)\
**Post date:** [12. Juli 2017 um 07:04 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/4 "2017-07-12T07:04:42Z")

</div>

> [@Aquatuning GmbH schrieb:](https://forum.shopware.com/profile/11752/Aquatuning%20GmbH "Aquatuning%20GmbH")
> 
> Ich würde die benötigten Elemente verstecken und standardmäßig mit irgendetwas füllen. So brauchst du keine 200 Zeilen in deinen replace hook zu kopieren.

Das ist natürlich auch eine valide Lösung. Ich hätte nur keine&nbsp;Lust diesen Dummy Müll in der Datenbank zu haben :-)&nbsp;

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [12. Juli 2017 um 07:54 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/5 "2017-07-12T07:54:51Z")

</div>

Ich würde auf alle Fälle die Lösung von [@Aquatuning GmbH]([http://forum.shopware.com/profile/11752/Aquatuning](http://forum.shopware.com/profile/11752/Aquatuning) GmbH “Aquatuning GmbH”)‍ bevorzugen. Replace-Hooks sollte man so gut es geht vermeiden. Einfach im PreDispatch-Event die Felder mit Dummy-Daten füllen. Und falls es dich wirklich in der Datenbank stört, kannst du sie ja im PostDispatch-Event wieder entfernen.

Speicherplatzverbrauch in der Datenbank bleibt sowieso gleich, da pro Zeile der Platz immer al­lo­zie­rt wird.

Viele Grüße

---

<div class="post-metadata">

**Author:** ![Thomas](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/thomas/32/8167_2.png) [@Thomas](https://forum.shopware.com/u/Thomas)\
**Post date:** [12. Juli 2017 um 08:19 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/6 "2017-07-12T08:19:07Z")

</div>

> [@maxf5 schrieb:](https://forum.shopware.com/profile/25706/maxf5 "maxf5")
> 
> Kann mir jemand erklären wie ich diesen Controller überschreiben kann mit einem eigenen Plugin?

Mir war nicht bewusst, dass&nbsp;ich mit der Beantwortung der Frage hier so eine Grundsatzdiskussion auslöse.&nbsp; ![Grin](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/70033cde8e8ff79cfa538293ae71318d6f788423.png "Grin") ![Smile](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/bac5f5766b6a20565a8fb995345b4e41d24daac0.png "Smile")&nbsp;

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [12. Juli 2017 um 08:36 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/7 "2017-07-12T08:36:13Z")

</div>

Mit Grundsatzdiskussion hat das wenig zu tun. Shopware selbst sagt schon lange:

> […] For these reasons, events should always be preferred over hooks, **especially the replace hook should be avoided when possible**. Also that’s the reason why only core classes and repositories are hookable in Shopware. […]

> **[Understanding the Shopware hook system](https://developers.shopware.com/blog/2015/06/09/understanding-the-shopware-hook-system/)**
>
> Shopware was built with plugin developers in mind, so there are powerful ways to modify the default behaviour of the system 
> without losing backward compat

Replace-Hooks sind einfach Bad-Practice, da sollte man den Fragesteller vor dieser Lösung warnen ![Sticking-out-tongue](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/sticking-out-tongue.png "Sticking-out-tongue").&nbsp;

Viele Grüße

---

<div class="post-metadata">

**Author:** ![Thomas](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/thomas/32/8167_2.png) [@Thomas](https://forum.shopware.com/u/Thomas)\
**Post date:** [12. Juli 2017 um 09:42 UTC](https://forum.shopware.com/t/artikel-bewertung-controller-uberschreiben/46846/8 "2017-07-12T09:42:05Z")

</div>

> Replace-Hooks sind einfach Bad-Practice, da sollte man den Fragesteller vor dieser Lösung warnen ![Sticking-out-tongue](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/ac0c93e2160fb9e91c30767775d64444ad0f06c4.png "Sticking-out-tongue").&nbsp;

Vorher Daten in die Datenbank zu schreiben um diese dann direkt danach wieder zu löschen ist genau so Bad-Practise.&nbsp; ![Sticking-out-tongue](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/ac0c93e2160fb9e91c30767775d64444ad0f06c4.png "Sticking-out-tongue")
