How to load post ID using the gform_form_post_get_meta filter? - gravity-forms-plugin

Trying to load post ID (and then some ACF field) of the post, where the form is currently embedded. Using get_the_id() or global $post w/ $post->ID returns NULL.
Loading post ID works correctly when using the other Gravity Forms filters (e.g. gform_admin_pre_render), but I was told using the gform_form_post_get_meta is the better way to do ths. What is the right approach for this?
add_filter( 'gform_form_post_get_meta' , 'my_populate_cpt_as_choices' );
function my_populate_cpt_as_choices( $form ) {
$current_post_id = get_the_id();
$postargs = array(
'post_type' => 'suhlasy',
'post_status' => 'publish',
'posts_per_page' => '-1',
);
$posts = get_posts( $postargs );
$input_id = 1; // this makes sure the checkbox labels and inputs correspond
foreach ( $posts as $post ) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if ( $input_id % 10 == 0 ) {
$input_id++;
}
$post_id = $post->ID;
$id_souhlasu = 1200 + $input_id;
$title = get_the_title($post_id);
$checkbox_text = get_field('checkbox_text', $post_id);
$text_suhlasu = get_field('text_suhlasu', $post_id);
$kategoria = get_field('kategoria_suhlas', $post_id);
// getting other fields for this post to display as values or checkbox labels
$nazev_souhlasu = GF_Fields::create( array(
'type' => 'consent',
'id' => $id_souhlasu, // The Field ID must be unique on the form
'formId' => $form['id'],
'isRequired' => true,
'label' => $title,
'parameterName' => 'my_custom_parameter',
'checkboxLabel' => $checkbox_text,
'description' => '<h2>' . $current_post_id . $post_id . $kategoria . '</h2><br>' . $text_suhlasu,
'pageNumber' => 1, // Ensure this is correct
) );
$form['fields'][] = $nazev_souhlasu;
$input_id++;
}
return $form;
}

Related

How can I filter through my meta query via acf fields?

I have a group of posts that can select a custom field 'Partner Content'. These posts then select their post author. The post author is a custom post type that has a relationship field that selects the 'Partner Company' associated with the post author.
I am now trying to set up a single-partner_company.php template that displays all posts associated with the specific company. My meta query looks like this:
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$args = array(
'numberposts' => -1,
'fields' => 'ids',
'post_type' => 'partner_authors',
'meta_query' => array(
'key' => 'author_company',
'value' => $current_company
)
);
$authors = get_posts( $args );
$post_args = array(
'fields' => 'ids',
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
'key' => 'post_author',
'value' => $authors
)
);
$partner_posts = get_posts( $post_args );
$count = 0;
?>
neither array is working for me. what am I doing wrong?

Gravityview - Show payment status of another form

I currently have Gravityview view which is getting the source from form A. However, I want to show if the current user has made a payment on form B. Obviously this can be fetched from the {payment_status} merge tag of form B. But How can I pull the data of form B onto the Gravity view custom content field of form A?
I've looked at the gform_entry_id_pre_save_lead hook but I think there's a better way. Thanks for your help in advance..
add_filter( 'gform_entry_id_pre_save_lead', 'my_update_entry_on_form_submission', 10, 2 );
function my_update_entry_on_form_submission( $entry_id, $form ) {
$update_entry_id = rgpost( 'my_update_entry_id' );
return $update_entry_id ? $update_entry_id : $entry_id;
}
You can either use the Gravityview Multiple Forms Add-on or you can try adding the following code to your functions.php:
function gf_check_form($atts = array()) {
$details = shortcode_atts( array(
'form_id' => "",
'user' => ""
), $atts );
$form_id = $details['form_id'];
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'all',
array(
'key' => 'created_by',
'operator'=> 'is',
'value' => $details['user']
)
)
);
$entries = GFAPI::get_entries( $form_id, $search_criteria);
foreach($entries as $entry){
$paid .= 'Paid on '.date("F d, Y", strtotime($entry['date_created'])).'<br><br>';
}
return $paid;
}
add_shortcode( 'check_form', 'gf_check_form' );
You can then add the following shortcode to your Gravityview in a custom content field:
[check_form form_id="Add the form ID of form B here" user="{created_by:ID}"]

Drupal 7 | Form managed file upload image preview

I'm can't get to work image preview on Drupal 7 Form managed file.
I have for code like this in template.php:
function testform($form, &$form_state) {
$form = array();
$form['con_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('con_image', ''),
'#progress_indicator' => 'bar',
'#progress_message' => 'Uploading ...',
'#upload_location' => 'public://gallery/',
'#theme' => 'test',
'#upload_validators' => array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('jpg jpeg'),
'file_validate_image_resolution' => array('6000x4000','800x600'),
'file_validate_size' => array(6 * 1024 * 1024),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to site'),
);
return $form;
}
I call testform after checking additional conditions (like userpoints) via code
$arr = drupal_get_form('testform');
print drupal_render($arr);
The form itself is working, I'm able to add image to node (programmatically) but cannot get the preview of image during upload process. I try to use #theme but it seems doesn't work at all.
My theme function looks like this:
function theme_test($variables) {
$element = $variables['element'];
$output = '';
$base = drupal_render_children($element); // renders element as usual
if($element['fid']['#value'] != 0) {
// if image is uploaded show its thumbnail to the output HTML
$output .= '<div class="multifield-thumbnail">';
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
$output .= '</div>';
}
Any ideas?
You need to declare your theme with a hook_theme in your module.
function yourmodule_theme() {
return array(
'test' => array(
'render element' => 'element',
)
);
}

Multistep drupal form with tables

Can anyone tell me how to make a multi-step drupal form which prints a table in one of the steps?-
This is the code I already have, it corresponds to the second step of the form. It throws me this error:
Fatal error: Cannot unset string offsets in C:\wamp\www\academicus\includes\form.inc on line 497
function listarAlumnos($nombre, $apellido) {
if($nombre=='') {
$consulta="SELECT * FROM Pinteres WHERE PinApe1='".$apellido."' or PinApe2='".$apellido;
} else if ($apellido=='') {
$consulta="SELECT * FROM Pinteres WHERE PinNom1='".$nombre."' or PinNom2='".$nombre."'";
} else {
$consulta="SELECT * FROM Pinteres WHERE (PinNom1='".$nombre."' or PinNom2='".$nombre."') AND (PinApe1='".$apellido."' or PinApe2='".$apellido."')";
}
$resultado=consultarContacta($consulta);
$form=array();
$header = array(t('Id'), t('Primer nombre'), t('Segundo nombre'), t('Primer apellido'), t('Segundo apellido'), t('Direccion'), t('Telefono'), t('Celular'), t('Email'));
$rows = array();
while ($rs=odbc_fetch_array($resultado)) {
$row=array();
$id=$rs['PinId'];
$primerNombre=$rs['PinNom1'];
$segundoNombre=$rs['PinNom2'];
$primerApellido=$rs['PinApe1'];
$segundoApellido=$rs['PinApe2'];
$direccion=$rs['PinDir'];
$telefono=$rs['PinTelPri'];
$celular=$rs['PinTelCel'];
$email=$rs['PinEMail'];
$row[] = $id;
$row[] = $primerNombre;
$row[] = $segundoNombre;
$row[] = $primerApellido;
$row[] = $segundoApellido;
$row[] = $direccion;
$row[] = $telefono;
$row[] = $celular;
$row[] = $email;
$rows[] = $row;
};
$form['IdIngresado'] = array (
'#title' => t('Id interesado a importar'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('Ingrese el id del interesado a importar de los listados arriba.')
);
$form['CedulaIngresada'] = array (
'#title' => t('Cedula interesado a importar'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('Ingrese la cedula del interesado a importar.')
);
$form['finalizar'] = array (
'#type' => 'submit',
'#value' => t('Finalizar')
);
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
I could make it work using some code like this:
$form['serial'] = array(
'#type' => 'textfield',
'#title' => t('serial number'),
'#prefix' => '<table><tr><td>',
'#suffix' => '</td>',
);
but I know it's not the proper way of doing it.
I assume you know how to do the multi-step form. For the table, you can use Drupal's table theme function. If the table contains only markup, you can do something like this:
$form['table'] = array ('#value' => theme('table', $headers, $rows));
$headers are the table headers and $rows are your data.
If the table contains actual form elements (such as selects, textfields, etc.), you can wrap them in a form element that calls a theme function, like so:
$form['table'] = array ('#theme' => 'output_table');
$form['table']['element_1'] = array (...);
$form['table']['element_2'] = array (...);
Within 'theme_output_table', generate your $headers and $rows to feed to 'theme_table' by calling 'drupal_render' on the relevant form elements that are children of $form['table'].

Drupal Form:want to show previous form value as default_value on page

My Goal is if user is submitting this form with "Product Name" value as "YYY". On submit page should reload but this time "Product Name" should show previous valye as default as in this case "YYY".
Here is my code...
function addnewproduct_page () {
return drupal_get_form('addnewproduct_form',&$form_state);
}
function addnewproduct_form(&$form_state) {
$form = array();
$formproductname['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
);
if (isset($form_state['values']['productname']))
{
$form['productname']['#default_value'] = $form_state['values']['productname'];
}
//a "submit" button
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Add new Product'),
);
return $form;
}
You can use $form_state['storage'] in your submit handler to hoard values between steps. So add a submit function like so:
function addnewproduct_form_submit ($form, &$form_state) {
// Store values
$form_state['storage']['addnewproduct_productname'] = $form_state['values']['productname'];
// Rebuild the form
$form_state['rebuild'] = TRUE;
}
Then your form builder function would become:
function addnewproduct_form(&$form_state) {
$form = array();
$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
);
if (isset($form_state['storage']['addnewproduct_productname'])) {
$form['productname']['#default_value'] = $form_state['storage']['addnewproduct_productname'];
}
return $form;
}
Just remember that your form will keep being generated as long as your $form_state['storage'] is stuffed. So you will need to adjust your submit handler and unset($form_state['storage']) when are ready to save values to the database etc.
If your form is more like a filter ie. used for displaying rather than storing info, then you can get away with just
function addnewproduct_form_submit ($form, &$form_state) {
// Rebuild the form
$form_state['rebuild'] = TRUE;
}
When the form rebuilds it will have access to $form_state['values'] from the previous iteration.
Drupal will do this by default if you include:
$formproductname['#redirect'] = FALSE;
In your $formproductname array.
I prefer to save all values in one time when we are speaking about complex forms :
foreach ($form_state['values'] as $form_state_key => $form_state_value) {
$form_state['storage'][$form_state_key] = $form_state['values'][$form_state_value];
}
simply adding rebuilt = true will do the job:
$form_state['rebuild'] = TRUE;
version: Drupal 7
For anyone looking for an answer here while using webform (which I just struggled through), here's what I ended up doing:
//in hook_form_alter
$form['#submit'][] = custom_booking_form_submit;
function custom_booking_form_submit($form, &$form_state) {
// drupal_set_message("in form submit");
// dpm($form_state, 'form_state');
foreach ($form_state['values']['submitted_tree'] as $form_state_key => $form_state_value) {
$form_state['storage'][$form_state_key] = $form_state_value;
}
}
Note: added the ' as it was lost
In my case I had a couple of drop-downs. Submitting the form posted back to the same page, where I could filter a view and I wanted to show the previously selected options. On submitting the form I built a query string in the submit hook:
function myform_submit($form, &$form_state) {
$CourseCat = $form_state['values']['coursecat'];
drupal_goto("courses" , array('query' =>
array('cat'=>$CourseCat))
);
}
In the form build hook, all I did was get the current query string and used those as default values, like so:
function myform_form($form, &$form_state) {
$Params = drupal_get_query_parameters ();
$CatTree = taxonomy_get_tree(taxonomy_vocabulary_machine_name_load ('category')->vid);
$Options = array ();
$Options ['all'] = 'Select Category';
foreach ($CatTree as $term) {
$Options [$term->tid] = $term->name;
}
$form['cat'] = array(
'#type' => 'select',
'#default_value' => $Params['cat'],
'#options' => $Options
);
$form['submit'] = array(
'#type' => 'submit',
'#default_value' => 'all',
'#value' => t('Filter'),
);
return $form;
}
I usually solve this by putting the submitted value in the $_SESSION variable in the submit hook. Then the next time the form is loaded, I check the $_SESSION variable for the appropriate key, and put the value into the #default_value slot if there's anything present.
Not sure if this would work for you, but you could try adding the #default_value key to the form array
$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
'#default_value' => variable_get('productname', ''),
);
That way if the variable is set it will grab whatever it is, but if not you can use a default value.