Send Gravity Forms data to redirection page - wordpress

I have a very simple form created with Gravity Forms;
It submits two numbers and then redirects to a different result page.
How do I retrieve those two numbers on the result page?

add_filter("gform_confirmation_4", "custom_confirmation", 3, 4 );
function custom_confirmation($confirmation, $form, $lead, $ajax)
Gives a custom confirmation. Each field value can be retrieved by using $lead[{field ID}]

I have a solution for this based on using a combination of form submission hooks and the GForms API. It's a horrible plugin so I apologise for the messiness of the logic flow. It's important to use the framework methods rather than processing the data yourself since there are a good amount of hacks and shonky things going on in there to correctly match field IDs and so forth.
I will provide a solution to pass a submission from one form to pre-populate another. Changing the destination for POST data is pretty straightforward, they have an example for it on their gform_form_tag hook documentation page. Yes, that really is the only way of doing it.
Without further ado here is the code. I've set it up to work off form configuration to make things simpler for the end user, so it works like this:
Select "allow field to be populated dynamically" in your destination form field's advanced settings and choose a parameter name for each.
Add matching CSS classes on the source fields of the other form(s) to setup the associations.
Add a CSS class to the source forms themselves so that we can quickly check if the redirection is necessary.
.
$class = 'GForms_Redirector';
add_filter('gform_pre_submission', array($class, 'checkForSubmissionRedirection'), 10, 1);
add_filter('gform_confirmation', array($class, 'performSubmissionRedirection'), 10, 4);
abstract class GForms_Redirector
{
const SOURCE_FORMS_CLASS_MATCH = 'submission-redirect';
const DEST_PAGE_SLUG = 'submit-page-slug';
const DEST_FORM_ID = 1;
protected static $submissionRedirectUrl;
// first, read sent data and generate redirection URL
function checkForSubmissionRedirection($form)
{
if (false !== preg_match('#\W' . self::SOURCE_FORMS_CLASS_MATCH . '\W#', $form['cssClass'])) {
// load page for base redirect URL
$destPage = get_page_by_path(self::DEST_PAGE_SLUG);
// load form for reading destination form config
$destForm = RGFormsModel::get_form_meta(self::DEST_FORM_ID, true);
$destForm = RGFormsModel::add_default_properties($destForm);
// generate submission data for this form (there seem to be no hooks before gform_confirmation that allow access to this. DUMB.)
$formData = GFFormsModel::create_lead($form);
// create a querystring for the new form based on mapping dynamic population parameters to CSS class names in source form
$queryVars = array();
foreach ($destForm['fields'] as $destField) {
if (empty($destField['inputName'])) {
continue;
}
foreach ($form['fields'] as $field) {
if (preg_match('#(\s|^)' . preg_quote($destField['inputName'], '#') . '(\s|$)#', $field['cssClass'])) {
$queryVars[$destField['inputName']] = $formData[$field['id']];
break;
}
}
}
// set the redirect URL to be used later
self::$submissionRedirectUrl = get_permalink($destPage) . "?" . http_build_query($queryVars);
}
}
// when we get to the confirmation step we set the redirect URL to forward on to
function performSubmissionRedirection($confirmation, $form, $entry, $is_ajax = false)
{
if (self::$submissionRedirectUrl) {
return array('redirect' => self::$submissionRedirectUrl);
}
return $confirmation;
}
}
If you wanted to pass the form values someplace else via the querystring then you'd merely need to cut out my code from the callback and build your own URL to redirect to.

This is a very old question, now you can send it using a Query String on the confirmation settings.
They have the documentation on this link:
How to send data from a form using confirmations
Just follow the first step and it will be clear to you.

Related

I want to track internal URL that brought the URL to the next page or exit page

I want to track the URL of the current page, then pass it to a URL variable to the next page.
For example
https://www.mywebsite.com
https://www.mywebsite.com/about/?fromsource=https://www.mywebsite.com
https://www.mywebsite.com/about/careers/?fromsource=https://www.mywebsite.com/about
https://www.mywebsite.com/contact/?fromsource=https://www.mywebsite.com/about/careers
So everytime someone moves to the next page, the URL of the previous page is add to a URL variable in the next page.
I'm not sure if this is the right way. I would use Google Analytics API referral data. But you can create the following shortcode:
function get_current_page_link () {
global $wp; //get global wp query
return home_url( $wp->request ); //get the url of the current page
}
add_shortcode('get_current_page_link ', 'get_current_page_link ');
If you want to add the code via PHP code:
About
If you want to use it in your editor you can add url's as such:
https://www.mywebsite.com/about/?fromsource=[get_current_page_link]
The editor in the text mode, it will show up like this:
About
Why not use document.referrer?
So e.g. create a new variable in your web-analytics system and use the following js code to populate it:
function includeReferrer() {
var ref = document.referrer,
loc = document.location.href,
newLoc;
if (loc.includes('?') != -1) {
newLoc = loc + "&fromsource=" + ref;
} else {
newLoc = loc + "?fromsource=" + ref;
}
}
In this case, you are not overwriting the actual URL, but instead create a new string which includes the requested parameter and can be used for any kind of tracking purpose.

Adding users and attributes programatically in Concrete5 version 8

Hi I have recently followed some documentation to create new users from a csv file programatically. According to the Concrete5 docs/api there was method called getByID( $uID ) but this has since be deprecated!
I am creating a new user like so:
$userRegistration = Core::make('user/registration');
$file = fopen("test-users.csv","r");
while (($data = fgetcsv($file)) !== FALSE) {
echo "email address " . $data[0];
$userRegistration->create([
'uName' => $data[0],
'uPassword' => $data[1],
'uEmail' => $data[2],
'uIsValidated' => true,
]);
}
However if I want to add a value to an existing non-core attribute for instance lets call it user_county then how would I change this after programatically adding the user? I may need to do this for several user attributes as well so the values would need to come from the CSV and automatically be looped through to apply the correct value to the corresponding attribute whether it is blank or filled.
The create() method will return a UserInfo object (Concrete\Core\User\UserInfo) after successfully adding a new user. With the returned UserInfo object, you can use the setAttribute() method to set your custom user attribute.
Make sure that you have created the customer user attribute first and check that it is available before setting it, otherwise setting it will throw an error. I believe you can do this using Concrete\Core\Attribute\Key\UserKey::getByHandle('my_user_attribute') and seeing if it returns an object.
The create() method is in the RegistrationService class:
https://github.com/concrete5/concrete5/blob/develop/concrete/src/User/RegistrationService.php#L51-L140

how to read additional parameters in alfresco 5.1.1- aikau faceted search

Custom Search UI will be populated when user selects Complex asset in the Advance search screen drop down(apart from Folders,Contents) where 12 fields will be displayed .So when user clicks search button ,need to read those values and redirect to the alfresco repo files(org/alfresco/slingshot/search/search.get.js).We have already customized these files(search.get.js,search.lib.js) existed in the repository to suit out logic and working fine in 4.2.2;As we are migrating to 511,so we need to change this logic in customized faceted-search.get.js to read these values.How to write this logic in customized facted-search.get.js?
It's not actually possible to read those URL hash attributes in the faceted-search.get.js file because the JavaScript controller of the WebScript does not have access to that part of the URL (it only has information about the URL and the request parameters, not the hash parameters).
The hash parameters are actually handled on the client-side by the AlfSearchList widget.
Maybe you could explain what you're trying to achieve so that I can suggest an alternative - i.e. the end goal for the user, not the specifics of the coding you're trying to achieve.
We will be reading the querystring values something like below in the .get.js file.
function getNodeRef(){
var queryString = page.url.getQueryString();
var nodeRef = "NOT FOUND";
var stringArray = queryString.split("&");
for (var t = 0; t < stringArray.length; t++) {
if (stringArray[t].indexOf('nodeRef=') > -1) {
nodeRef = stringArray[t].split('=')[1];
break;
}
}
if (nodeRef !== "NOT FOUND") {
nodeRef = nodeRef.replace("://", "/");
return nodeRef;
}
else {
throw new Error("Node Reference is not found.");
}
}
It may be help you and we will wait for Dave Drapper suggestion also.

How can I remove a message shown from a different module?

In Drupal 7, I could use the following code.
if ($_SESSION['messages']['status'][0] == t('Registration successful. You are now logged in.')) {
unset($_SESSION['messages']['status']);
}
What is the equivalent code for Drupal 8?
First of all, in Drupal 8, messages are stored in the same $_SESSION['messages'] variable as before. However, using it directly is not a good way, as there exist drupal_set_message and drupal_get_messages functions, which you may freely use.
Then, status messages are shown using status-messages theme. This means that you can write preprocess function for it and make your alteration there:
function mymodule_preprocess_status_messages(&$variables) {
$status_messages = $variables['message_list']['status'];
// Search for your message in $status_messages array and remove it.
}
The main difference with Drupal 7, however, is that now status messages are not always strings, they may be objects of Markup class. They are wrappers around strings and may be cast to underlying string using magic method __toString. This means that they can be compared with and as strings:
function mymodule_preprocess_status_messages(&$variables) {
if(isset($variables['message_list']['status'])){
$status_messages = $variables['message_list']['status'];
foreach($status_messages as $delta => $message) {
if ($message instanceof \Drupal\Component\Render\MarkupInterface) {
if ((string) $message == (string) t("Searched text")) {
unset($status_messages[$delta]);
break;
}
}
}
}
}
Upon reading the related change record, I have discovered \Drupal::messenger()->deleteAll(). I hope this is useful to someone. UPDATE: You should NOT do this, as it removes all subsequent messages as well. Instead, do unset(['_symfony_flashes']['status'][0]).
You can install the module Disable Messages and filter out messages by pattern in the configuration of the module.
For this particular case, you can filter out the message using the following pattern in the module configuration
Registration successful.*
Although the question is asked around Drupal 8 which is no longer supported, the module works for Drupal 7, 8, 9.
You can solve your problem in more than one way.
First way:
You can make minor change in core user module.
Go on:
\core\modules\user\src\RegisterForm.php
In that file you have line you can change:
drupal_set_message($this->t('Registration successful. You are now logged in.'));
NOTE: This is easiest way but in this case way you will edit Drupal core module and that is generally bad practice. In further development you could have problems like overwrite your changes when you do update.
Second way:
You can disable end user message using module. Disable message module have option you need. In module configuration you have text box where you can filter out messages shown to the end users.
Third way:
Messages in Drupal 8 are stored in a session variable and displayed in the page template via the $messages theme variable. When you want to modify the variables that are passed to the template before it's invoked you should use preprocess function. In your case here you can just search string in session variable and alert/remove it before it's displayed.
function yourmodule_preprocess_status_messages(&$variables) {
$message = 'Registration successful. You are now logged in.';
if (isset($_SESSION['messages'])) {
foreach ($_SESSION['messages'] as $type => $messages) {
if ($type == 'status') {
$key = array_search($message, $messages);
if ($key !== FALSE) {
unset($_SESSION['messages'][$type][$key]);
}
}
}
}
}
(Note:Untested code, beware of typos)
Hope this helps!

after login, redirect to entrance url?

The site I'm working on is using the Frontpage module to redirect anon users to a login page. And I know about using triggers to set an action to redirect after login, (set to one specific url). But here's the catch:
My users are each arriving at a different entrance url, eg: www.mysite/PersonsName
Is there a way to redirect back to the entrance url after login?
No need to code: this is performed, with various settings available, by the existing login_destination module.
you can put this code in your custom module implementing hook_user().
function yourmodule_user($op, &$edit, &$account, $category = null)
{
switch ($op) {
case 'login':
$_REQUEST['destination'] = $_REQUEST['q'];
break;
}
}
generally it's enough to set $_REQUEST['destination'] to you desidered destination page
(this is what the module login_destination does i guess)
You can take the URL and explode by "/" like this:
$url = explode("/",$_SERVER['REQUEST_URI']);
Then, set up a session to keep the username he accessed like this:
$_SESSION['used_name'] = $url[0];
And you can set up the page that he is being redirected to after the succesful login like this:
$success_page = "yourpage/".$_SESSION['used_name'];
I hope this is what you were looking for.

Resources