PrestaShop FormHelper preload multilanguage values - php

here I am seeing a small problem, I have two tables template and template_text, in template table I store the template type and visibility data and in the template_text table I store the template text in many languages, atm. I have two langs. such as EN and DE. So, when I am editing the template I preload the current template values into the form, but there is a problem with the values like autoemailer_title and autoemailer_body, when the from helper generates the form field it is generating this fields this way autoemailer_title_1, autoemailer_title_2 according the language ID, so, the question would be how to preload all this values the right way? I could use javascript and ajax get request to get the values and then set them by using the input ID, but I think there is a better way using the form helper.
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l($params['title']),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select', // This is a <select> tag.
'label' => $this->l('Template type'), // The <label> for this <select> tag.
'desc' => $this->l('Choose a template type'), // A help text, displayed right next to the <select> tag.
'name' => 'autoemailer_type', // The content of the 'id' attribute of the <select> tag.
'required' => true, // If set to true, this option must be set.
'options' => array(
'query' => array(
array(
'id_option' => 'static', // The value of the 'value' attribute of the <option> tag.
'name' => 'Static' // The value of the text content of the <option> tag.
),
array(
'id_option' => 'dynamic',
'name' => 'Dynamic'
),
), // $options contains the data itself.
'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
)
),
array(
'type' => 'text',
'label' => $this->l('News letter title'),
'name' => 'autoemailer_title',
'desc' => $this->l('Title'),
'lang' => true,
// 'style' => 'width:300px',
'class' => 'fixed-width-lg',
),
array(
'type' => 'textarea',
'label' => $this->l('Text'),
'name' => 'autoemailer_body',
'desc' => $this->l('Email body'),
'lang' => true,
'cols' => 60,
'rows' => 10,
'class' => 'rte', // we need this for setuping the tiny mce editor
'autoload_rte' => true, // we need this for setuping the tiny mce editor
),
),
'submit' => array(
'title' => $this->l('Save')
)
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'newTemplate';
$helper->currentIndex = $this->getThisUrl() . '&emailer_action=main';
$helper->token = Tools::getAdminTokenLite('AdminModules');
return $helper->generateForm(array($fields_form));

Solved this one. So, when we have more than one lang then the attribute for which we apply the multilangiage option becomes an array, so to set the value according the language we can just do this way :
$lang_id = 1; // setting the language ID, usually it's done by foreaching the records from ps_lang table
$helper->fields_value['description'][$lang_id] = 'my default value'; // setting the default value
return $helper->generateForm(array($fields_form)); // returning the output of generated form

Related

Adding a dropdown menu in Prestashop 1.7 module

I'm so beginner in Prestashop 1.7, I wanted to add a dropdown select section in my banner module to select the way to open the banner link.
but the selected value is never passed to the HTML, the code below IS passed but the one under isn't, can you please assist me?
[enter image description here][1]
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Banner description', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_DESC',
'desc' => $this->trans('Please enter a short but meaningful description for the banner.', array(), 'Modules.Banner.Admin')
)
array(
'type' => 'select', //select
'lang' => true,
'label' => $this->trans('Banner tab', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_TAB',
'required'=>'true',
'options' => array(
'query' => array(
array('key' => '_blank', 'name' => 'New tab'),
array('key' => '_self', 'name' => 'Same tab'),
),
'id' => 'key',
'name' => 'name'
),
'desc' => $this->trans('Please select the way to open the link.', array(), 'Modules.Banner.Admin')
)
This is how it looks in the Backoffice:
Here
You not only need to add a new field to your form but also handle saving the data from it.
Take a look at a few examples:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L122
Notice how the module author managed to save each configuration field from the form. This is what you need to do.
If you want to have access to data in your view, you have to pass it:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L244
Maybe after you added a new field, you forgot to handle the saving + passing to the view?

Show the configuration values in the text field of form in prestashop 1.7

I am creating a module in prestashop 1.7 to save my settings.
Also I created a form to display my settings. Form sample is shown below:-
//display form function
public function renderCustomerForm()
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Customer Settings'),
'icon' => 'icon-time'
),
'input'=>array(
array(
'type' => 'text',
'label' => $this->l('BusinessCustomerFlag'),
'name' => 'C_BUSINESS_FLAG',
'lang' => false,
'required' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => 'submitCustomer',
'icon' => 'process-icon-save'
)
);
I am saving this values in configuration table using configuration class functions.
I know how to retrieve it but don't know how to show in the form. Please some one guide on this will be really helpful.
Add this line to helper on your module (before generateForm):
$helper->fields_value = $this->getFormValues();
and add function to define values:
public function getFormValues()
{
$fields_value = array();
$fields_value['C_BUSINESS_FLAG'] = "some data or retrieved data";
return $fields_value;
}

Prestashop 1.6 - Create Forms with more than 1 submit buttons

I'm trying to implement a form in a tab on the back office on prestashop 1.6.
I've succeeded creating the form using the Helper class and all works fine. However, of what I see the helper class only allows 1 submit button. For my needs I need to use more than 1 that perform different actions on the postProcess() part of the controller. Any help on achieving that would be much appreciated. Here is my render form that works with a single submit button:
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Contact details'),
'icon' => 'icon-envelope'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Account owner'),
'name' => 'BANK_WIRE_OWNER',
),
array(
'type' => 'textarea',
'label' => $this->l('Details'),
'name' => $this->l('test2'),
'desc' => $this->l('Such as bank branch, IBAN number, BIC, etc.')
),
array(
'type' => 'textarea',
'label' => $this->l('Bank address'),
'name' => 'BANK_WIRE_ADDRESS',
),
),
'submit' => array(
'title' => $this->l('Save'),
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = 1;
$helper->fields_value['BANK_WIRE_OWNER'] = "";
$helper->fields_value['test2'] = "";
$helper->fields_value['BANK_WIRE_ADDRESS'] = "";
$this->fields_form = array();
$helper->submit_action = 'test';
return $helper->generateForm(array($fields_form));
}
You can create more forms on the same screen.
return $helper->generateForm(array($fields_form1, $fields_form2, $fields_form3));
Or if you really need to reuse the same form, you can add <select> as a last form element, that will give you a choice of action to be executed by submit button.

Creating new node type with custom field makes page for adding the node without any fields in Drupal 7

I'm trying to make a simple custom field "field_book_year" for the new node type "synopsis_book".
I have wrote in .install file:
function synopsis_install() {
node_types_rebuild();
$types = node_type_get_types();
// In Drupal 7 you must explicitly add a "body" field when you create a custom content type,
// thus the call to node_add_body_field($types['newsletter']);.
node_add_body_field($types['synopsis_book']);
$body_instance = field_info_instance('node', 'body', 'synopsis_book');
$body_instance['type'] = 'text';
field_update_instance($body_instance);
field_create_field(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'type' => 'text',
'translatable' => TRUE,
)
);
field_create_instance(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'entity_type' => 'node',
'bundle' => 'synopsis_book',
'type' => 'text',
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'example_node_list' => array(
'label' => t('Company posting the job listing'),
'type' => 'text',
),
),
'description' => 'Begin Date',
)
);
}
Then, I have in .module these functions:
function synopsis_node_info()
{$types = node_type_get_types();print_r($types);
return array(
'synopsis_author' => array('name' => t('Author'), 'base' => 'synopsis_author', 'description' => t('Author description, biography etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Author full name')
),
'synopsis_book' => array('name' => t('Book'), 'base' => 'synopsis_book', 'description' => t('Book description, synopsis etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Book title')
),
);
}
/**
* Implement hook_form() with the standard default form.
*/
function synopsis_book_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_validate().
*/
function synopsis_book_validate($node)
{
// Enforce a minimum character count of 2 on company names.
if (isset($node->field_book_year) &&
strlen($node->field_book_year['und'][0]['value']) < 2
) {
form_set_error('job_post_company',
t('The company name is too short. It must be atleast 2 characters.'),
$limit_validation_errors = NULL);
}
}
Not matter all I've written, the page of adding the Book looks like this! No 'field_book_year' field. There is even no body field
What am I doing wrong? Thank you.
I think the problem you're having is that hook_install is run when your module is installed, before the node type is created. So essentially you're trying to attach a field to a non-existant node type at that point.
The way I've always done this is to create the node type in hook_install before trying to attach the field (this is an example from a module that provides a testimonial content type):
// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
$type = array(
'type' => 'testimonial',
'name' => st('Testimonial'),
'base' => 'node_content',
'description' => st("Use <em>basic pages</em> for your static content, such as an 'About us' page."),
'custom' => 1,
'modified' => 1,
'locked' => 0,
'title_label' => 'Customer / Client Name'
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
// See if the testimonial date field exists
if (!field_info_field('field_testimonial_date')) {
field_create_field(array(
// Field info here
));
}
// If the date field is not attached, attach it
if (!field_info_instance('node', 'field_testimonial_date', 'testimonial')) {
field_create_instance(array(
// Field instance info here
));
}
If you look in the standard installation profile install file you'll see this is actually how the Drupal core performs this task, so I would expect it's the 'correct' way to do it.

How do I save custom node types in Drupal 7

I've created a custom node type in Drupal 7, using the hook_node_info method in the install file:
// declare the new node type
function foo_node_info ( ) {
return array(
'foo' => array(
'name' => t('Foo entry'),
'base' => 'node_content',
'description' => t('For use to store foo entries.'),
));
} // END function foo_node_info
and I'm trying to save that type in the module file using the following code:
// INSERT the stuff
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'Title, blah blah blah',
'url' => 'url here, just pretend',
'body' => '<p>test</p>',
)));
My issue, is that the url, and body fields aren't saving. Any idea what I'm doing wrong?
So, after a ton of digging, it turns out that the way I was entering the custom fields in the node_save was wrong. The node_save needs to look like the following:
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'the title',
'url' => array(
'und' => array(array(
'summary' => '',
'value' => 'url value',
'format' => 2,
))),
'body' => array(
'und' => array(array(
'summary' => '',
'value' => 'the body goes here',
'format' => 2,
))),
)));
Notice that for the custom fields, the array structure has to match what was previously going on with CCK (pretty much exactly). The first key in the array describing the field value is the language for the content.
I've used 'und' here only because that's what I saw going into the database when entering the data through a form.

Categories