Hi Guys..,

I hope someone can help... I've been struggling with this for a few months and getting closer but not close enough.

Using Views2 I have created a view with an exposed filter, The website visitor would enter the information to limit the search.

What I need to do is modify the information entered by the user.

I have created a module and can get access to $view->build_info['query'] but the query text is showing $d where the information should be...

When I have viewed the sql code in the view builder, i see the text I want to modify, so I created a module and tested with the following function....

function albany_views_query_alter(&$view, &$query) {
if ($view->name == 'Postcode_Search') {
print($view->build_info['query']);
}
}

Also tried **function albany_views_pre_execute(&$view)**

But the text I needed to change isn't in the *sql query* array, but the placeholders for the informaiton is e.g. $d

Any Ideas how I can get access to the entered text?

Your help would be very very well appreciated.

Many thanks in advance.

Regards

Steve

Comments

albany’s picture

Ok, so I assume all of the *experts* that claim they are drupal *experts* are just script kiddies....

Working through what I have got so far, incase it helps someone else... (doesn't mean *I* know what I'm doing, just experimenting)

This code does mess up the output of the view, but at least we can see what the $view array looks like

created a module called "albany" with albany.module and albany.info

The following code is in the albany.module file

function albany_views_query_alter(&$view, &$query) {
	// - make sure we only chage the *view* we intend to change
	if ($view->name == 'Postcode_Search') {

		//Loop through the array and print the array key and value
		foreach( $view as $key => $value){
			echo "Key: $key, Value: $value <br />";
			
			//If the key is another array - loop through that array and print ---> to indicate its a child array 
			if (is_array($value)){
				foreach( $value as $ikey => $ivalue) {
				echo "--->Key: $ikey, Value: $ivalue <br />";
				}
			} 
		}
		//check to make sure we have found the right array element by printing a reference to it
		print("<br><br>Before.. " . $view->exposed_raw_input['field_ca_postcode_value']."<br>");
		$retval = '';
		$retval = '-' . $view->exposed_raw_input['field_ca_postcode_value'];

		// modify the array element - found 3 that relate to our data entered
		$view->exposed_raw_input['field_ca_postcode_value'] = $retval;
		$view->exposed_input['field_ca_postcode_value'] = $retval;
		$view->exposed_data['field_ca_postcode_value'] = $retval;

		// print the array element to confirm we have altered the array 
		print("After.. " . $view->exposed_raw_input['field_ca_postcode_value']."<br>");

		RETURN $view;
	}
}


The code displays something like this (only part of the displayed output posted here):-

Key: exposed_input, Value: Array
--->Key: field_ca_postcode_value, Value: S1
--->Key: field_ca_experties_value, Value: 1
Key: exposed_data, Value: Array
--->Key: field_ca_postcode_value, Value: S1
--->Key: field_ca_experties_value, Value: 1
--->Key: submit, Value: Apply
--->Key: form_build_id, Value: form-4361a5c03a8acd52c68fd0112cdba85d
--->Key: form_token, Value: c586bbd81aa76e0da11bf3b8128894e8
--->Key: form_id, Value: views_exposed_form
--->Key: , Value: Apply
Key: exposed_raw_input, Value: Array
--->Key: field_ca_postcode_value, Value: S1
--->Key: field_ca_experties_value, Value: 1

The visitor has entered "S1" in the search field, with 3 instances of "S1" being visible in the array.

Only problem is, after modifying the 3 instances i've found, it still does not modiy the query... this can be a cache problem so I have also tried hook_views_query_substitute.... not sure how to send the results back yet so going to experiment some more.

Next step for me is to create another loop to view more array elements, just incase the bit i need to modify is actually deeper in the array than I have looked so far....

Any help / advice is welcome.

If not, i will keep at it.... ya never know, one day *I* may become an expert *grin*

Regards

Steve

albany’s picture

Ok, so its done, and working....

added this ..

drupal_set_message('

' . print_r($query, TRUE) . '

');

under...

// print the array element to confirm we have altered the array
print("exposed_raw_input after.. " . $view->exposed_raw_input['field_ca_postcode_value']."
");
print("exposed_input after.. " . $view->exposed_input['field_ca_postcode_value']."
");

and it enabled me to see which array element I needed to change...

then added...

$query->where[0]['args'][2] = $retval;

And it works

Here is the module without the experimental code...


<?php
// $Id$


function albany_views_query_alter(&$view, &$query) {
	// - make sure we only chage the *view* we intend to change
	if ($view->name == 'Postcode_Search') {

		//check to make sure we have found the right array element by printing a reference to it
		$retval = '';
		$retval = '*' . $view->exposed_raw_input['field_ca_postcode_value'];

		// modify the array element - found 3 that relate to our data entered
		$view->exposed_raw_input['field_ca_postcode_value'] = $retval;
		$view->exposed_input['field_ca_postcode_value'] = $retval;
		$view->exposed_data['field_ca_postcode_value'] = $retval;
		
		// change the argument
		$query->where[0]['args'][2] = $retval;
	}
}

It may not need this next section, but I was too excited that it worked to remove it and retest... may do it later:-

// modify the array element - found 3 that relate to our data entered
$view->exposed_raw_input['field_ca_postcode_value'] = $retval;
$view->exposed_input['field_ca_postcode_value'] = $retval;
$view->exposed_data['field_ca_postcode_value'] = $retval;

I hope it helps others...

Have fun.

Regards

Steve