CVS edit link for kostajh

I would like to contribute the attached Dynamic Select module, which allows users to add a component to the Webform 3.x module.

Users have long requested the ability to dynamically populate a select list in Webform (see http://drupal.org/node/151603; there are several other issues in the Webform issue queue about this). This would allow users to create select lists populated with, for example, upcoming events. @Bartezz created a patch to Webform 2.x that modified the Webform core module, and provided a new Dynamic Select component. This patch was never committed to the 2.x branch as the maintainer of the project created an API for Webform 3.x that allows third-party components in Webform (http://drupal.org/node/525446). Webform's maintainer @quicksketch has indicated that @Bartezz's patch will not be committed to 2.x branch as development has ceased on Webform 2.x.

Myself and @gloryfish updated the code written by @Bartezz to utilize the Webform 3.x API. Our module provides a new component, Dynamic Select, to Webform 3.x. The "Options" field in the Dynamic Select component allows a user to populate the select list via PHP code. The code must return an associative array; the values in the array are what will be saved in the Webform results table.

An example is provided in the README of how to populate the select list with a View, which will likely be the most popular use case for this module.

@gloryfish (http://drupal.org/user/281675) and @Bartezz (http://drupal.org/user/284019) will also be applying for CVS accounts to co-maintain this module.

Please let me know if you have any questions.

Thanks!

Kosta

CommentFileSizeAuthor
#7 dynamicselect.tar_.gz6.04 KBkostajh
#1 dynamic_select.tar_.gz6.05 KBkostajh
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kostajh’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
6.05 KB
GloryFish’s picture

I would like to be a co-maintainer of this module.

Bartezz’s picture

I would like to be a co-maintainer of this module.

apaderno’s picture

Status: Needs review » Needs work
Issue tags: +Module review

Hello, and thanks for applying for a CVS account. I am adding the review tags, and some volunteers will review your code, pointing out what needs to be changed.

As per http://drupal.org/cvs-application/requirements, the motivation message should be expanded to contain more details about the features of the proposed module, and it should include also a comparison with the existing solutions.
As you are referring to an existing project, why didn't you open a feature request for the existing project?

CVS applications are for single users.

kostajh’s picture

Hi Kiam,

Thanks for the reply.

As per http://drupal.org/cvs-application/requirements, the motivation message should be expanded to contain more details about the features of the proposed module

The module has one feature: it provides a new component, Dynamic Select, to the Webform 3.x module. To use it, you enable the Dynamic Select module. Create a Webform or edit the Webform to which you wish to add a dynamic select component. Under "Type" you should now see a component called "Dynamic Select". Enter a component name and click "Add". All fields are similar to what you are provided with in the other Webform component options. The exception is the "Options" field. You can enter PHP code in this field. Your code must return an associative array; the values in the array are what will be saved in the Webform results table. In all other respects, the component works like the regular Webform Select component. Administrators will have to be wise about how they grant permissions for users to create/edit Webforms if they are using the Dynamic Select module; I have made this clear in the README.

and it should include also a comparison with the existing solutions.

There is no existing solution. There is however a lot of demand for this functionality. As noted in the original motivation message, Webform has been updated in 3.x to provide an API for other modules to provide components. This module does just that.

As you are referring to an existing project, why didn't you open a feature request for the existing project?

I requested this to be included in the Webform 2.x branch, but the maintainer had good reasons to not want to include this since development is focused on Webform 3.x. To quote the maintainer of Webform: "the module provided in this issue will never be part of the Webform project itself." #151603: Can I put options in a select field from a database query? The Webform 3.x API was created for the specific purpose of allowing developers to extend Webform's components via modules, rather than adding feature requests to the core Webform module. Hence this module, rather than a patch Webform 3.x, which would not be approved for "by design" reasons.

CVS applications are for single users.

I understand that, but if the application is approved, I would like to add @gloryfish and @bartezz as co-maintainers.

Thanks for your help.

apaderno’s picture

Status: Needs work » Needs review

I understand that, but if the application is approved, I would like to add @gloryfish and @bartezz as co-maintainers.

If they don't apply for a CVS account, they cannot get one. They should wait your CVS account is approved, and then apply for one. It is not possible to get a CVS account if not following the steps you followed; that is the only way for the tab user/<nid>/edit/cvs to appear, which is the one that allow to approve a CVS account, or disable it.

kostajh’s picture

FileSize
6.04 KB

Per the recommendation in Pro Drupal Development ('Development Best Practices'), I removed the underscore from the module name. Updated code is attached.

I look forward to the code review. Thanks!

luthien’s picture

Hello, I installed the dynamic select module in our production site and it is working perfectly fine. I used the module to populate a drop down select with content generated by querying an external database. I'm surprise this module is not part of the core of the webforms module. For users like me, that need to combine Drupal forms with external databases fields, it is a very important module to have.

quicksketch’s picture

Since I've removed all custom PHP functionality from Webform itself (this stuff is dangerous folks), if kostajh wants to maintain http://drupal.org/project/webform_php as well once his application is approved, that would be fine by me.

apaderno’s picture

Thank you, quicksketch, for chiming in. Let's see what kostajh replies.

kostajh’s picture

Hi quicksketch & kiam,

In the original issue #151603: Can I put options in a select field from a database query? that prompted the development of this module, quicksketch suggested yesterday that instead of a new module I consider extending webform_php to achieve the functionality that exists in the Dynamic Select module.

My inclination is to not proceed with that route for the following reasons:

  1. The Dynamic Select module doesn't depend on any of the custom PHP functionality in Webform, so it's not affected by #754580: Remove Custom PHP Execution (Additional Processing/Validation) Fields. Just to be sure, I updated to the latest 6.x-3.x-dev of Webform via CVS and tested DynamicSelect, it still works fine.
  2. The scope of http://drupal.org/project/webform_php and Dynamic Select module are currently quite different. webform_php is for additional processing/validation of a Webform during the submission process; Dynamic Select is used simply to populate a select list. Dynamic Select does not care about additional processing of the form after submission, nor should it, in my opinion.
  3. It's true that webform_php could be extended and modified to work in the way that Dynamic Select does. And I understand the argument to provide only one module that allows users to input PHP to manipulate a Webform. But I am not convinced this is desirable, because Webform PHP and Dynamic Select satisfy two very different use cases. I think the functionality should be kept separate.
  4. The Dynamic Select module exists, has been tested, and works with Webform 3.x.

Those are my thoughts. Let me know if you have any more questions. Also, quicksketch feel free to contact me via my contact form if you'd like to discuss further outside of the issue queue, and thanks for contributing your thoughts.

quicksketch’s picture

There are two kinds of people that make Drupal sites: Those that type code into textareas and those that don't. Since I'm of the latter group I don't really care how this ends up happening, I'll let those other people install Contemplate, PHP filter, CSS Injector, Dynamic Select, Webform PHP, and whatever other unholy modules they want to install (might as well throw in Hooker).

It's true the two modules accomplish different things, it's just likely that a user of one would also be a user of the other. They both add PHP textareas to Webform, just in different places, making the grouping of them seem like a logical thing to do.

GloryFish’s picture

@quicksketch I am also of the latter group. The concept of executing arbitrary user-supplied code within the same context as trusted code is contrary to all good sense. it is, as you say, unholy.

That being said, PHP execution isn't going away. There ARE (shudder) people who use it. Likewise, there are people now who want to populate a webform select programmatically. This module provides that functionality. Adding the functionality to webform_php might be a great idea but it hasn't been done yet.

I'm new to this aspect of Drupal but wouldn't it make sense to make this module available now, rather than waiting for a whole new chunk of functionality to be developed? Or, put another way, which is more important: providing all potentially-related functionality in a single module, or getting functionality that works into users hands in a timely fashion?

If at some future time someone extends webform_php to handle selects then this module would most likely be deprecated, but right now that isn't the case.

quicksketch’s picture

Well I shrug. I think putting in webform_php would be the "right" way. This approach from the start is dubious (duplicating 90% code of an existing Webform component). But again, I don't really care how this happens so long as it doesn't cause more support requests in the Webform queue. Maybe I was just looking for someone to maintain webform_php, because I sure don't want to. :P

luthien’s picture

considering I'm one of those users that needs to populate fields from custom PHP code, what will be the correct way of populating a hidden text field with custom code? I'm using the dynamic select already to populate the select box, now I need a few hidden fields with values coming from an external database. I don't need to validate fields, just to use the hidden fields to build the confirmation emails. I also need the "add another" textfield functionality but that one is not as important as populating fields with custom code. Thanks.

Bartezz’s picture

Well I suppose you could use dynamic select as your hidden field... just have one option and hide it via css... not the best solution but one you could implement right now untill better solutions are available...

Add another is a bigger issue... and one that doesn't belong here. Have a look here: http://drupal.org/user/284019

Cheers,
Bartezz

GloryFish’s picture

It would be useful to keep discussion in this (3 week old) thread limited to whether or not kostajh's cvs account request will be approved. Is there anything we can be doing to help with that decision?

luthien’s picture

Hi Bartezz, the solution you proposed is the one I was using because the dynamic select was the only available tool to achieve my goals. As I mentioned in a previous post, the dynamic select module is working and it is working fine, so count my vote towards the approval of kostajh's request please.

roball’s picture

If the suggested module will be approved and depends on webform.module, I suggest to rename it to "webform_dynamicselect" or another name starting with "webform_".

kostajh’s picture

@roball - I agree.

Kiam, is there anything else you need from me for the code to be reviewed?

roball’s picture

Status: Needs review » Needs work

Just gave it a try. Some remarks:

  • The version number cannot be "1.x-dev". Should be changed to "6.x-1.x-dev"
  • The attempt how the module plugs to its mother module (Webform) is obsolete. Instead of copying most code of an old dev version of Webform (6.x-3.x-beta2 or older) and building a modified version of the original "select" component, look how Webform's built-in "select" component has been expanded since 6.x-3.0-beta3: #406486: Allow pluggable select list values. All built-in select fields now have a "Load a pre-built option list" drop-down menu - make use of this new feature, maybe by adding an option "Populated by custom PHP code".
  • Also re-read quicksketch's comment at http://drupal.org/node/151603#comment-2787248: "This makes the dynamic select module largely unnecessary, because there is now a hook in the normal select for populating lists. kostajh, rather than starting a new module, it would be really great if you could extend the split-off module http://drupal.org/project/webform_php instead, making it a one-stop-shop for people that want to type PHP code into textareas."
  • Your module's ReadMe states: "IMPORTANT: anyone who has permissions to create or edit a Webform has permissions to use this component, and consequently run arbitrary PHP code on your site.". This seems too dangerous. webform_php.module introduced a permission "use PHP for additional processing" - I think your approach should look similar.
luthien’s picture

Hello kostajh, I started a new topic and it is related with your module and webform. Could you please look at the post? perhaps you know the answer. Thanks.

http://drupal.org/node/769302#comment-2838324

I also noticed that values selected using the dynamic select are not being displayed in the web form reports (webform-results/table). The field is empty for the report.

apaderno’s picture

Status: Needs work » Closed (won't fix)
Bartezz’s picture

Won't fix? So no CVS account for kostajh?

Cheers

apaderno’s picture

If kostajh wants a CVS account, he can re-apply to get one.

vasanthkay’s picture

Component: Miscellaneous » miscellaneous

The dynamic select module created by kostajh is very useful... I used it in one of the form I created.
As luthien had pointed out above there is an issue wrt report generated from the webform. The dynamic select field is empty in it.
Also, I noticed one more issue wherein, doing validations for the dynamic select field with "webform validation" module is not possible. It does not detect the dynamic select option as one of the fields in the form.

Has anyone experienced the same problems ? Any ideas or alternate solutions to overcome these two problems would be highly helpful to me and the community as well.

kostajh’s picture

@vasanthkay: check out hook_webform_select_options() in the webform_hooks.php file in Webform 3.x.

vasanthkay’s picture

@kostajh
I see that hook_webform_select_options_info() has implemented a pre-defined list for days of the week and whose values are hardcoded in the webform.options.inc file.
There is one more pre-defined list which creates a list of countries.

I had used your #7 module and entered a query in it to dynamically load values from database and present it in the form.
So, in order for these values to appear in the report, could you give me some more pointers ?
I see that webform_get_submission() in webform.submission.inc retrieves the submission data and webform_results_download in webform.report.in creates the report.

I am not sure if I misunderstood what you had mentioned in the previous reply. I am quite new to Drupal and hooks implementations. Could you please help me out with some more pointers.

luthien’s picture

due to the problem with the reports, for one of the dynamic web forms, I started using the http://drupal.org/project/webform_report module to generate the reports. It worked out well as I was able to generate the reports and I added some filters too.

vasanthkay’s picture

@luthien
Thank you so much luthien and kostajh. As suggested, I used "webform report" module. Now, the reports are generated correctly with the dynamic select values included in the excel rows.

There is still one more challenge which I am facing,
The dynamicselect option is not listed by the "Webform Validation" module and hence I am not able to validate the selections done by the users.
The form which I am implementing right now, would be complete if I am done with this last part.

Could you share with me your ideas/suggestion regarding how this can be implemented ? or has any of you implemented this in some other way other than editing code in "Webform validation" module. ?

vasanthkay’s picture

@kostajh, luthien
I found that it is possible to validate the dynamic select options using "Webform validation" module, by including "dynamicselect" in the code wherever it checks for component types to validate and then using one of the validation rules.

By editing this code I was able to get it almost working. However there is still one small issue, which I would like to discuss with you. In a conventional select field, the default select box displays a test "- Select -" in the select box below which all the other options for selection are present.

The validation rules does not validate for this text ( - Select - ) as one of options. It just checks if any of the options have been selected in case its a mandatory field.

Whereas in a dynamic select option, there is no (" - Select - ") default option.
Hence, it display one of the dynamic options by itself as a default value. Hence, all the values in the drop down box EXCEPT this 1st value gets validated.

Is there a way by which the default ("-select-") field can be added to every dynamic select box ?
By adding this all the values in the dynamic select box gets validated and it completes the functionality too.

roball’s picture

It seems that the Webform View Reference Component module is doing exactly what the proposed module here is supposed to do.

luthien’s picture

check out the above module, it might works for what you need. I used the hook_webform_select_options() to create my dynamic drop down fields and I'm not doing validation because I don't needed for this particular field.

See example below:

<?php
function mywebform_webform_select_options_info() {
  $items = array();
  
  $items['yesno'] = array(
    'title' => t('Yes/No List'),
    'options callback' => 'webform_options_yesno',
    'file' => 'includes/webform.options.inc',
  );
  
  $items['housing_prog'] = array(
    'title' => t('Housing - Program Names'),
    'options callback' => 'webform_options_housing_programs',
    'file' => 'includes/webform.options.inc',
  );
  return $items;
}

/**
 * Option list containing Yes/No.
 */
function webform_options_yesno() {
  $yesno = array(
    'yes' => t('Yes'),
    'no' => t('No'),
  );
 return $yesno;
}

/**
 * Generate the drop down box with options from an external database.
 */
function webform_options_housing_programs() {

db_set_active('mydb');
$q = 'SELECT * FROM ... add your query here'; 
$result = db_query($q);
$housing_prog =array();
$housing_prog[''] = "Please select one";
$q = 'SELECT count(*) FROM ... the rest of your query here';
$itemnum = db_result(db_query($q));
db_set_active('default');
if($itemnum > 0) { 
	while ($appObj = db_fetch_object($result)) { 
        for($x = 0; $x < $itemnum; $x++)
		{ 
			$housing_prog[$appObj->acronym] = $appObj->name; 
		} 
	}
}
return $housing_prog;
}
?>

what I would like to do is to display another dynamic drop down box based on the selection of the first dynamic drop down box. The validation will be done based on the first selected value. If you get it to work please let me know. In my particular case, the drop down boxes changes based on dates calculated from today's date. It is kind of volatile-dynamic boxes as the values are never the same.

apaderno’s picture

Issue summary: View changes

Please read the following links as this is very important information about CVS applications.

Drupal.org has moved from CVS to Git! This is a very significant change for the Drupal community and for these applications. Please read Migrating from CVS Applications to (Git) Full Project Applications and Applying for permission to opt into security advisory coverage on how this affects and benefits you and the application process. In short, every user has now the permissions necessary to create new projects, but they need to apply for opt into security advisory coverage. Without applying, the projects will have a warning on projects that says:

This project is not covered by Drupal’s security advisory policy.

apaderno’s picture

Component: miscellaneous » new project application
Status: Closed (won't fix) » Closed (duplicate)
Related issues: +#981312: kostajh [kostajh]