Gravity Form Hidden Field - wordpress

I would like to add a hidden input field to a gravity form like:
<input type="hidden" id="Campaign_ID" name="Campaign_ID" value="ABCD">
where "ABCD" is a value that can be changed in the Gravity form interface.
Any tips / ideas
Thanks!

Have you tried adding a Hidden Field via the Gravity Form interface? Hidden is one of the field types you can add via the Standard Fields box.
I just made a field, and it generated this:
<input name="input_10" id="input_2_10" type="hidden" class="gform_hidden" value="ABCD">
Unfortunately, I do not see a way to change the name, id, or class of the field, but you can set a label (which appears to do nothing) and value, and set the value to be dynamically generated.
Here is an article about the Hidden field in Gravity Form's Documentation:
https://www.gravityhelp.com/documentation/article/hidden/

In gravity form, you can add the hidden field using the form design page from Standard Fields. There is no need to add html code for that, gravity form will add the field code to your form.
If you want to assign the value of the hidden field to a static one, you can enter the value from in the Default Value of the hidden field from advanced tab. You can also utilize the Merge Tags to make the hidden filed value dynamic. For example, you could add {ip} merge tags if you want to make the value of the hidden field to be the user's IP address.
If you need to change the value based on user input from other fields (or anything else), then you can add PHP code that controls that.
let's assume the form ID is: 1, and the hidden filed id is 14, and you would like to change the value of the hidden filed based on the value of filed number 5.
The following code should be added to the functions.php file of your active theme.
add_action( 'gform_pre_submission_1', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
if ($_POST['input_5'] == 'ABCD'){
$_POST['input_14'] = 'Value 1';
}
else{
$_POST['input_14'] = 'Value 2';
}
}
You can read more about the gform_pre_submission from this page.
https://docs.gravityforms.com/gform_pre_submission/

Related

How do I set the value of a WordPress custom field from another custom field?

The user is asked to provide a custom field value whilst filling in a form: an event_end_date to show in an event listing when an event has finished.
I want to be able to take that value and also apply it to another custom field to cause the theme to exclude it from being listed, which it does based on the value of an Expiry Date custom field (event_expire_date).
Usefully, the form (WP User Frontend Pro) provides an Action Hook option that I have added to the form before the submit button. As I understand it, this calls a named function (I've called it 'set_expire_date').
I've added the following code to the theme's functions.php:
function set_expire_date () {
$event_expire_date = $event_end_date;
}
After submission I expected that the event_end_date in the custom field would be populated but it is not.
To test, I have tried setting event_expire_date to an absolute value instead (based on the appropriate date / time format) but that is not populating the custom field event_end_date either.

Add label-element to managed_file field with Form API

I want to style my managed_file-Field in a Drupal 7 Form-API custom form and therefore, insert a label element that will replace the "Browse for file".
So I want exactly this:
<input type="file" name="files[bild_0]">
<input type="submit" id="edit-bild-0-upload-button" value="Upload">
<label for="files[bild_0]">Choose Picture</label>
However, the Form API makes it very difficult to insert the label directly next to the input. "#prefix" and "#suffix" are not working. "#field_prefix" and "#field_suffix" inserted the html in a wrapper...any ideas?
Thank you very much in advance!
So, i finally figured out an alternative solution. I used the
'#field_suffix'
property on the Form API field to add the label element. Besides of that, I had to overwrite the initial id of the field via
'#id' => 'upload-selector-' . $index_of_picture field
Since the id of the actual input-field is now different from the div that contains the field, I could use the label of the field to trigger the file-dialog. I styled the label element to my needs and hid the initial input.

How do I change the add to cart text for specific variations in WooCommerce?

In my WooCommerce store I have simple products and variable products, both simple products and variations have a meta value of 'pre_order' which is yes/no.
If this option is set on the backend I want to have the add to cart button say 'Pre-Order' as opposed to 'Add To Cart'.
For simple products I have had no issue changing this using filters or editing file templates, variations seem much trickier though...
When variation options are selected then the price and sku updates, during this process I also want to check the meta value mentioned above and if that variation has the meta value pre order of 'yes' then I would also like to change the button text but I cannot find a way to do so!
WooCommerce provide a hidden input field named variation_id. As soon as variation changes, updated variation id is saved in this hidden input field. So first you have to detect using jQuery when the value of this input field is updated, when detected fire your own ajax request and check whether this variation was assigned for pre order or not if yes now you can change the text of add to cart after the response of ajax is success.
WooCommerce stores the variation details (like price, dimensions, weight etc) in a data attribute named data-product_variations in .variations_form element. This data is used to show the price and other details when user selects a particular variation. You can add your meta value in this data attribute using a filter woocommerce_available_variation something like:
add_filter('woocommerce_available_variation', 'vna_add_variation_meta');
function vna_add_variation_meta($dataattr, $product, $variation) {
$dataattr['pre_order'] = get_post_meta($variation->get_id(), 'pre_order', true);
return $dataattr;
}
You can find more details about woocommerce_available_variation in class-wc-product-variable.php file.
Once you have got the meta value in DOM element, you can use following event to know when user has selected a variation.
$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
// Fired when the user selects all the required dropdowns / attributes
// and a final variation is selected / shown
// Here you will need to get the value of 'pre_order' from the variation object
// something like: variation.pre_order
// and take appropriate action
} );
Please note: code is untested so there could be some typos ;)

Drupal 7: how to filter view content (with entity reference field) based on current page content

in my drupal 7 I have 2 content-types like these:
ContentA
ContentB (with a field Entity Reference to ContentA)
In the front-end detail page of ContentA, I would love to show a block/view with a list of ContentB whose Entity Reference field is set to the current ContentA.
I made a view of type Block and added it correctly to the page, but I cannot filter ContentB based on the current ContentA.
Could you help me?
Thanks
You should add a contextual filter for the value you will use for filtering to the block View of ContentB.
Then in your contextual filter in the "When the filter value is NOT in the URL" area select "Provide default value" and Type "PHP code" (You should have enable php filter for this). In your php code area you should have the following code
$node=menu_get_object();
return $node->field_your_machine_field_name['und'][0]['target_id']; // this is the field you will use for fitlering
Hope it helps
UPDATE
The above code will work if you need to show in your block similar results with the same selection (for example similar results of ContentB with the same selection in the referencing field of ContentA ).I will not delete because you might need it also in your project.I misunderstood. Actually the solution is simpler. You should add the contextual filter to the field and in "When the filter value is NOT in the URL" area select "Provide default value" and "Provide id from url"

Entity reference field and dependent dropdown

I have a content type with an entity reference field referencing to a custom entity. I need to use a select box because an autocomplete widget is not suitable in my case. However, I cannot load all the entities at once as selectable values because they are too many (72000+ the form won't even load). So I default the entity reference select box to a limited number of values using a views filter and then hide it by default. Then I use an ajax dependent dropdown to show and populate the entity reference select box with filtered down values (I'm using a module that implements hook_form_alter).
My problem is that the form won't validate because now I can select entity reference values which are not the default ones in the select box. So I guess I should control in some way the validation rules of the entity reference field. Is there an easy way to do this? Which hook should I use?
Set the entity reference field to autocomplete and take it out of the process entirely in your form alter with $form['field_entity_ref']['#access'] = FALSE. This should fix the validation problem. (of course, "field_entity_ref" is what I'm calling your actual reference field.
Add your own validation to the form, if that is still necessary.
Finally, implement hook_node_presave() to manually put the value of your custom ajax drop down box.
So if your custom ajax select box was named my_custom_ref, then it would look something like this:
function mymodule_node_presave($node) {
if (isset($node->my_custom_ref)) {
$node->field_entity_ref[$node->language][0]['target_id'] = $node->my_custom_ref;
}
}

Resources