This module has been working great for me. I recently discovered a conflict with another module that also converts multiselect fields. (The multiselect module -- http://drupal.org/project/multiselect ). I looked at the code of both of these modules, and because they work pretty differently, I couldn't see an obvious way to make them play well together.

So that leads me to a much more general question: Is there a way to exclude a field from being converted to Better Select checkboxes? Anyone done this already?

CommentFileSizeAuthor
#8 540918.patch2.62 KBalex.k
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

batchman_paradize’s picture

Status: Active » Needs review

Here is a quick and dirty way to add this functionality to betterselect.module. I won't provide a patch at the moment since I have lots of other new code in my current betterselect.module right now. It might get submitted later though, if someone really wants it.

Note: All the code below is supposed to be put in betterselect.module.

Add this to function betterselect_admin_settings:

  $form['betterselect_general']['betterselect_exclude_fields'] = array(
    '#type' => 'textarea',
    '#title' => t('Excluded fields (uses preg_match)'),
    '#cols' => 120,
    '#rows' => 7,
    '#description' => t('Enter regular expressions to exclude pages and fields from conversion (eg "/[path regexp]/;/[fieldname_regexp]/"). One entry per row.'),
    '#default_value' =>  variable_get('betterselect_exclude_fields', '/^\/admin.*$/;/.*/'),
  );

Add this to the end of the arguments of the first if-statement in function betterselect_process:

&& !betterselect_is_page_excluded($complete_form, $element)

Add this function somewhere at the end:

/**
 * Check if the current page is supposed to be excluded
 * based on the regexps in betterselect_exclude_fields
 */
function betterselect_is_page_excluded($complete_form, $element) {
	// Get the action-string
	$action = $complete_form['#action'];
	// Remove ?q= if needed
	$action = preg_replace("/^\/\?q\=/", "/", $action, 1);
	// Retrieve all regular expressions into an array
	$exclude_patterns = explode("\n", variable_get('betterselect_exclude_fields', '/^\/admin.*$/;/.*/'));
	foreach($exclude_patterns as $cur_pattern) {
		$cur_pattern = explode(";", trim($cur_pattern));
		if(preg_match($cur_pattern[0], $action) != 0) {
			if(preg_match($cur_pattern[1], $element['#name']) != 0) {
				return TRUE;
			}
		}
	}
	return FALSE;		
	
}
lelizondo’s picture

I've tried the code and I don't know if is not working or if I just have the wrong expression, I'm using:

/^\/node/add/news.*$/;/.*/

Since I don't want to use betterselect on any field on that node add page.

Any help would be appreciated

batchman_paradize’s picture

The code works if you put it in the right place, but your regular expression is wrong, it should be:

/^\/node\/add\/news.*$/;/.*/

All "/" except the start and end ones has to be escaped using "\" since I use the whole string, including the start and end "/" directly as an argument. If you want to know more about regular expressions in PHP you can visit this page:
http://www.regular-expressions.info/php.html

What do you think about this type of filter? Maybe it would've been a good idea to add some easy way to check the validity of a filter? Anyone knows about a good set of regular expressions to validate regular expressions in PHP?

lelizondo’s picture

Thanks, I'll try it later. About your question, I think is not the easiest way to filter what fields should be excluded. Check-boxes would be easier but that means the route would be excluded and there could be cases when you want to exclude a field on a certain route and include it on some other route

batchman_paradize’s picture

Ok, looking forward to your report whether it works or not.

I know it's not a very easy filter to use for everyone, but it's definitely the most flexible one. What I was looking for was to maybe use at least some type of validation which makes sure the strings are correct. Also maybe some type of test where you can enter a path and test against. Someone who feels up to the task? I don't have the time at the moment.

lelizondo’s picture

I don't know enought about preg_match to help you with a validation, what I can do is to provide better help inline and provide a patch will I'll be doing it tonight.

scalp’s picture

I'd love to be able to use this. I've got all the code in the right place (I think), but no matter what I put in the exclude list I cannot get it to exclude. I really just need to have this module affect the output of one field on the user/%/edit page, but hiding it on the node creation forms would probably do the trick.

EDIT: Disregard. I got this working per the directions above. I had another module installed that was causing the problem. Works great. Thanks!

alex.k’s picture

FileSize
2.62 KB

The change works for me and here it is attached as a patch against current CVS.

danielbeeke2’s picture

Hey

I like the module,

and I needed this patch

wouldn't it be beter to select where the betterselect gets loaded with the 'common' drupal way

pages to exclude
or
pages to include

for now I added a checkbox to the settings view with 'show only on view pages'
cause thats where i needed it

than i added a check if were on a view.

Greetings Daniel