gravity forms to update previously entered entry - wordpress

following this article:
http://techslides.com/editing-gravity-forms-entries-on-the-front-end
trying to get gravity forms to update a submission rather than create a new one.
problem is that in the
/* https://www.gravityhelp.com/documentation/article/gform_pre_submission */
add_action("gform_pre_submission", "pre_submission_handler");
function pre_submission_handler($form){
if ( strpos($_SERVER['REQUEST_URI'], 'application-edit', 0) !== false ) {
//Get original entry id
parse_str($_SERVER['QUERY_STRING']); //will be stored in $entry
//get the actual entry we want to edit
$editentry = GFAPI::get_entry($entry);
//make changes to it from new values in $_POST, this shows only the first field update
$editentry[1]=$_POST['input_1'];
//update it
$updateit = GFAPI::update_entry($editentry);
header("Location: http://yourdomain.com/apply/application-thank-you/");
//dont process and create new entry
die();
}
}
section of the code, the header redirect is not functioning. Any suggestions?

Related

I am trying to set default value in Billing_city in woocommerce but on checkout it says City field is required

As I am serving only in one city, so that one City should be visible but not editable. I am using this code
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_checkout_fields($fields) {
$fields['billing'] = my_woocommerce_billing_fields($fields['billing']);
return $fields;
}
function my_woocommerce_billing_fields($fields) {
// Note: Default value is only used if the user account does
// not have any value for the meta field yet. (Empty value is
// also a value.) Ensure that the value is set correctly.
$fields['billing_city']['default'] = 'Karachi';
$fields['billing_postcode']['default'] = '75500';
// Disable the field in the form.
// (Note: Does not prevent different values from being posted.)
$fields['billing_city']['custom_attributes']['readonly'] = TRUE;
$fields['billing_city']['custom_attributes']['disabled'] = TRUE;
$fields['billing_postcode']['custom_attributes']['readonly'] = TRUE;
$fields['billing_postcode']['custom_attributes']['disabled'] = TRUE;
return $fields;
}
Disabled fields will not be submited, even if they have a value.
What you can do is connect to woocommerce_checkout_posted_data filter and check if the field doesn't exist, add the field with the value you wanted.
// manipulate the posted data
add_filter('woocommerce_checkout_posted_data', 'bt_manipulate_checkout_posted_data');
function bt_manipulate_checkout_posted_data ($data) {
if (empty($data['billing_city'])) $data['billing_city'] = 'Karachi';
return $data;
}
Add as many fields as needed

How can I move this custom code out of drupal core user module?

I am in the process of trying to get our custom code out of core.
In this function I capture the old session ID to update a project the user may have been working on while not logged in.
function user_authenticate_finalize(&$edit) {
global $user;
watchdog('user', 'Session opened for %name.', array('%name' => $user->name));
// Update the user table timestamp noting user has logged in.
// This is also used to invalidate one-time login links.
$user->login = time();
db_query("UPDATE {users} SET login = %d WHERE uid = %d", $user->login, $user->uid);
$old_session_id = session_id(); //THIS LINE NEEDS TO BE MOVED
// Regenerate the session ID to prevent against session fixation attacks.
sess_regenerate();
tf_user_new_session_id($user, $old_session_id); //THIS LINE NEEDS TO BE MOVED
user_module_invoke('login', $edit, $user);
}
The lines right before and right after sess_regenerate();
As far as I can tell, you would have to write your own login process to accomplish this cleanly in D6. I'd probably do a hook_form_alter() and replace the submit handler on the login form with my own.
function tf_user_user($op, &$edit, &$account, $category = NULL)
{
$current_session = session_id();
if ('login' == $op)
{
setcookie('session_id_anonymous', $current_session, time() + 86400);
}
if ('load' == $op)
{
if (isset($_COOKIE['session_id_anonymous']) && $_COOKIE['session_id_anonymous'] != $current_session)
{
tf_user_new_session_id($account->uid, $_COOKIE['session_id_anonymous'], $current_session);
setcookie('session_id_anonymous', $current_session, time() - 3600);
}
}
//more code
}

How to enable/disable revision in drupal 7

What I am trying to do is to enable/disable revision according to the selected taxonomy term in the content type that I have created i.e. when a user add the content the user can select the taxonomy term field(may be a select field) according to the selected option I want to enable/disable the revision. How can I do this?
Turn off the create new revision setting for the content type.
Then in hook_form_alter add a new submission handler before the main one:
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'CONTENT_TYPE_node_form':
//dpm($form);
$form['actions']['submit']['#submit'][] = 'revision_control_node_form_submit';
$form['actions']['submit']['#submit'] = array_reverse($form['actions']['submit']['#submit']); // reverse array to put our submit handler first
break;
}
}
Then in the new submit handler check if the taxonomy term has the correct value to save a new revision. I've not tried this next bit but according to this page putting
$node->revision = 1;
before node save will create a new revision.
node_save is called in node_form_submit and the node object is built in node_form_submit_build_node.
Looking at the other attributes like vid that belong to $form_state I would say an good educated guess would be to put $form_state->revision = 1; and see if that comes out as a property of the node after node_form_submit_build_node.
So you final new submit handler will look something like:
function revision_control_node_form_submit($form, &$form_state) {
if($form_state['values']['your_taxonomy_field'] == 'your_value') {
$form_state->revision = 1;
}
}
Now I've not actually tried any of this but even if it doesn't work I'm sure you will be on the right track... Good luck!

Modifying a field collection programmatically missing hostEntity fields

I am trying to modify a field collection in a node that already exists so I can change an image on the first element in an array of 3. The problem is, the hostEntity info is not set when I do a entity_load or entity_load_single so when I do a:
$field_collection_item->save(true); // with or without the true
// OR
$fc_wrapper->save(true); // with or without the true
I get the following error:
Exception: Unable to save a field collection item without a valid reference to a host entity. in FieldCollectionItemEntity->save()
When i print_r the field collection entity the hostEntity:protected fields are indeed empty. My field collection is setup as follows:
field_home_experts
Expert Image <--- Want to change this data only and keep the rest below
field_expert_image
Image
Expert Name
field_expert_name
Text
Expert Title
field_expert_title
Text
Here is the code I am trying to use to modify the existing nodes field collection:
$node = getNode(1352); // Get the node I want to modify
// There can be up to 3 experts, and I want to modify the image of the first expert
$updateItem = $node->field_home_experts[LANGUAGE_NONE][0];
if ($updateItem) { // Updating
// Grab the field collection that currently exists in the 0 spot
$fc_item = reset(entity_load('field_collection_item', array($updateItem)));
// Wrap the field collection entity in the field API wrapper
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);
// Set the new image in place of the current
$fc_wrapper->field_expert_image->set((array)file_load(4316));
// Save the field collection
$fc_wrapper->save(true);
// Save the node with the new field collection (not sure this is needed)
node_save($node);
}
Any help would be greatly appreciated, I am still quite new to Drupal as a whole (end-user or developer)
Alright so I think I have figured this out, I wrote up a function that will set a field collection values:
// $node: (obj) node object returned from node_load()
// $collection: (string) can be found in drupal admin interface:
// structure > field collections > field name
// $fields: (array) see usage below
// $index: (int) the index to the element you wish to edit
function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
if ($node && $collection && !empty($fields)) {
// Get the field collection ID
$eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];
// Load the field collection with the ID from above
$entity = entity_load_single('field_collection_item', array($eid));
// Wrap the loaded field collection which makes setting/getting much easier
$node_wrapper = entity_metadata_wrapper('field_collection_item', $entity);
// Loop through our fields and set the values
foreach ($fields as $field => $data) {
$node_wrapper->{$field}->set($data);
}
// Once we have added all the values we wish to change then we need to
// save. This will modify the node and does not require node_save() so
// at this point be sure it is all correct as this will save directly
// to a published node
$node_wrapper->save(true);
}
}
USAGE:
// id of the node you wish to modify
$node = node_load(123);
// Call our function with the node to modify, the field collection machine name
// and an array setup as collection_field_name => value_you_want_to_set
// collection_field_name can be found in the admin interface:
// structure > field collections > manage fields
updateFieldCollection(
$node,
'field_home_experts',
array (
'field_expert_image' => (array)file_load(582), // Loads up an existing image
'field_expert_name' => 'Some Guy',
'field_expert_title' => 'Some Title',
)
);
Hope this helps someone else as I spent a whole day trying to get this to work (hopefully I won't be a noob forever in Drupal7). There may be an issue getting formatted text to set() properly but I am not sure what that is at this time, so just keep that in mind (if you have a field that has a format of filtered_html for example, not sure that will set correctly without doing something else).
Good luck!
Jake
I was still getting the error, mentioned in the question, after using the above function.
This is what worked for me:
function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
$eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];
$fc_item = entity_load('field_collection_item', array($eid));
foreach ($fields as $field => $data) {
$fc_item[$eid]->{$field}[LANGUAGE_NONE][0]['value'] = $data;
}
$fc_item[$eid]->save(TRUE);
}
I hope this helps someone as it took me quite some time to get this working.

wordpress contact form 7 unique id

i am currently using contact form 7 for wordpress. i would like a unique id for every form filled in and sent.
i have had a look around the internet but cant find anything, although i did find the following:
http://contactform7.com/special-mail-tags/
would there be any easy way to make my own function to do something similar to the above tags? i would need it to be a function to go into my themes function file, so that plugin updates wont affect it.
Cheers Dan
[_serial_number] field is an auto-incremented form submission ID. You just have to have Flamingo plugin installed as well. See http://contactform7.com/special-mail-tags/
(It's the same link as in the question, I guess the field wasn't there when the question was asked).
To generate ID for the Contactform7 you need to hook the 'wpcf7_posted_data'.
However, to generate incremental ID you need to save the forms in database so you can retrieve which ID should be next on next Form submit. For this you will need CFDB plugin (https://cfdbplugin.com/).
If you dont want to put the code inside theme functions.php file, you can use this plugin instead: https://wordpress.org/plugins/add-actions-and-filters/
Example code:
function pk_generate_ID($formName, $fieldName) {
//Retrieve highest ID from all records for given form stored by CFDB, increment by 1 and return.
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$start = '001';
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName>=$start&&$fieldName<999999999999"; //is_numeric() is not permitted by default for CFDB filter
$atts['limit'] = '1';
$atts['orderby'] = "$fieldName DESC";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = $start;
while ($row = $exp->nextRow()) {
$row2 = $row[$fieldName];
$row2 += 1;
$found = max($found,$row2);
}
return $found;
}
function pk_modify_data( $posted_data){
$formName = 'Form1'; // change this to your form's name
$fieldName = 'ID-1'; // change this to your field ID name
//Get title of the form from private property.
$cf_sub = (array) WPCF7_Submission::get_instance();
$cf = (array) $cf_sub["\0WPCF7_Submission\0contact_form"];
$title = (string) $cf["\0WPCF7_ContactForm\0title"];
if ( $posted_data && $title == $formName) ) { //formName match
$posted_data[$fieldName] = pk_generate_ID($formName, $fieldName);
}
return $posted_data;
}
add_filter('wpcf7_posted_data', 'pk_modify_data');

Resources