Jump to content

Marque : Ajout d'une deuxième image


Pierre335

Recommended Posts

Salut @tous, 
j'ai crée un module pour ajouter une deuxième image pour les marques ; le champ est bien ajouté côté BD + classe Manufacturer .
j'utilise les hooks action Modifier.
Qui a idée pour ce genre de prob ? comment je pourrais enregistrer l'image et avoir sa preview coté BO ?
 

public function hookActionManufacturerFormBuilderModifier($params)
    {
        
        $languages = Language::getLanguages();
        $formBuilder = $params['form_builder'];
        $formBuilder->add('second_logo', 
            FileType::class, [
            'label' => $this->getTranslator()->trans('Second Image', [], 'Modules.brandextras'),
            'required' => false,
            'mapped' => false,
            'constraints' => [
                new File([
                    'mimeTypes' => [
                        'image/*',
                    ],
                    'mimeTypesMessage' => 'Imgae format',
                ])
            ]
        ]);

          
        $manufacturer = new Manufacturer((int)$params['id']);
        
        $params['data']['second_logo'] = $manufacturer->second_logo;
       

        $formBuilder->setData($params['data']);
    }
public function hookActionAfterCreateManufacturerFormHandler(array $params)
    {
        $this->updateData($params['form_data'], $params);
    }


    public function hookActionAfterUpdateManufacturerFormHandler(array $params)
    {
        $this->updateData($params['form_data'], $params);
    }
protected function updateData(array $data,$params)
    {
   
        $savePath = _PS_ROOT_DIR_.'/img/m';
        $uploader = new Uploader('second_logo');
         
        $file = $uploader->setAcceptTypes(['jpg', 'png', 'git', 'jpeg'])
                         ->setCheckFileSize(UploaderCore::DEFAULT_MAX_SIZE)
                        ->setSavePath($savePath)
                        ->process();

        $fileName = ltrim(str_replace($savePath, '', $file[0]['save_path']), '/');
        
        try {
           
            $manufacturer = new Manufacturer((int)$params['id']);
            $manufacturer->second_logo = $fileName;
            $manufacturer->save();
            
        } catch (PrestaShopException $e) {
            echo $e->getMessage();
            return false;
        }
 
        return true;
    }

le nouveau champ apparaît bien en BO , quand je télécharge une image , une erreur se produise 
 

Quote

Une erreur inattendue s'est produite. [Symfony\Component\Debug\Exception\ContextErrorException code 0]

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...