I'm developing an prestashop module and it's just a beginning.
The problem is in the configuration page of my module.
I append some text boxes and the label and description are visible, but the textbox is not visible.
I have looked many other examples but haven't found yet any differences and what is wrong.
Mystery is that if I change the type to date or file, it's visible.
// This function called in getContent() of main module php file.
public function displayForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->trans('*'),
),
// Here is the textbox.
'input' => array(
array(
'type' => 'text',
'label' => $this->trans('Link:'),
'name' => 'LINK_PRODUCT',
'desc' => $this->trans('Please input the link...'),
'lang' => true,
'required' => true
)
),
'submit' => array(
'title' => $this->trans('Save'),
'class' => 'btn btn-default pull-right'
)
)
);
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->title = $this->displayName;
$helper->show_toolbar = false;
$helper->submit_action = 'submit';
$helper->fields_value['LINK_PRODUCT'] = Configuration::get('LINK_PRODUCT');
return $helper->generateForm(array($fields_form));
}
I would like to make the textbox visible, how should I go about doing this?
You want to have multilingual so you need to define which language is a default. Add this code to your helper definition
$helper->default_form_language = $this->context->language->id;
and replace
$helper->fields_value['LINK_PRODUCT'] = Configuration::get('LINK_PRODUCT');
with
$helper->tpl_vars = array(
'fields_value' => array('LINK_PRODUCT' => Configuration::get('LINK_PRODUCT')),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
to define all available languages and values of your LINK_PRODUCT variable.
And also, don't forget that you work with multilingual field and you need to have an array with values for your variable. So during extracting and saving you need to treat them as values for all languages. For example, if you have three languages available you need to get three values. The best way to get them is
$values = [];
foreach ($this->context->controller->getLanguages() as $language) {
$values[$language['id_lang']] = Configuration::get('LINK_PRODUCT', $language['id_lang']);
}
$helper->tpl_vars = array(
'fields_value' => array('LINK_PRODUCT' => $values),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
and when you will save it use similar combination but with updateValue
Related
I'm having troubles with a FORM in Drupal 8, the idea is:
Chose a radio value from radio-estaciones to recharge radio-contaminantes, but, after the callback, the result I see is empty, but in replace command I use, I see it correcly
This is the code of buildForm
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state){
$arrRadioContaminantes = array();
$arrRadioContaminantes["0"] = t("ALL");
//My function to access DB
$result = \Drupal\map_data\Controller\Contaminante::list();
foreach ($result as $it){
$arrRadioContaminantes[$it["id_contaminante"]] = t($it["description"]);
}
$form['radio_contaminantes'] = array(
'#type' => 'radios',
'#options' => $arrRadioContaminantes,
'#ajax' => [
'callback' => '::seleccionContaminantes',
'disable-refocus' => true,
'event' => 'change',
'wrapper' => 'radios-estaciones',
'method' => 'replace'
]
);
$form['radio_estaciones'] = array(
'#type' => 'radios',
'#id' => 'radios-estaciones',
'#options' => ['-1'=>t("SELECT CONTAMINANTE FIRST")]
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#button_type' => 'primary',
);
$form_state->setCached(false);
$form_state->setRebuild(true);
return $form;
}
This is callback function:
public function seleccionContaminantes(array &$form, FormStateInterface $form_state){
if($form_state->getValue("radio_contaminantes") != 0){
$form['radio_estaciones']["0"] = t("All");
}
//My Database function
$resultado = \Drupal\montar_mapa\Controller\Estacion::list($form_state->getValue("radio_contaminantes"));
foreach ($resultado as $it){
$form['radio_estaciones'][$it["id"]] = t($it["name"]);
}
$ajaxResp = new AjaxResponse();
$ajaxResp->addCommand(new \Drupal\Core\Ajax\ReplaceCommand('radios-estaciones', \Drupal::service('renderer')->render($form['radio_estaciones'])));
return $ajaxResp;
}
I try returning $form['radio_estaciones'] with same results, and try DataCommand pasing new values, same result... I need help
After change type to 'select' works fine.
Drupal have this bug active
https://www.drupal.org/project/drupal/issues/2758631
Same situation with checkboxes
I need some data added default in the editor.
Like I want a template to get loaded in editor when I click on edit template option..
Can anyone suggest me some tip??
Here you can see how it can be done
$dataa= $this->getTemplate1();
$fieldset->addField('content', 'editor', array(
'name' => 'content',
'label' => Mage::helper('abandonedcart')->__('Content'),
'title' => Mage::helper('abandonedcart')->__('Content'),
'style' => 'width:700px; height:500px;',
'wysiwyg' => true,
'required' => true,
'state' => 'html',
'config' => $wysiwygConfig,
'value'=> $dataa,
));
if (Mage::getSingleton('adminhtml/session')->getAbandonedcartData()) {
$form->addValues(Mage::getSingleton('adminhtml/session')->getAbandonedcartData());
Mage::getSingleton('adminhtml/session')->setAbandonedcartData(null);
} elseif (Mage::registry('abandonedcart_data')) {
$form->addValues(Mage::registry('abandonedcart_data')->getData());
}
return parent::_prepareForm();
}
and calling a function to have data
public function getTemplate1() {
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('abandonedcart_abandonedcart_group_email_template');
$emailTemplate['template_text'];;
$template_id = Mage::getStoreConfig('abandonedcart/abandonedcart_group/email_template');
$emailTemplate = Mage::getModel('core/email_template')->loadDefault($template_id);
return $processedTemplate = $emailTemplate->getProcessedTemplate();
}
I'm working on a module where I need to save the records based on the scope of website and store. For that I need to have the select box as its in the system->configuration.
How can I get that select box in my form and save the website/store value in database so that it can be displayed in the specific store/website? Any suggestions on it?
Now I'm trying it breaking into two fields -> website and store. Now, how can I change the options in store based on the website selected?
Finally, I'd written my own code for this.
$scope = array('default' => 'default');
foreach (Mage::app()->getWebsites() as $website) {
$scope['website_' . $website->getCode()] = $website->getName();
foreach ($website->getGroups() as $group) {
$stores = array();
foreach ($group->getStores() as $store) {
$stores[] = array(
'label' => $store->getName(),
'value' => 'store_' . $store->getCode()
);
}
$scope[] = array(
'label' => $website->getName(),
'value' => $stores
);
}
}
$fieldset->addField('website', 'select', array(
'label' => Mage::helper('designer')->__('Website'),
'name' => 'website',
'values' => $scope
));
Thanks to this post by Marius
I have a content type (budget), it is a custom content type
function budget_node_info() {
return array(
'budget' => array(
'base' => 'budget',
'name' => t('Budget'),
'description' => t('Represents individual budget.'),
'title_label' => t('Budget Name'),
'locked' => TRUE
)
);
}
also I have the form function
function budget_form($node, $form_state) {
drupal_add_library('system', 'ui.tabs');
drupal_add_library('budget', 'highcharts');
drupal_add_js(drupal_get_path('module', 'budget') . '/js/budget_base.js');
drupal_add_js(drupal_get_path('module', 'budget') . '/js/' . $node->type . '.js');
$form = node_content_form($node, $form_state);
$config = variable_get($node->type);
I want to show the edit form on the node view page (I want to show form from node/[nid]/edit to node/[nid])?
I tried next:
function budget_node_view($node, $view_mode, $langcode){
$node->content['edit-form'] = array(
'#markup' => render(drupal_get_form('budget_form', $node)),
'#weight' => 10,
);
}
also I tried
function budget_node_view($node, $view_mode, $langcode){
$node->content['edit-form'] = array(
'#markup' => render(budget_form($node, array())),
'#weight' => 10,
);
}
2nd version show form but without any js or css loaded.
What am I doing wrong?
I used function node_page_edit($node) and seems like it works
function wp_budget_node_view($node, $view_mode, $langcode){
module_load_include('inc', 'node', 'node.pages');
$node->content['edit-form'] = array(
'#markup' => render(node_page_edit($node)),
'#weight' => 10,
);
}
also for new (empty) form you should use node_add function function node_add($type
What I want to do is: I want to render a form on a page with a view. This view has a list of 'notes' (=CT Note). When you fill in the form, the note is stored, the form is cleared, and the new note is added to the list. All without page refreshes.
I create a module new_note, and addes this function:
function new_note_form($nodeid = NULL) {
ctools_include('ajax');
// drupal_add_js(drupal_get_path('module', 'custom_forms') . '/js/note_form.js');
//dpm($nodeid);
module_load_include('inc', 'node', 'node.pages');
$form = node_add('note');
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['und']['#validated'] = 'TRUE';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#executes_submit_callback' => FALSE,
'#ajax' => array(
'callback' => 'ajax_note',
'wrapper' => 'status',
),
);
$output= drupal_render($form);
dpm($form);
print $output;
}
function ajax_note(&$form, &$form_state) {
return 'test';
}
I use this function in a display suite block field, which is rendered above the note list. So far so good.
The only problem is, that when I submit the form, the ajax is not called, and the normal submit is done.
Can anyone help me out
# Edit.
After what clive suggested I changed the code, and got the ajax working.
function new_notes_form($nodeid = NULL) {
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'note',
'language' => LANGUAGE_NONE,
);
$form_state = array();
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
$form = drupal_build_form('note_node_form', $form_state);
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'button',
'#value' => 'Submit',
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'ajax_note_replace',
'wrapper' => 'status',
),
);
return $form;
}
function ajax_note_replace(&$form, &$form_state) {
dpm("test");
dpm($form);
$output = '<h1>' . t('Hello World') . '</h1>';
// $node = node_load('6');
// $output .= drupal_render(node_view($node, $style = 'teaser', $options = array()));
ctools_include('ajax');
$commands = array();
$commands[] = ajax_command_prepend(".view-content", $output);
print ajax_render($commands); // this function exits.
exit;
}
#Clive, can you help me out with the rest ? I want to save the node on callback (if valid?). In the ajax callback my node-id is null because it is not stored yet? how can I validate and save the node, otherwise set the not valid form items to red as normal.
It's likely to be because you're sidestepping Drupal's proper methods for creating forms so the AJAX preprocessing won't be performed. If you have a look at drupal_get_form() (the function used pretty much exclusively to prepare a form in Drupal) you'll see it in turn calls drupal_build_form() which is what you need to do:
function new_note_form($form, &$form_state, $nodeid = NULL) {
$form_state['build_info']['args'] = array('note');
$form = drupal_build_form('node_add', $form_state);
$form['submit'] = array(
'#type' => 'button',
'#value' => 'Submit',
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'advanced_form_callback',
'wrapper' => 'status',
),
);
return $form;
}
echo render(drupal_get_form('new_note_form'));
I've changed the element from type submit to button because I find it works much better when you want to do some ajax processing, removed #executes_submit_callback, and added #limit_validation_errors which will stop the form from otherwise validating (I think that's what you were trying to do by setting #validated to TRUE but I might be wrong).
Hope that helps, it's untested but should give you a good place to start.