ikucheriavenko
6/7/2018 - 12:37 PM

override serialization group in view

    /**
     * @param array $groups
     * @param bool  $leavePrevious
     */
    protected function overrideViewSerializationGroups(array $groups, bool $leavePrevious = false)
    {
        $request = $this->get('request_stack')->getCurrentRequest();
        $view = $request->attributes->get('_template');
        if ($view) {
            $existingGroups = $view->getSerializerGroups();
            if ($leavePrevious) {
                $groups = array_merge($groups, $existingGroups);
            }
            $view->setSerializerGroups($groups);
        }
    }