I would love the ability to use attribute selectors, eg for wildcard searches. A use case would be for Views Exposed Forms - each view/display gets a unique form id and could be matched with the following css partial attribute selector (the ^ means "starts with")

[id^=views-exposed-form-]

Comments

marcvangend’s picture

Yes, this would be great to have. Another syntax (a little friendlier to less technical users) would be to use wildcards like we do in block configuration:
views-exposed-form-*

yannickoo’s picture

That would be possible with a jQuery selector: [class^="views-exposed-form-"].

marcvangend’s picture

FYI: Currently, the module has a smart way of deciding (during a hook_form_alter) if the compact forms javascript needs to be added to the page. It does so by comparing the form_id to the module settings. If the module would allow jQuery selectors to be entered (as in #2), that would probably mean that it's no longer possible to figure out if the javascript needs to be added, so it should be added on every page.

nuez’s picture

StatusFileSize
new3.09 KB

I´ve created a patch that adds the possiblity to select forms with an CSS ID wildcard. Please review.

yannickoo’s picture

Status: Active » Needs work

Hey Nuez, please use spaces instead of tabs :)

+++ b/compact_forms.jsundefined
@@ -81,7 +81,19 @@ Drupal.behaviors.compactForms = {
+		// check if the selectors have a wildcard
+		var ids = settings.compactForms.forms;
+		for (var i=0;i<ids.length;i++){
+			var value = ids[i];
+			if(value.substring(0, 1) == "*"){
+				var value = value.slice(1);
+				ids[i] = '[id*="'+value+'"]';
+			} else {
+				ids[i] = '[id="'+value+'"]';
+			}
+		}

Tabs! Use spaces instead.

+++ b/compact_forms.moduleundefined
@@ -74,8 +74,23 @@ function compact_forms_pre_render($form) {
+	$apply = false;
+
+	foreach($css_ids as $key => $css_id){
+		$form_id = $form_ids[$key];
+		if(substr($css_id == '*')){
+			if(strpos($form['form_id']['#value'], $form_id) !== false || strpos($form['#id'], $css_id) !== false){
+				$apply = true;
+			}
+		} else {
+			if ($form['form_id']['#value'] == $form_id || $form['#id'] || $form['#id'] == $css_id){
+				$apply = true;
+			}
+		}
+	}
+

Tabs again

I think it would be better to allow every kind of selector like it is allowed in jQuery.

nuez’s picture

Hi yannickoo,

Thanks for the hint, i´ve generated the patch with git on ubuntu, so it´s the default behaviour, I think. Although I did manually remove some trailing whitespaces. Can you give me some more information about why spaces instead of tabs and how to change this in GIT? Maybe some links?

About the patch:

A standard Drupal form has no specific selectors apart from the ID. If you repeat that one form several times in the same page, Drupal will give each form a different ID (e.g. user-login--1, user-login--2 etc). That was the reason to add this. But using jqueyr selectors do make more sense and is probably easier to make. give me a second to redo the patch...

nuez’s picture

Status: Needs work » Needs review
StatusFileSize
new4.52 KB

New patch...

Not sure whether the wildcard or jquery selector is the best option: The module matches the selector against the form properties. So the ID is matched against $form['id'] and the class against $form['attributes']['class']. To allow all the jquery selectors (with one or more elements) would mean a complete rewrite of the module.

This patch makes selecting by classes (added to the $form['attributes'] and Ids possible. I leave it to the module admins to decide which one is the best option.

yannickoo’s picture

Status: Needs review » Needs work

Thank you for taking time again and working on the patch :) Just reviewed it and found some coding standards issues. Please take a look at the coding standards. You can change the indentation style in your editor and coding standards say that you should use two spaces instead of a tab.

+++ b/compact_forms.admin.incundefined
@@ -15,7 +15,7 @@ function compact_forms_admin_form($form, &$form_state) {
+    '#description' => t('Enter ID�s or classes of the forms you want to display compact, e.g. <em>#user-login-form</em>, or <em>.user-form</em>. One per line.'),

Oh, strange character here.

+++ b/compact_forms.jsundefined
@@ -81,7 +81,8 @@ Drupal.behaviors.compactForms = {
+    $(selectors.join(','), context).compactForm(settings.compactForms.stars);

Do we really have to join that? We can also pass the whole selectors string.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+  $apply = false;

Coding standards says that you have to uppercase booleans like TRUE and FALSE.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+  foreach($selectors as $key => $selector){

Coding standards says that you have to put a space after an if or foreach.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+    $selector_type = (substr($selector , 0 ,1));

Why there are brackets around substr?

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+    $selector_value = substr($selector,1);

A missing space before the 1.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+        $apply = true;

Uppercase the boolean.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+        $apply = true;

Uppercase the boolean.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+  if ($apply == true) {

if ($apply) is enough.

nuez’s picture

Status: Needs work » Needs review
StatusFileSize
new4.58 KB

Thanks for the feedback, really appreciate it.

All the issues you´ve mentionted have been fixed. The jquery selector issue: haven´t changed that, because that´s the way it was done originally: the selectors are passed as a javascript array, they need to be joined into a string.
Since it was done like this in the original module, I changed it as little as possible,

Adding the patch once again,

yannickoo’s picture

Status: Needs review » Needs work

Sorry but there are minor things...

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+    $form_ids[] = strtr($id, array('-' => '_'));

Why did you change this line?

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+    if ($selector_type == "#") {

Please use single quotes instead of double quotes, forgot that.

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+      if ($form['form_id']['#value'] == $form_id || $form['#id'] || $form['#id'] == $selector_value) {

Why do you check for $form['#id']?

+++ b/compact_forms.moduleundefined
@@ -55,34 +55,50 @@ function compact_forms_form_alter(&$form, $form_state, $form_id) {
+    if ($selector_type == ".") {

Please use single quotes instead of double quotes, forgot that.

yannickoo’s picture

Warning: substr() expects parameter 2 to be long, array given in compact_forms_pre_render() (line 82 of compact_forms.module

Also forgot to mention that I get a message because of $form_id = substr($selector_value, array('-' => '_'));. You mean str_replace instead of substr right?

nuez’s picture

Status: Needs work » Needs review

Allright:) thanks for the feedback again,

about #11: in the original code de strtr was used, don´t know why I changed that, but changed it into str_replace now. Patch looks clean now,

nuez’s picture

StatusFileSize
new4.45 KB

the patch

nuez’s picture

StatusFileSize
new4.45 KB

And another small fix,

esmilia’s picture

I used patch #13 but i dont know how it works. What shall i add to the textarea of compact forms to use it for all webform-client-form ?

yannickoo’s picture

You can use Compact Forms for all Webforms with just adding .webform-client-form into the textarea :)

calebyoder’s picture

Issue summary: View changes

Hmm.. This patch didn't apply right for me. :(

It says:

patching file compact_forms.admin.inc
Hunk #1 succeeded at 16 (offset 1 line).
patching file compact_forms.js
Hunk #1 succeeded at 82 (offset 1 line).
patching file compact_forms.module
Hunk #1 FAILED at 55.
Hunk #2 succeeded at 82 (offset -8 lines).
1 out of 2 hunks FAILED -- saving rejects to file compact_forms.module.rej

This feature is something that I would like! I'm having issues with a Views exposed form in a block. The problem occurs when I place the block into the mmenu (mobile menu) and they both have the same ID.