hook_form_FORM_ID_alter function in Drupal - forms

I would like to overwrite a form by creating a custom module. But therefor, I need to get a hold to my form ID in the function.
Drupal.org mentionned the hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) function, but when I apply this to my form, it doesn't respond. Do I have a syntax error when I use following statement?:
function redactie_omgeving_fiche_node_form__form_alter(&$form, $form_state, $form_id) {
echo(var_dump('ONZE redactie_omgeving__fiche_node_form__form_alter'));
}
and this is my form:
<form class="node-form node-fiche-form" enctype="multipart/form-data" action="/node/1/edit" method="post" id="fiche-node-form" accept-charset="UTF-8">

A nice little trick to make sure you have the right form id is to first use hook_form_alter() to get the form id, then use hook_form_FORM_ID_alter(). For example, use:
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message($form_id);
}
to see a Drupal message of all form id's on the current page. Once you have the correct id, then place it into hook_form_FORM_ID_alter(). Try that and make sure you are using the right form id.

Related

Lock/unlock form with toggle

I'm looking for a way to have a button toggeling the field of my form. When "locking" the form with the toggle button no data can be typed. When "unlocking" data should be allowed to be typed. What I want to achieve with this is simple avoiding users to accidentally type.
I found the code below and it works. Only problem is that it only applies to one input field. I want it to work on more that one.
<input type="checkbox" id="yourBox">
<input type="text" id="yourText">
<script>
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').disabled = this.checked;
};
</script>
Mark the fields you want to disable with a CSS class, and then use jQuery to disable them.
jQuery - Disable Form Fields
If you want a pure Javascript solution, just repeat this line
document.getElementById('yourText').disabled = this.checked;
for each field.
Or, you can do something like this this: How to Get Element By Class in JavaScript?. Note that you can assign multiple CSS classes to the same field, so assign another class to identify those fields that need to be disabled.

lift jsonform id

I use SHtml.jsonForm in myjsonclass.show to wrap a jsonform to the HTML page with the following command:
<div id="form" class="lift:myjsonclass.show">
It works fine.
The SHtml.jsonForm method defines a random id for the form tag, I wonder if there is a solution to get that id and use it in the HTML. It will make easier for example to apply form validators in Javascript.
I have solved that by accessing a known element of the form and asking for its parent in javascript: element.parent(); So I am able to get id of the form with: element.attr('id').

Change Element's Name Attribute in Gravity Forms

Is it possible via some sort of Hook or Filter to change the "name" attribute on a form element in Gravity Forms? It allows you to select "Allow field to be populated dynamically" and then set a "Parameter Name", however the parameter name doesn't match up with the element's name attribute. My element's names are like input_6_1 or something.
I'm trying to avoid using jQuery to accomplish this, but I suppose I will resort to it as a last resort. Any ideas?
It isn't the most beutiful code, and I'm not sure if there is a gravity forms approved way (it wasn't apparent to me), but something like this should work for you.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#input_1').attr('name','YOURCUSTOMNAMEVALU');
});
</script>
Also, for WordPress you should probably wrap it in a scope to contain this to the page that contains your form.
Something like
<?php
if(is_page('forms-page') && !is_admin()) {
?>
//Javascript Here
<?php } ?>
For style points you can use enqueue_script to include it from your functions.php
Set the parameter name to something like 'customparam'.
Then dynamically populate it using a filter.
add_filter('gform_field_value_customparam', 'populate_customparam');
function populate_customparam($value){
return 'Hello';
}
This will dynamically insert 'Hello' into every gravity form field with parameter named 'customparam'

question about CodeIgniter urls

I am using an application (a blog) written using the CodeIgniter framework and would like to search my blog from my browsers location bar by adding a string to the end of my blogs url like this:
http://mysite.com/blog/index.php/search...
As you can see in the example above I am not really sure how to format the rest of the url after the search part so I am hoping someone here might be able to point me in the right direction.
This is what the form looks like for the search box if that helps at all.
form class="searchform" action="http://mysite.com/blog/index.php/search" method="post">
<input id="searchtext" class="search_input" type="text" value="" name="searchtext">
<input type="submit" value="Search" name="Search">
</form>
Thx,
Mark
Since your form is posting to http://mysite.com/blog/index.php/search, I'm assuming this 'search' controller's default function is the one your are attempting to submit your data to. I think that the easiest way to do this would be to just grab the post data inside of the controller method you're posting to. Example:
function search()
{
$search_params = $this->input->post('search_text');
}
Then you would have whatever the user input stored as $search_params, and you can take actions from there. Am I misunderstanding what you're asking?
It seems like you're kind of discussing two different approaches. If you wanted to make a request to
mysite.com/blog/index.php/search&q=what_I_am_looking_for
This is going to call the search controllers default method (which is index by default). If you wanted to use the URL to pass parameters like that you would go to your function in the search controller and do:
print_r($this->input->get('q'));
This will print out "what_am_I_looking_for".
An easier approach in my opinion would be to:
1. Create a view called "search_view" with the HTML content you pasted above, and have the form "action" http://www.mysite.com/blog/index.php/test/search
Create a controller called "Test" that looks like the following:
class Test extends CI_Controller {
function search()
{
$search = $this->input->post('searchtext');
print_r($search);
}
public function display_search()
{
$this->load->view('search_view');
}
}
Visit http://www.mysite.com/blog/index.php/test/display_search in your browser. This should present you with the form you placed in search_view.php. Once the form is submitted, you should be sent to the search function and print out the variable $search, which will have whatever text you submitted on that form.
If this isn't what you were looking for then I am afraid I do not understand your question.

Programmatically submitting a form while using AjaxForm

I wanted to find a way to upload a single file*, in the background, have it start automatically after file selection, and not require a flash uploader, so I am trying to use two great mechanisms (jQuery.Form and JQuery MultiFile) together. I haven't succeeded, but I'm pretty sure it's because I'm missing something fundamental.
Just using MultiFile, I define the form as follows...
<form id="photoForm" action="image.php" method="post" enctype="multipart/form-data">
The file input button is defined as...
<input id="photoButton" "name="sourceFile" class="photoButton max-1 accept-jpg" type="file">
And the Javascript is...
$('#photoButton').MultiFile({
afterFileSelect: function(){
document.getElementById("photoForm").submit();
}
});
This works perfectly. As soon as the user selects a single file, MultiFile submits the form to the server.
If instead of using MultiFile, as shown above, let's say I include a Submit button along with the JQuery Form plugin defined as follows...
var options = {
success: respondToUpload
};
$('#photoForm').ajaxForm(options);
... this also works perfectly. When the Submit button is clicked, the form is uploaded in the background.
What I don't know how to do is get these two to work together. If I use Javascript to submit the form (as shown in the MultiFile example above), the form is submitted but the JQuery.Form function is not called, so the form does not get submitted in the background.
I thought that maybe I needed to change the form registration as follows...
$('#photoForm').submit(function() {
$('#photoForm').ajaxForm(options);
});
...but that didn't solve the problem. The same is true when I tried .ajaxSubmit instead of .ajaxForm.
What am I missing?
BTW: I know it might sound strange to use MultiFile for single-file uploads, but the idea is that the number of files will be dynamic based on the user's account. So, I'm starting with one but the number changes depending on conditions.
The answer turns out to be embarrassingly simple.
Instead of programmatically submitting using...
document.getElementById("photoForm").submit();
... I used...
$("#photoForm").submit();
Also, since I only need to upload multiple files on occasion, I used a simpler technique...
1) The form is the same as my original...
<form id="photoForm" action="image.php" method="post" enctype="multipart/form-data">
2) The file input field is basically the same...
<input id="photoFile" "name="sourceFile" style="cursor:pointer;" type="file">
3) If the file input field changes, submit is executed...
$("#photoFile").change(function() {
$("#photoForm").submit();
});
4) The AjaxForm listener does its thing...
var options = {
success: respondToUpload
};
$('#photoForm').ajaxForm(options);