/**
  * 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();
 }
 /**
  * @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);
         }
     }
 }