Drupal 7: Webform: Set hidden field to some value on radio button selection - drupal

I have a Webform which has a Selection List of radio buttons on it.
I would like to set the value of a hidden text field based on radio button selection.
So if user selects 1st option i would like the text field to have "First" as text and like that.
can this be done? I want to eventually use this text field later on for other processing.

I agree with Ben...you should use the value of the select fields themselves using the syntax for the select field Options, ie "safe_key|Some readable option" (without quotes)
So your select field options would have something like:
first|This is some pizza
second|Just the cheese
If you like, since you want the value "First", you can even capitalize the keys, ie:
First|This is some pizza
Second|Just the cheese
However, if you insist on having the select field change the hidden field's value and you want to do this purely through Drupal 7 front-end, then I suggest the following:
Install the following module:
Code per Node - allows you to add custom CSS/JS to content types or individual nodes
Update your content type settings (ie. by content types or individual nodes)
Then, write some jQuery or JS to do what you want on the node that is housing your webform...ie:
Some Javascript::jQuery for what you want:
jQuery( "#my-select-field" )
.change(function () {
var str = jQuery(this).text();
jQuery( "my-hidden-field" ).val( str );
})
.change();
The ".change()" call at the end is to trigger a change event so that your hidden field is always populated with the appropriate data...
Be sure to put your code in a "jQuery( document ).ready(function() { ... }" call so that the bind is made at the right moment and so your page has finished loading.
*Note: In Drupal 7, the core jQuery Library is referenced by "jQuery", not the "$" alias

You can create a module that implements a submit function. So if your module is "specialFormProcessing" you would create the function:
function specialFormProcessing_mySubmitFunction($form, &$form_state) {
}
Within that function, you would set the value of the hidden field from the SELECT box.
You should also add your function to the array of functions that will process the form. There will be yours and the form's original submit function (depending upon what the form is), and maybe others that other modules have implemented. You do this by creating a hook_form_alter() function in your module:
function specialFormProcessing_form_submit(&$form, &$form_state, $form_id) {
}
You use the $form_id to make sure you are working on the correct form, then do something like:
$form['#submit'][]='specialFormProcessing_mySubmitFunction';
I found the book "Pro Drupal Development" by VanDyk very helpful when I was learning Drupal and manipulating Forms. I was using Drupal 6, but he has a newer edition for Drupal 7. There are criticisms that it doesn't deal with some of the new Drupal 7 features, but its treatment of Forms should be good.

Related

algolia wordpress autocomplete

I'm trying to tweak the WordPress plugin https://github.com/algolia/algoliasearch-wordpress to suit our needs. What we want to be able to do is have a search result that will load the data up in the same page.
I have my WordPress posts and its data being indexed successfully. I have added a search box to the page, and autocomplete is being fired.
Out of the box, the WordPress plugin template wraps the result in an anchor tag and assigns it the URL of the found result. When clicked, this navigates you to that post. However, what I want to do is intercept that click and load the result in the same page without navigating away.
I have removed the href from the anchor. I have also edited the supplied autocomplete.php template where the call to autocomplete:selected occurs. In there I have removed the call to navigate away by removing window.location.href.
Now I have two main issues.
1 - When the user clicks the search result I would like the input to be populate with the title of the item they clicked on. I added this in the autocomplete:selected callback by adding $searchInput[0].value = suggestion.post_title. Which seems to change the value of the input correctly, but as soon as I click away from the input, it is re-set back to the original typed value. So if I type 'may' and click the result 'mayonnaise', the result data can be accessed but the input returns back to 'may'. My function looks this:
/* Instantiate autocomplete.js */
var autocomplete = algoliaAutocomplete($searchInput[0], config, sources)
.on('autocomplete:selected', function (e, suggestion) {
console.log(suggestion);
autocomplete.autocomplete.close();
});
2 - It seems that the autocomplete dropdown does not hide when the user clicks away. To resolve this i've had to use what I think is a bit of a nasty hack with jQuery. I was wondering if this is really required? My code just below the autocomplete:selected call looks like this:
jQuery('body').on("click", function(event){
if (!jQuery(event.target).closest($searchInput[0]).length) {
autocomplete.autocomplete.close();
}
});
Found some answers to my questions.
1 - In order to populate the input with the title of the selected search result I added a call to the setVal method of the autocomplete object. I'[m still not sure why this is required.
/* Instantiate autocomplete.js */
var autocomplete = algoliaAutocomplete($searchInput[0], config, sources)
.on('autocomplete:selected', function (e, suggestion) {
autocomplete.autocomplete.setVal(suggestion.post_title);
});
2 - It looks like the config of the autocomplete object uses the value of WP_DEBUG in order to set the debug value. The options available for the autocomplete component can be found here https://github.com/algolia/autocomplete.js#options. This lead me to find that when debug is set to true, the autocomplete box does not hide on selection. This is to allow for easier debugging and styling of the component.

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"

How do you access the title (Node module element) field in Drupal 7?

I have a computed field in a Drupal 7 content type that is populated by my description (text) field:
$entity_field[0]['value'] = $entity->field_desciption['und'][0]['value'];
It works fine. I want to create another computed field that is populated by the title (Node module element) field.
I tried with the following lines, but they don't work.
$entity_field[0]['value'] = $entity->title['und'][0]['value'];
$entity_field[0]['value'] = $node->title;
How can I achieve this?
The node title is not a field; therefore, using $entity->title['und'][0]['value'] will not work. What you should use is $entity->title.
As side note, to get the value of a field, you should use field_get_items(), which takes care of the language set for the field, which isn't necessarily LANGUAGE_NONE.
If it's a node module element, it should be accessible via $entity->title directly.
Try a print_r($entity); die; to get all elements of the entity. Hope this help you.
You should look at printing the array/object to the page to see what you are working with exactly.
Try adding print_r($entity); or print_r($node); to the page where the entity or node is displayed followed by exit;
You can then right click the page and click 'View page source' to display the output in a structured format. Use this to see the variable names, object/array types and hierachy to then write your full variable code correctly.
print_r($node);
exit;
I would imagine it should have been $node->title though...

Drupal 6 Views: Toggling filters on/off via checkbox?

I have a view I'm using as an "Advanced Search" tool - it simply provides a list of relevant nodes which are limited by a number of exposed filters I have in place. In particular, I have a "keyword" exposed filter that's acting as the main search box, which filters on the node title. Now, I've had a request for the ability to toggle (I assume with a checkbox or similar) this filter to search on both the node title and body at the same time, as an option to provide users with more search results.
I currently have a view set up with two displays: one that filters on just the node title, and one that uses Views Or to limit the results to nodes that have the keywords in either the node title OR the node body. By themselves, both of these displays are working the way I'd like.
My question is how I'd go about toggling between these two displays using a checkbox or something similar. I don't want to use Views Display Tabs because both displays look visually identical (the only difference is how the filters are configured to include more/less results) which would be confusing to the user - plus it uses AJAX which breaks some (mostly small) modifications I've made via jQuery to the behavior of the view.
What I'd like to accomplish, in essence, is a conditional check when the view is submitted - depending on whether a checkbox is selected, the arguments are sent to one display or the other. Any suggestions on how to accomplish this?
One way you might be able to go about this could involve adding a custom validate handler on the exposed filter form that would in theory allow you to check the value and forward the request to a different page display appropriately. Exposed filters are exposed in the URL as $_GET variables, so, forwarding a user to a specific page with filters predefined should be easy enough.
<?php
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'views_exposed_form':
// Deterimine the ID so you only do this
// to a specific exposed filter form
// drupal_set_message($form['#id']);
if ($form['#id'] == 'views-exposed-form-api-search-page-1') {
// You might also want to add the checkbox FAPI item in this area
$form['#validate'][] = 'mymodule_api_search_validate'; // custom validate handler function name
}
break;
}
}
function mymodule_api_search_validate($form, &$form_state) {
// Check if the FAPI item has the specified checkbox value
if ($form_state['values']['options'] == 'title') {
// The get variables to pass to the views exposed filters
// You can configure what this $_GET variable should be while editing the filter
$query = array(
'query' => 'the search query',
);
drupal_goto('api/search', $query);
}
}
?>

JavaScript calendar in Drupal (Scheduler module)

hi i have installed scheduler module from drupal.org. and i have set all the settings regarding this. now i can set publish and unpublished date in text box(there is format of date below text box).
i want to use java script calendar so when user click on the text box ,the calendar should open.
how can i do this
Since you're using Drupal you should have JQuery preloaded. You don't necessarily need JQuery to do this but it makes it easier, you'll use JavaScript either way.
With JQuery, simply setup a click listener on the textbox and have it call the whatever function triggers the calendar display like so...
Note: I used a simple ID selector below "#textbox" replace it with whatever way you want to select your textbox input. (eg. if you've given the HTML textbox input element an ID of "textbox" you can use my example)
$(document).ready(function(event) {
$('#textbox').live('click', function() {
//call your function here
});
});

Resources