Drupal - hook_menu_alter for webform - php

I have created a webform for client where the client information gets stored and the client can login and view the form, but when the client veiw the form it displays the submission table and then the client have to click on view in operation to display the results, i wanted to implement a function so it becomes possible to redirect the clients to the actual results directly instead of the submission table while for admin the submission table should be there...i guess i need to implement hook_menu_alter() in a custom module...was wondering if someone could help me with the code for hook_menu_alter()...the url for submission table is "node/$nid/submissions" and for the results is "node/$nid/submission/$sid". Thanks

You don't need hook_menu_alter to redirect after form submission.
You can simply add a #redirect to your $form at hook_form_submit()
It should be something like this:
function hook_form($form_state){
// $form[] definition here
$form[] = array(
'#type' => 'submit',
'#value' => 'Submit Me!',
'#submit' => array('hook_form_submit'),
);
}
function hook_form_submit($form,&$form_state){
// sanitize/save your data here!
$form_state['redirect'] = 'redirect/me/to/somewhere/else';
}

yes..both the user and the admin view the same form...n admin will be filling up the form on behalf of user and set the author as user so that user can view the filled info as we will set the user permission as "access own results". Now, the issue is when user views the webform results he gets a 'table' first which shows the 'date' and 'Operations' and in operation if user click on 'view' then the filled info is being displayed. so, i was just wondering if we can use hook_menu_alter to change that and instead of that 'table' it directly shows the filled info to the user...thanks andre..

Related

Wordpress: Access URL for post which was just created

I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).
To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).
I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.
My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.
My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??
I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!
The frame of my code is attached.
add_shortcode('sut_form', 'sut_form_shortcode');
function sut_form_shortcode() {
if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {
// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);
// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED
wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST
// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}
Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.
The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:
$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");
It is not too neat but should work.

How to show a field after validateForm() is called in Drupal 8?

Inside a hook_form_FORM_ID_alter() function I show or hide a field based on another checkbox field is checked or not with the following code in Drupal 8.
$form['field_my_url']['#states'] = [
'visible' => [
':input[name="field_my_checkbox[value]"]' => ['checked' => TRUE],
],
];
In case user checks field_my_checkbox checkbox, enters an invalid URL in field_my_url and decides to uncheck field_my_checkbox checkbox, the field_my_url will then be hidden with the invalid URL remaining. It will fail the validation because the enter URL is not valid.
The user will then be redirected back with the error message. But because the field_my_checkbox checkbox was not checked, the field_my_url field will be hidden with the error message and the user cannot see that.
In this case, how can I show the field_my_url field if it failed the validation because it contains invalid URL in Drupal 8?
I have found a solution. It might not be efficient. But it solves the problem for now, since there is no one come up with any solution on this moment. If the validation failed. I just unset the '#states' from the field. By doing this, no #states visible will be applied and the field_my_url will be show as normal.
unsert($form['field_my_url']['#states']);

How to to implement a forgot password feature using the Google Identity Toolkit in php

I am trying to add google identity toolkit in php. Signin option is working correctly but when i am clicking on problem in sign in link it is showing capthca after submitting captcha it is not navigating to any url.
email.php
<?php
include "identity-toolkit-php-client-master/src/GitkitClient.php";
$gitkitClient=new Gitkit_Client();
$oob_response = $gitkitClient->getOobResults($_POST);
$oob_link = $oob_response['oobLink'];
echo json_encode($oob_response);
?>
email.php is the oobactionurl file. when i am using this code I am getting this error .image
You need to create a php file to retrieve and send the reset link to the user. Make sure the oobActionUrl widget option points to this file. Within the file, you'll get the generated link and additional information by calling $gitkitClient->getOobResults($_POST). It should also work if you exclude $_POST, as the function will check the post contents if no arguments are passed. Then, you can get the link itself like this:
$oob_response = $gitkitClient->getOobResults($_POST);
$oob_link = $oob_response['oobLink'];
From there, you can use your email function of choice to send it to the user. The returned array should contain the following.
'email' => email of the user,
'oldEmail' => old email (for ChangeEmail only),
'newEmail' => new email (for ChangeEmail only),
'oobLink' => url for user click to finish the operation,
'action' => 'RESET_PASSWORD', or 'CHANGE_EMAIL',
'response_body' => http response to be sent back to Gitkit widget
Let me know if you have any further questions.

Initializing the php form created with Formtools.org

I'm working on a form with Form tools http://www.formtools.org
My task is to create php multi-page form, using their API. And it should also save data after every step, not only after the last one.
But I already have a problem while initializing the form into the system.
I have all pages built, and when I'm doing test submission it works good. I successfully see my 'Thank you page'.
Following is the code from my 1st page
<?php
require_once("/app-administration/global/api/api.php");
$fields = ft_api_init_form_page("", "test");
$params = array(
"submit_button" => "submit",
"next_page" => "lc_2.php",
"form_data" => $_POST
);
ft_api_process_form($params);
?>
Then, due to the tutorial, I go to the admin panned, create new form there. And then, I need to initialize it.
So, I'm changing this line:
$fields = ft_api_init_form_page(12, "initialize");
But then, when I try to submit the form I get 100 error on the last page:
http://docs.formtools.org/api/index.php?page=error_codes#100
It says there is something with ID, but 12 is correct one because I have it from admin panel, and I double-checked it there.
Anybody have any ideas why I might have this error? Any help will be appreciated.
Thanks

yii button to create db entry

I would like to create a button that can be used to populate a table in my db with a single click.
I am just not sure what I need to do here to make this happen. Can I assign a method to be executed by a button? Or just have values picked up in my controller? Below is something like what I want to execute but through a button.
public function addInterest($interest)
{
$interest->UserId=Yii::app()->user->id;
$interest->ItemId=$this->ItemId;
return $interest->save();
}
**Additional details in response to Jaison Justus
With this implementation I am using controller and view from Model A (ItemId) where the button is to be displayed. Then there is Model B (UserId). Taking the info from Model A (ItemId) and Model B (UserId) I want to populate Model C ($interest) with that ItemId and UserId upon clicking a button. Looks like CJuiButton might provide a means to build it from being as then I can disable/hide the button after selected once. I am just not familiar with using buttons other than on a form where user input in collected, as links, or to provide pop up messages.
The code above currently sits in Model A model. With the code below in Model A controller everything works to populate Model C if I use a form and collect input. Since I do not require any input other then selecting the button from the user the form has nothing to put into it and therefore I know I can not use if(isset($_POST['Interest'])) as I have below.
public function actionView($id) {
$items=$this->loadModel($id);
$interest=$this->newInterest($items);
$this->render('view', array(
'model' => $items,
'interest' => $interest,
));
}
protected function newInterest($items)
{
$interest=new Interest;
if(isset($_POST['Interest']))
{
$interest->attributes=$_POST['interest'];
if($items->addInterest($interest))
$this->refresh();
}
return $interest;
}
In response to VarioN
Here is an attempt at using ajax. However this does not work and gives an Error 500 when ran. Is my controller action appropriate for what I am trying to do here?
Controller
public function actionAddInterest() {
$connection = yii::app()->db;
$sql1 = "INSERT INTO interest (UserId, ItemId)
VALUES(".Yii::app()->user->id.",".$this->ItemId.")";
$connection->createCommand($sql1)->execute();
}
View
<?php
echo CHtml::ajaxLink(
'Add Interest',
array('/item/addInterest'),
array('update'=>'#req_res')
);
?>
Looking at your question I see that you don't understand how MVC in Yii works.
Look at this 15 minutes screencast (Yii Tour - 3rd Stop - CRUD County) and after you will be able to create such button in any way you need (try use Gii and than customize it in your way - it's the easiest way).
Updated:
Seems that you need AJAX request. You can add CHtml::ajaxButton() in your view.
It will work this way:
User push the button, button do request (with JavaScript) to your
site without reloading the page and invisible for user.
Your controller action will serve this request: it can make some things (for ex., save data to db) and output data that your JavaScript possibly will display to user.
Than your JavaScript get answer and can make some changes on the page
(for example, hide button or show text got from request).
You can look at simple example with ajax here
If you needn't to submit form info with your button you can user ajaxLink. Example for it is here
There are a lot of examples with ajax and yii in the internet and at yii forum. Try to find them it may be very helpful.
Ask questions if you would have any.
Second update:
First, try to do your sql query simplier:
"INSERT INTO interest (UserId, ItemId) VALUES (1, 2)"
Than enable logging of mysql queries to log: at config/main.php add "trace" to "levels"
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning, trace',
),
Now you can try to press an AJAX link and look at the protected/runtime/log.txt and determine the problem.
Additional info to AJAX requests
All that outputs your ajax scripts can be viewed by browser's features:
At Chrome: press F12, go to Network, press an ajax-link and look at request response.
At Firefox with addon "Firebug".
With this you can determine whether a request is done or not.

Categories