Hi:

Thank you so much for giving us this wonderful module.

I run into a little issue, please help me a little bit.
I have a site which allow normal users to create a node attached to a webform.
I don't want them to see the template selection field, so I hide it and set the '#default_value' => '1'.
It works fine when users create a node, but when user edit the node, the Webform Submission access checkbox will become empty instead of the one I had. If I set the template selection to none, it will not overwrite the Submission access.

Is there a way that I add add a little code before I set '#default_value' => '1'

Something like If (add new) '#default_value' => '1'; elseif (edit) '#default_value' => '0'

Thank you very much~!

Comments

rv0’s picture

Status: Active » Fixed

There is some functionality in the module that will help you with hiding the selection field.. It's not very flexible, but it works:
From webform_template.api.php:

/**
 * Modify the visibility of the template selection.
 *
 * @param bool $show
 *  When set to FALSE, the template selection will be deactivated.
 * @param Array $context
 *  The form.
 */
function hook_webform_template_show_selection_alter(&$show, $context) {
  // @todo Add sample code.
}

Below this code adapted to your usecase

/**
 * Implements hook_webform_template_show_selection_alter().
 *
 * Do not show the template form on edit screen.
 */
function YOURMODULE_webform_template_show_selection_alter(&$show, $context) {
  $form = $context;
  if (isset($form['#node']->nid)) {
    $show = FALSE;
  }
}

Replace YOURMODULE by whatever name you gave your module.
Another approach will work too, but this seems most elegant.
You can also use the if (isset($form['#node']->nid)) { in your current form_alter code (that is where you are setting the default, right?)

jams1988’s picture

Thank you rv0:

My problem is not hiding the form, it is to set different default_value of template selections.
Sorry, I am using the 7.12, not the dev version.
When user create a node, the default_value is 1
when user edit a node, default_value is 0

There the code I try in webform_template.module:

if ($op == 'create') {
    $form['webform_template']['source'] = array(
      '#type' => 'select',
      '#title' => t('Templates'),
      '#options' => $templates,
      '#default_value' => '1',
      '#description' => t('Pick a template if you want to attach a webform with predefined fields.'),
    );
    }
    else if ($op == 'update') {
   $form['webform_template']['source'] = array(
      '#type' => 'select',
      '#title' => t('Templates'),
      '#options' => $templates,
      '#default_value' => 'none',
      '#description' => t('Pick a template if you want to attach a webform with predefined fields.'),
    );
    }

It didn't work because the if statement didn't go through..
Thanks for the quick reply......

The reason why I want to change the default_value is that if it is '0' wheh user edit a node, it will not trigger the
db_delete('webform')->condition('nid', $node->nid)->execute();
db_delete('webform_component')->condition('nid', $node->nid)->execute();
db_delete('webform_emails')->condition('nid', $node->nid)->execute();
db_delete('webform_roles')->condition('nid', $node->nid)->execute();
db_delete('webform_submissions')->condition('nid', $node->nid)->execute();
db_delete('webform_submitted_data')->condition('nid', $node->nid)->execute();

And it will still keep the webform attributes of the node, but change the node itself....

rv0’s picture

@jams1988

never change the module code itself unless you plan on extending the functionality/providing a patch

The code you have there cannot work for many reasons (no $op variable, syntax errors, ..)

Make a custom module (there's plenty of info on how to do that on this site)
Use hook_form_alter to modify the #default_value property
The module will need a higher "module weight" to make sure it runs after webform_template, to do this see the code in webform_template.install.php

Alternatively, someone could write a patch for webform_template to add a config screen to the template field, where setting a default value is made possible. I currently dont have the time/need to provide this, but will review any patches for this.

rv0’s picture

@jams1988

never change the module code itself unless you plan on extending the functionality/providing a patch

The code you have there cannot work for many reasons (no $op variable, syntax errors, ..)

Make a custom module (there's plenty of info on how to do that on this site)
Use hook_form_alter to modify the #default_value property
The module will need a higher "module weight" to make sure it runs after webform_template, to do this see the code in webform_template.install.php

Alternatively, someone could write a patch for webform_template to add a config screen to the template field, where setting a default value is made possible. I currently dont have the time/need to provide this, but will review any patches for this.

rv0’s picture

@jams1988

never change the module code itself unless you plan on extending the functionality/providing a patch

The code you have there cannot work for many reasons (no $op variable, syntax errors, ..)

Make a custom module (there's plenty of info on how to do that on this site)
Use hook_form_alter to modify the #default_value property
The module will need a higher "module weight" to make sure it runs after webform_template, to do this see the code in webform_template.install.php

Alternatively, someone could write a patch for webform_template to add a config screen to the template field, where setting a default value is made possible. I currently dont have the time/need to provide this, but will review any patches for this.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

little more