Jump to content

How to use HelperOptions


jmeile

Recommended Posts

Hi

 

I'm just wondering how to use the HelperOptions. I want to do an own plugging for showing the social networking widgets in the right or the left columns. I got my module working by following the tutorial:

 

http://doc.prestasho...estaShop+module

 

Now I want to have several configuration forms in a page like this:

 

+ Facebook widget options
| - Widget options [several controls are shown here: text, textarea, etc. ]
+ Twitter widget options
| - Widget options [several controls are shown here: text, textarea, etc. ]

 

So, I followed the example here:

http://doc.prestasho...5/HelperOptions

 

And adapted it to my needs:

 

public function displayForm()
{
 $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 $fields_form = array(
   'facebook' => array(
	'title' => $this->l('Facebook widget'),
	'description' => $this->l('In order to preview the widget, please go here first: ').'http://developers.facebook.com/docs/plugins',
	'fields' => array(
	 'facebook_widget' => array(
	  'title' => $this->l('Facebook widget code:'),
	  'type' => 'textarea',
	  'identifier' => 'id_facebook_widget',
	  'cols' => 60,
	  'rows' => 5
	 ),
	),
   ),
   'twitter' => array(
	'title' => $this->l('Twitter widget'),
	'description' => $this->l('In order to preview the widget, please go here first: ').'https://twitter.com/about/resources/buttons',
	'fields' => array(
	 'twitter_widget' => array(
	  'title' => $this->l('Twitter widget code:'),
	  'type' => 'textarea',
	  'identifier' => 'id_twitter_widget',
	  'cols' => 60,
	  'rows' => 5
	 ),
	),
   ),
  );

 $helper = new HelperForm();
 $helper->module = $this;
 $helper->name_controller = $this->name;
 $helper->token = Tools::getAdminTokenLite('AdminModules');
 $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
 $helper->default_form_language = $default_lang;
 $helper->allow_employee_form_lang = $default_lang;
 $helper->title = $this->displayName;
 $helper->show_toolbar = true;
 $helper->toolbar_scroll = true;
 $helper->submit_action = 'submit'.$this->name;
 $helper->toolbar_btn = array(
  'save' => array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
  ),
  'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
  )
 );
 $helper->fields_value['facebook_widget'] = Configuration::get('blocksocialJM_facebook_widget');
 $helper->fields_value['twitter_widget'] = Configuration::get('blocksocialJM_twitter_widget');
 return $helper->generateForm($fields_form);
}

 

At first, I only have two textareas on each form, but later I will add some other controls: select, radio buttons, etc..

 

For this easy example, I'm getting this:

Notice: Undefined index: form in C:\wamp\www\prestashop_1-5-4\cache\smarty\compile\5a40d1b1f19f449c171f7e5a9cd689f798c5b6c9.file.form.tpl.php on line 115

 

Looking the whole traceback brings me to this line:

return $helper->generateForm($fields_form);

 

So, I guess the HelperOptions aren't compatible with the HelperForm, so, the question is: How do I use the HelperOptions?

 

Best regards

Josef

Link to comment
Share on other sites

Ok, now I have two problems, first I get:

 

Notice: Undefined property: HelperOptionsCore::$id in C:\wamp\www\prestashop_1-5-4\classes\helper\HelperOptions.php on line 49

 

Looking at the code from Helper.php and HelperOptions, there isn't indeed an "id" property that you can set. It is only present in the HelperForm.

 

Then, how do you save the values after submitting the form and update them in the form? The example:

http://doc.prestashop.com/display/PS15/HelperOptions

 

must be expanded, ie: it doen't tell you that you have to create a HelperOptions object and it doesn't tell you how to render the forms.

 

Best regards

Josef

 

This is my final code:

public function displayForm()
{
 $fields_options = array(
   'facebook' => array(
	'title' => $this->l('Facebook widget'),
	'description' => $this->l('In order to preview the widget, please go here first: ').'http://developers.facebook.com/docs/plugins',
	'fields' => array(
	 'blocksocialJM_facebook_widget' => array(
	  'title' => $this->l('Facebook widget code:'),
	  'type' => 'textarea',
	  'identifier' => 'id_blocksocialJM_facebook_widget',
	  'cols' => 60,
	  'rows' => 5
	 ),
	),
   ),
   'twitter' => array(
	'title' => $this->l('Twitter widget'),
	'description' => $this->l('In order to preview the widget, please go here first: ').'https://twitter.com/about/resources/buttons',
	'fields' => array(
	 'blocksocialJM_twitter_widget' => array(
	  'title' => $this->l('Twitter widget code:'),
	  'type' => 'textarea',
	  'identifier' => 'blocksocialJM_twitter_widget',
	  'cols' => 60,
	  'rows' => 5
	 ),
	),
   ),
  );
 $helper = new HelperOptions();
 $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
 $helper->token = Tools::getAdminTokenLite('AdminModules');
 $helper->module = $this;
 $helper->title = $this->displayName;
 $helper->show_toolbar = true;
 $helper->toolbar_scroll = true;
 $helper->toolbar_btn = array(
  'save' => array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
  ),
  'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
  )
 );
 return $helper->generateOptions($fields_options);
}

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

Ok, finally I got it working. I was missing the submit button. The only thing I would like to know is if there is someway of changing the name of the submit button. It defaults to "submitOptions".

 

Anyway, I think that the HelperOptions example must be extended. Just showing how the data is arranged didn't help. I had to test a lot till I got it.

 

It works now, but there is still a problem when activating the debug mode: _PS_MODE_DEV_. I still get the following warnings:

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\prestashop_1-5-4\classes\controller\AdminController.php on line 910
( ! ) Notice: Undefined property: HelperOptions::$id in C:\wamp\www\prestashop_1-5-4\classes\helper\HelperOptions.php on line 49

 

It could be that they are only meaningless warnings.

 

So, here is the final code:

public function getContent()
{
if (Tools::isSubmit('submitOptions'))
{
 $blocksocialJM_facebook_widget = Tools::getValue('blocksocialJM_facebook_widget');
 $blocksocialJM_twitter_widget = Tools::getValue('blocksocialJM_twitter_widget');
 Configuration::updateValue('blocksocialJM_facebook_widget', $blocksocialJM_facebook_widget);
 Configuration::updateValue('blocksocialJM_twitter_widget', $blocksocialJM_twitter_widget);
 $this->_clearCache('blocksocialJM.tpl');
}
return $this->displayForm();
}
public function displayForm()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_options = array(
  'facebook' => array(
   'title' => $this->l('Facebook widget'),
   'image' => '../img/admin/information.png',
   'description' => $this->l('In order to preview the widget, please go here first: ').'http://developers.facebook.com/docs/plugins',
   'fields' => array(
    'blocksocialJM_facebook_widget' => array(
	 'title' => $this->l('Facebook widget code:'),
	 'type' => 'textarea',
	 'identifier' => 'id_blocksocialJM_facebook_widget',
	 'cols' => 60,
	 'rows' => 5
    ),
   ),
   'submit' => array(
    'title' => $this->l('Save'),
    'class' => 'button'
   ),
  ),
  'twitter' => array(
   'title' => $this->l('Twitter widget'),
   'image' => '../img/admin/information.png',
   'description' => $this->l('In order to preview the widget, please go here first: ').'https://twitter.com/about/resources/buttons',
   'fields' => array(
    'blocksocialJM_twitter_widget' => array(
	 'title' => $this->l('Twitter widget code:'),
	 'type' => 'textarea',
	 'identifier' => 'blocksocialJM_twitter_widget',
	 'cols' => 60,
	 'rows' => 5
    ),
   ),
  ),
 );
$helper = new HelperOptions();
$helper->module = $this;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->toolbar_btn = array(
 'save' => array(
  'desc' => $this->l('Save'),
  'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
  '&token='.Tools::getAdminTokenLite('AdminModules'),
 ),
 'back' => array(
  'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
  'desc' => $this->l('Back to list')
 )
);
return $helper->generateOptions($fields_options);
}

Link to comment
Share on other sites

  • 1 month later...

Hy jmeile,

 

I'm struggling with HelperOptions.

In the end I copy paste your code in my file and I still have some

Notice: Undefined property: HelperOptions::$id in /srv/www/htdocs/shop/classes/helper/HelperOptions.php on line 49

I don't understand what's wrong.

In the HelperOptions Line 49, I observe this :

$tab = Tab::getTab($this->context->language->id, $this->id);

Do you know where to set Tabs for a Helper Options

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

Hi flotho

 

I don't really recall that error. I just remember having some warnings. I could get ride of someone, but others remained. Anyway, the configuration is working now. I changed my code as follows:

 

After initalizing the fields, I replaced those lines by:

$this->helper = new HelperOptions($this);
// Module, token and currentIndex
$this->helper->id = $this->id;
$this->helper->module = $this;
$this->helper->name_controller = $this->name;
$this->helper->token = Tools::getAdminTokenLite('AdminModules');
$this->helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;

// Title and toolbar
$this->helper->title = $this->displayName;
$this->helper->show_toolbar = true;
$this->helper->toolbar_scroll = true;
$helper->submit_action = 'submit'.$this->name;
$this->helper->toolbar_btn = array(
'save' => array(
 'desc' => $this->l('Save'),
 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
   '&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
 'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
 'desc' => $this->l('Back to list')
)
);
return $this->helper->generateOptions($fields_options);

 

I'm not sure if it solved your problem.

 

Best regards

Josef

  • Like 1
Link to comment
Share on other sites

Hi flotho

 

I'm glad that you solved your problem.

 

Anyway, I hope that the developers improve the documentation of the HelperForm, HelperList, and HelperOptions classes. There isn't an easy example on how to use them. I think somewhere I saw only one example about the HelperForm class and some on the PrestaShop source, which after lots of coding, gave me some light about the HelperOptions class. To me those classes looks like a powerfull but unexplored resource. It really saves you some coding.

 

Best regards

Josef

Edited by jmeile (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

The same here. I agree the docs don't dive too deep. Also, I see that here in forums, for most of the questions, if they go a little further the basics, you'll probably will not get answers. Also, you won't fine a really professional prestashop book.

I guess They don't really want us to learn all the things too fast. Maybe for commercial reasons? Bah..

Link to comment
Share on other sites

  • 5 months later...

Hello

 

So what is the deal with that HelperOptions, I'm trying too use it for module configuration (got same errors as above).

 

Is options in configuration table should be automatically saved and there is some sort of bug in Presta?

Or saving these options is not purpose of that helper? (fields have cast parameter so it looks like that helper will try to validate POST'ed values and it write information about correctly updated values after submitting form)

 

In fact it's look like none of modules using it, but a loot's of controller/admin/... controllers does, like AdminMetaController.php

 

Maybe it can't be used in module?

Edited by Piotr Kaczor (see edit history)
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...