/**
  * Remove an attribute form a variant group
  *
  * @param string $code        The variant group code
  * @param int    $attributeId The attribute id
  *
  * @AclAncestor("pim_enrich_group_remove_attribute")
  *
  * @throws NotFoundHttpException If variant group or attribute is not found or the user cannot see it
  *
  * @return JsonResponse
  */
 public function removeAttributeAction($code, $attributeId)
 {
     $group = $this->findVariantGroupOr404($code);
     $attribute = $this->findAttributeOr404($attributeId);
     $template = $group->getProductTemplate();
     if (null !== $template) {
         $this->templateBuilder->removeAttribute($template, $attribute);
         $this->groupSaver->save($group);
     }
     return new JsonResponse();
 }
 /**
  * {@inheritdoc}
  *
  * @Template
  * @AclAncestor("pim_enrich_variant_group_create")
  */
 public function createAction(Request $request)
 {
     if (!$request->isXmlHttpRequest()) {
         return new RedirectResponse($this->router->generate('pim_enrich_variant_group_index'));
     }
     $group = $this->groupFactory->createGroup('VARIANT');
     $group->setProductTemplate($this->productTemplateBuilder->createProductTemplate());
     if ($this->groupHandler->process($group)) {
         $request->getSession()->getFlashBag()->add('success', new Message('flash.variant group.created'));
         $url = $this->router->generate('pim_enrich_variant_group_edit', ['code' => $group->getCode()]);
         $response = ['status' => 1, 'url' => $url];
         return new Response(json_encode($response));
     }
     return ['form' => $this->groupForm->createView()];
 }
 /**
  * @param RemoveEvent $event
  */
 public function preRemove(RemoveEvent $event)
 {
     $subject = $event->getSubject();
     if (!$subject instanceof AttributeInterface) {
         return;
     }
     $productTemplates = $this->productTplRepository->findByAttribute($subject);
     foreach ($productTemplates as $productTemplate) {
         $this->productTplBuilder->removeAttribute($productTemplate, $subject);
         $attributeCodes = $productTemplate->getAttributeCodes();
         if (empty($attributeCodes)) {
             $this->objectManager->remove($productTemplate);
         } else {
             $this->objectManager->persist($productTemplate);
         }
     }
 }