hook_form_alter, add hidden elements such as an array - drupal

How do you attach hidden elements, or values such an array to forms via hook_form_alter ?
I can access different parts of the form, but get error when trying to add a key or value.

Something like this should work:
function sample_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'FORM-ID') {
$form['sample_module_hidden_field'] = array(
'#type' => 'hidden',
'#value' => 'your-value',
);
$form['#submit'][] = 'sample_module_form_submit';
}
}
function sample_module_form_submit($form, &$form_state){
$values = $form_state['values'];
// store the value from the hidden field
if(!empty($values['sample_module_hidden_field'])){
variable_set('sample_module_hidden_field', $values['sample_module_hidden_field']);
}
}
You should change the FORM-ID to the id of the form you would like to alter. Also, of course you should alter the name and value of the field.
This is also a must read: Form API Reference

Related

Change field value and submit node with a custom button

I am attempting to write a module that adds a button to a node type that, when pressed, will change a value of a field in that node and submit the changes. Everything seems to be working, as the button appears correctly and the node submits when it's pressed, but the value of the field remains unchanged. I feel like I'm missing something obvious.
<?php
function iu_buttons_node_view($node, $view_mode, $langcode) {
if ($node->type == 'billing_entry') {
if ($node->field_status['und'][0]['value'] == 'open') {
$form = drupal_get_form('submit_button_form');
$node->content['submit_button'] = $form;
}
}
}
function submit_button_form($form, &$form_submit) {
$form['submit'] = array(
'#type' => 'button',
'#value' => ('Submit'),
'#submit' => array('submit_button_form_submit'),
);
return $form;
}
function submit_button_form_submit($form, &$form_state) {
$node->field_status['und'][0]['value']['#value'] = 'submitted';
}
It's probably worth noting that the field I'm trying to change is a select list. Should I be using a different function than hook_form_submit?
I am assuming you are writing this code in a custom module named iu_buttons and you want to display this button in the node page, not in the node edit form.
In this case, the problem is that you never saved the node in your submit function. Here is a version of your submit function that will save the node.
function submit_button_form_submit($form, &$form_state) {
$node = menu_get_object(); //get the current node
$node->field_status['und'][0]['value']['#value'] = 'submitted';
node_save($node); // save the changed node
}
I think you may be interested in saving only the field, without the need to save the whole node. In this case, you might use the following:
function submit_button_form_submit($form, &$form_state){
$node = new stdClass(); // Create empty object
$node->nid = intval(args(1)); // Include the nid so that Drupal saves the new value to the correct node
$node->field_status['und'][0]['value']['#value'] = 'submitted';
field_attach_update('node', $node); // Save the field
}

Programmatically adding columns in VIEWS

In drupal 7 view, I have created a custom module and add dynamic columns using below method.
function my_module_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'my_view') {
$countries = my_module_vocab_terms('countries');
foreach ($countries as $country){
$view->add_item($view->current_display, 'field', 'views', 'nothing', array(
'label' => $country->name,
'alter' => array('text' => $country->tid),
'element_class' => 'my-field',
'element_default_classes' => 0,
), $country->tid);
}
}
}
}
But the text value repeated in each rows like this.
Please suggest how to pass value for each row.
Thanks
If anyone is still interested how to alter values to such fields.
You can do so by rendering the fields and altering the data in one of the hooks where the results are already fetched eg. the hook_views_post_execute, hook_views_pre_render, hook_views_post_render depending on your needs.
/**
* Implements hook_views_post_execute().
*/
function my_module_views_post_execute(view &$view) {
$view->style_plugin->render_fields($view->result);
$view_fields = &$view->style_plugin->rendered_fields;
foreach ($view_fields as &$field) {
$field['my_field'] = "some value";
}
unset($field);
}

Drupal Admin form AJAX callback not working

I am trying to implement an AJAX callback to change a drop down list options based on the values of another drop down.I have looked into the examples online and the examples in the examples module. I am trying to implement the example on one of the admin pages forms.
To simplify, I tried to achieve the following: Just to change the title of the second dropdown with a random number once the first dropdown has changed. Please note that I am using a field collection field.
function myaction_form_alter(&$form, &$form_state, $form_id) {
$form['field_programme_permission']['und']['0']['field_programme']['und']['#ajax']=array(
'event' => 'change',
'callback' => 'programmes_ajax_callback',
'method' => 'replace',
'wrapper' => 'countries_wrapper'
);
$form['field_programme_permission']['und']['0']['field_countries']['und']['#title']=rand(1,100);
return $form;
}
function programmes_ajax_callback($form, $form_state) {
return $form['field_programme_permission']['und']['0']['field_countries'];
}
It is as if programmes_ajax_callback is not triggered at all. I see this Drupal AJAX please wait message but nothing actually happens. The weird thing, If I submit the form and it doesn't pass validation, I don't even see this Drupal AJAX "please wait" message. I have simplified the code as much as possible to try to pin point the problem, but it didn't work...
Any ideas?
The problem was caused by the fact that countries_wrapper didn't actually exist as I was actually calling it countries-wrapper somewhere else.
Find below the code that I have actually used. This code covers a muti-value (up to 10) field collection at which one field (countries) in the field collection is dependent on the other (programme). Hopefully, it will prove to be useful for someone.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
for($i=0;$i<10;$i++) {
if(($form_id=='user_register_form') || ($form_id=='user_profile_form')) {
if(isset($form_state['values']['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE][0]['tid'])) {
$programme_selected= $form_state['values']['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE][0]['tid'];
} else {
$programme_selected=0;
}
if(isset($form_state['field']['field_programme_permission']['und']['entity'][$i]->field_programme['und'][0]['tid'])){
$programme_selected=$form_state['field']['field_programme_permission']['und']['entity'][$i]->field_programme['und'][0]['tid'];
}
$form['field_programme_permission'][LANGUAGE_NONE][$i]['field_programme'][LANGUAGE_NONE]['#ajax']=array(
'event' => 'change',
'callback' => '_programmes_ajax_callback',
'method' => 'replace',
'wrapper' => 'countries_wrapper'.$i
);
$form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#title']='Countries';
$form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#prefix']='<div id="countries_wrapper'.$i.'">';
$form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#suffix']='</div>';
$form['field_programme_permission'][LANGUAGE_NONE][$i]['field_countries'][LANGUAGE_NONE]['#options']=_countries_ajax_callback($programme_selected);
}
}
return $form;
}
function _programmes_ajax_callback($form, $form_state) {
//we first need to know the triggering element, to know the index of the countries field that we need to affect.
$index= $form_state['triggering_element']['#field_parents'][2];
return $form['field_programme_permission'][LANGUAGE_NONE][$index]['field_countries'];
}
function _countries_ajax_callback($selected) {
$programme_value = $selected;
$options=array();
if(taxonomy_term_load($programme_value)){
$programme_taxonomy=taxonomy_term_load($programme_value);
if(isset($programme_taxonomy->field_countries[LANGUAGE_NONE])) {
$countries=$programme_taxonomy->field_countries[LANGUAGE_NONE];
foreach($countries as $country) {
$country_tid = $country['tid'];
$country_term = taxonomy_term_load($country_tid);
$country_name = $country_term->name;
$options[$country_tid]=$country_name;
}
}
}
return $options;
}

how to add a button to drupal forms?

I want to add a custom button to drupal create form so if the user clicked on it instead of submit button, the workflow state of the created object change to another state(not the default first state)
any suggestion?
To modify the default forms generated by drupal you have to add a form_alter hook to your module. You can do it by defining a function like modulename_form_alter assuming the name of your module is modulename. The drupal system passed the form array and the form_state array which you can use to override the default behavior. In your case, the complete function would look something like this.
function modulename_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'what you want') {
$form['buttons']['another_button'] = array(
'#type' => 'submit',
'#value' => 'Add to another state',
'#submit' => array('modulename_custom_form_submit')
);
}
}
function modulename_custom_form_submit($form, &$form_state) {
if($form_state['values']['another_button'] == 'Add to another state') {
//Do your thing
}
}
After you made the necessary modifications, you can simply submit to the default submit action of the creation form.

Drupal 6 Form Api adding an item to an existing form

I want to add a new item into an existing form. I have the ID of the form and I know I need to use hook form_alter but not sure how to add it.
function modulename_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'form id goes here':
// Need to do something here....
break;
}
}
Because the &$form variable is a reference, whatever you do to it changes the original value. so just add it to $form;
//After, need to do something here:
$form['my_new_field'] = array(
'#type' => 'select',
//etc..
);
//You can also add a new validation here:
$form['#validate'][] = 'my_valiation_callback';
See the drupal api ref for better details:

Resources