I am trying to update form fields based on an uploaded file. I'm using hook_form_alter() to perform these updates, and I have found that I can change the default_value of a field when the webpage is first loaded. However, the same thing does not work when hook_form_alter() is called as a result of a file being uploaded. For instance,
function mymodule_form_alter(&$form, &$form_state, $form_id){
// I can change the default value here
$form['field_myfield']['und'][0]['value']['#default_value'] = "Some Value";
// Check to see if the upload button has been pressed
if (array_key_exists('clicked_button', $form_state))){
$trigger = $form_state['clicked_button']['#value'];
if($trigger == 'Upload'){
// Try to change the default value here. The value is changed in $form,
// but the field is not updated on the website.
$form['field_myfield']['und'][0]['value']['#default_value'] = "New Value";
}
}
}
Do I need to perform some sort of refresh? I briefly looked at AHAH, is this something I need to use? I am new to Drupal and I apologize in advance if there is something fundamentally wrong with my terminology and/or approach.
Any help is greatly appreciated.
Related
I am building my wedding website and want to integrate an RSVP form using Gravity Forms. The issue I am running into is how to set certain guest that have +1's. I would like to show an additional guest entry (First Name, Last Name, Meal Option) when the initial First Name and Last Name has been populated. How would I go about doing this? Any help would be great! Thanks in advance!
Here is how I'd solve this problem:
First, you need to put everything in the DB, the easiest way would be to either do it manually or somehow loop through an array/CSV calling add_option($key, $value) Again, I would recommend a mobile/phone number as they'll be unique so you don't pull the wrong "John Smith". I'll assume you'll keep it basic with $key as the unique identifier and $value as boolean as to whether to show additional info. Interestingly, by default, if not found get_option($key) will return false and therefore not show your additional data, which I would assume you'd want anyway. If you'd rather it return true just pass true as the second argument.
Now for your answer:
Your URL is something like https://somesite.com/rsvp?id=1234.
function allowed_plus_one() {
$id = $_GET["id"];
$allowed = get_option($id);
return $allowed;
}
Then assumedly it'll be something like
if (allowed_plus_one()) {
// show form with plus one
} else {
// show form without
}
EDIT:
Keeping separate incase this has already been viewed.
You should also be checking for the existence of $_GET["id"] and behaving accordingly. eg:
if (isset($_GET["id"] && !empty($_GET["id"]) {
//do logic above
} else {
//here by mistake so don't show any form?
}
I have a custom behavior, specified in AppModel.php, that automatically creates a field based on the selected language. Thus, depending on the chosen language, name_eng -> name or name_fra -> name.
...
$virtualField = sprintf($model->name.'.'.$name.'_%s', Configure::read('Config.language'));
$virtualFields[$name] = $virtualField;
$model->virtualFields = $virtualFields;
...
This part works.
The issue arises when I submit the edit form, get a validation error and the field isn't available when the edit view is displayed with error prompts. I believe this is due to either my behavior not being called in this process or $this->request->data being created using form data?
I figured that I would initialize the values using beforeValidate. However, it's not working out: the field still doesn't exist once I've submitted the form which gives me this error:
In AppModel.php:
function beforeValidate(array $options = array()){
//hard coded for test purposes
$this->data['CertificateType']['name'] = $this->data['CertificateType']['name_'.Configure::read('Config.language')]
return true;
}
In the view (edit.ctp):
echo $this->request->data['CertificateType']['name'];
Essentially, how can I replicate the functionality of my custom behavior and initialize my field after a form has been submitted but doesn't validate?
The needed logic was eventually put in AppController.php. Everything works fine now.
I have what I believe to be a simple problem, however, I am stumped. I am working on a list of custom fields for a custom type in Wordpress for a plugin. I want to have a dynamic amount of custom fields depending on another post being linked (that's probably a complicated way of stating the issue).
It seems to work fine with the input types that I have on the page, however for the input file type, which is read only, everytime that I click to update the values that were stored previously are lost. I understand why this happens, when the page loads after saving a value the has no value. I show the image in a tag. When I add a different image to a different custom field, the original image now sends no data to the php script. Therefore the original value is lost.
Here is the relevant php:
// Attribute Images...
$save_img_values = array();
foreach ($_FILES['paired_att_img']['name'] as $key => $att_img) {
$file_name = $_FILES['paired_att_img']['name'][$key];
$file_tmp = $_FILES['paired_att_img']['tmp_name'][$key];
$upload = wp_upload_bits($file_name, null, file_get_contents($file_tmp));
if (isset($upload['error']) && $upload['error'] != false) {
continue;
} else {
$save_img_values[$key] = $upload['url'];
}
}
update_post_meta($post->ID, 'paired_atts_img', $save_img_values);
Please no comments on the hideous code, It needs refactoring, but I want to get it working first.
So in actual fact I found the answer! Feel free to let me know why this wouldn't be a good idea if it isn't but essentially I just added this in:
$custom_imgs = get_post_meta($post->ID, 'paired_atts_img', false);
if (count($custom_imgs) > 0)
$custom_img = $custom_imgs[0];
else
$custom_img = array();
$save_img_values = $custom_img;
and then the rest as before. So in essence we retrieve the values on the save rather than on the load. Maybe too much time in asp.net stopped me from seeing something so obvious.
I am trying to create a module where the users creates his account and on submit, i get his information and insert them in a second database too.
I mean that he will exist in both databases and in Drupals user table and in user table of the other database.
How can i get his information and insert them to a custom database?
I am totally new to Drupal development.
Thank you in advance for any help or advice.
You will need to implement hook_form_alter() and use the following code:
function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "user_register_form")
{
$form['#submit'][] = "your_custom_submit_callback";
}
}
Then create the custom submit callback to manipulate the submitted values the way you like:
function your_custom_submit_callback($form, &$form_state)
{
// your code goes here...
}
Hope this works... Muhammad.
I'm trying to create a fairly simple form that has a few checkboxes and input fields and a textarea. Nothing is required by itself; however, if 'A' checkbox is checked, then 'A' input field is required (and so on for the couple other checkboxes I have).
I have the above functionality in place, but I'm having a tough time figuring out how to have an error returned if the form is submitted blank (since nothing is required by default).
Does anyone know of an easy-ish solution for this? It seems like it should be so simple...
Thanks
I assume that your using the form_validation class..
You will need to write a callback that does something like this:
function _checking()
{
if (isset($_POST['a_checkbox']))
{
if (empty($_POST['a_text_field']))
{
$this->form_validation->set_message('_checking', 'this should not be empty');
return FALSE;
}
return TRUE;
}
}
I hope this is what you are looking for..
Just check if $_POST-array is empty, except for your submitbutton?