Jump to content

AdminCategoriesController form fields overriding


Krystian Podemski

Recommended Posts

Hello PrestaShopers,

 

Someone working with overriding AdminControllers::renderForm()?

 

It is possible to add inputs by using $fields_form_override method, but it is possible to override already assigned inputs?

 

We can add fields like that:

 

<?php
class AdminCategoriesController extends AdminCategoriesControllerCore
{
public function renderForm()
{
	$this->fields_form_override = array(
		'type' => 'text',
		'label' => $this->l('My test input:'),
		'name' => 'MyTestInput',
		'lang' => true,
		'size' => 48,
		'required' => true,
	);		return parent::renderForm();
}
}

 

but how to change other inputs, already assigned to variable $fields_form? Unsetting in overrided Class doesn't work for me.

 

Is the $fields_form variable should not be static?

Edited by Krystian Podemski (see edit history)
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Well the problem is that $fields_form is assigned parent's renderForm method, and as we have to call it and return its results, any changes made in the overrided class won't affect the variable.

 

The Core classes (plural, as it's the case for most of the admin controllers) should not reassign fields_form if it already set. I think you can "hack" the core to fit this until PS team improve the core.

 

In the core class, method renderForm, you could add an if (empty($this->fields_form)) {} about the $this->fields_form assignation. It's the only way to do it, for now, seeing how it is actually written in the core.

 

PS: I answer here, in an one month topic ressurected from one month before as I found it on google looking for something close and see it unanswered.

Edited by mdiblasio (see edit history)
Link to comment
Share on other sites

  • 6 months later...

Hi,

 

I 've just found a issue to add more than one field with fields_form_override function.

Override your AdminController with this function (only one line added):

public function renderForm()
{
   if (!$this->default_form_language)
                      $this->getLanguages();
   if (Tools::getValue('submitFormAjax'))
                       $this->content .= $this->context->smarty->fetch('form_submit_ajax.tpl');
 if ($this->fields_form && is_array($this->fields_form))
 {
        if (!$this->multiple_fieldsets)
                   $this->fields_form = array(array('form' => $this->fields_form));
        // For add a fields via an override of $fields_form, use $fields_form_override
        //Add foreach function to add more than one field
        if (is_array($this->fields_form_override) && !empty($this->fields_form_override))
						foreach($this->fields_form_override as $input){
                                   $this->fields_form[0]['form']['input'][] = $input;
                           }
      $helper = new HelperForm($this);
      $this->setHelperDisplay($helper);
      $helper->fields_value = $this->getFieldsValue($this->object);
      $helper->tpl_vars = $this->tpl_form_vars;
      !is_null($this->base_tpl_form) ? $helper->base_tpl = $this->base_tpl_form : '';
      if ($this->tabAccess['view'])
       {
            if (Tools::getValue('back'))
                     $helper->tpl_vars['back'] = Tools::safeOutput(Tools::getValue('back'));
            else
                      $helper->tpl_vars['back'] =                              Tools::safeOutput(Tools::getValue(self::$currentIndex.'&token='.$this->token));
  }
  $form = $helper->generateForm($this->fields_form);
  return $form;
 }
}

 

Then you can add many fileds you want like this (AdminTaxesController example here):

public function renderForm()
{
	$this->fields_form_override = array(
array('type' => 'text',
 'label' => $this->l('Account Identifier : '),
 'name' => 'account_id',
 'required' => false,
 'size' => 41,
 'maxlength' => 128,
 'desc' => $this->l('Fill in your account identifier for this taxe, if empty id_tax whould be taken by default.')
									),
							 array('type' => 'text',
 'label' => $this->l('Account Base Identifier : '),
 'name' => 'account_id_base',
 'required' => false,
 'size' => 41,
 'maxlength' => 128,
 'desc' => $this->l('Fill in your account base identifier for this taxe, if empty 000 whould be taken by default.')
				));
	return parent::renderForm();
}

Edited by IllicoPresta (see edit history)
Link to comment
Share on other sites

  • 1 month later...

An alternative method is using array_merge, you have to override AdminController with:

public function renderForm()
{
 if (!$this->default_form_language)
  $this->getLanguages();
 if (Tools::getValue('submitFormAjax'))
  $this->content .= $this->context->smarty->fetch('form_submit_ajax.tpl');
 if ($this->fields_form && is_array($this->fields_form))
 {
  if (!$this->multiple_fieldsets)
   $this->fields_form = array(array('form' => $this->fields_form));
  // For add a fields via an override of $fields_form, use $fields_form_override
  if (is_array($this->fields_form_override) && !empty($this->fields_form_override))
 $this->fields_form[0]['form']['input'] = array_merge($this->fields_form[0]['form']['input'], $this->fields_form_override);
   //$this->fields_form[0]['form']['input'][] = $this->fields_form_override;
  $helper = new HelperForm($this);
  $this->setHelperDisplay($helper);
  $helper->fields_value = $this->getFieldsValue($this->object);
  $helper->tpl_vars = $this->tpl_form_vars;
  !is_null($this->base_tpl_form) ? $helper->base_tpl = $this->base_tpl_form : '';
  if ($this->tabAccess['view'])
  {
   if (Tools::getValue('back'))
 $helper->tpl_vars['back'] = Tools::safeOutput(Tools::getValue('back'));
   else
 $helper->tpl_vars['back'] = Tools::safeOutput(Tools::getValue(self::$currentIndex.'&token='.$this->token));
  }
  $form = $helper->generateForm($this->fields_form);
  return $form;
 }
}

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...