When webform is overridden and we revert the feature it creates a new webform and the feature remains overridden.

Comments

miteshmap created an issue. See original summary.

mehul.gada’s picture

We faced similar issue when featuring existing webform for one of the application. On each feature revert, a new webform was getting created. On troubleshooting, we found that the webform itself was not correctly created. It had many components with duplicate machine name. So, we first deleted the duplicate webforms (those created by feature revert), and then manually corrected machine name of each webform component for original webform in database, so that each webform component has unique machine name. Once done, we cleared drupal cache and regenerated feature. This feature worked fine when we reverted it in other environment and no duplicate webform was created. Please note that machine name correction is required in both the environment (one where you will regenerate feature and one where you will revert feature.)

joseph.olstad’s picture

Category: Bug report » Support request
Status: Active » Reviewed & tested by the community

had exactly same issue as described and indeed comment #2 is correct about this. the webform table rows must have a machine_name. For some reason in my case there was none there.

if you take the machine names from the duplicate webforms and correspond those to the pre-existing ones that have no machine name, you'll find out which machine names to use for which row.

SELECT nid, confirmation, status, machine_name FROM webform WHERE nid = 12345 OR nid = 54321 ;

assuming 12345 is the webform nid you want to fix
assuming 54321 is the new duplicated webform that you just want to find out which machine name to use then delete that webform.

To fix manually:
UPDATE webform SET machine_name = 'example_machine_name_check_new_duplicate_webforms_for_name' WHERE nid = 12345 AND machine_name ='';

in my custom feature I added this to the cool_feature_with_forms.install file:

/**
 * Fix broken machine names for webforms so that we can correctly feature revert the coolstuff feature changes.
 */
function coolstuff_update_7000 () {
  _webform_fixing(12345, 'contact_us');
  _webform_fixing(12346, 'request_example1');
  _webform_fixing(12347, 'subscribe_to_our_stuff');
  _webform_fixing(12348, 'request_example2');
  _webform_fixing(12349, 'feedback');

  _webform_fixing_component(12345, 'contact_us'');
  _webform_fixing_component(12346, 'request_example1');
  _webform_fixing_component(12347, 'subscribe_to_our_stuff');
  _webform_fixing_component(12348, 'request_example2');
  _webform_fixing_component(12349, 'feedback');
}

/*
 * Function to add a correct machine name for the webform at $nid
 */
function _webform_fixing($nid, $machine_name_new) {
  $num_updated = db_update('webform')
     ->fields( array(
       'machine_name' => $machine_name_new,
     ))
     ->condition('nid', $nid, '=')
     ->condition('machine_name', '', '=')
     ->execute();
  drupal_set_message("Set machine name for webform nid $nid to $machine_name_new from ''", 'success', TRUE);
}

/*
 * Function to add a correct machine name for the webform at $nid
 */
function _webform_fixing_component($nid, $machine_name_new) {
  $total_updated = 0;
  $query = db_query("SELECT nid, form_key from {webform_component} where nid=$nid");
  $result = $query->fetchAll();
  foreach ($result as $webform_component) {
    $form_key = $webform_component->form_key;
    $num_updated = db_update('webform_component')
       ->fields( array(
         'machine_name' => $machine_name_new . '__' . $form_key,
       ))
       ->condition('nid', $nid, '=')
       ->condition('form_key', $form_key, '=')
       ->condition('machine_name', '', '=')
       ->execute();
    if ($num_updated >= 1) {
      $total_updated = $total_updated + $num_updated;
      drupal_set_message("Set machine name for webform_component nid $nid form_key $form_key to $machine_name_new" . '__' . "$form_key from ''", 'success', TRUE);
    }
  }
  if ($total_updated >= 1) {
    drupal_set_message("Total number of machine_name updates for webform_component nid $nid performed $total_updated time(s).", 'success', TRUE);
  }
  else {
    drupal_set_message("machine name for webform_component nid $nid was NOT changed.", 'error', TRUE);
  }
}

Once you've added the hook_update then go to update.php and it'll roll, or else do drush updb

vensires’s picture

Status: Reviewed & tested by the community » Needs work

Even though the solution in #3 may work, it needs a developer to customize things and know what he's doing since it's relevant to each webform. It also requires the developer to create a new update hook each time the feature gets regenerated.

aubjr_drupal’s picture