I'm trying to add a custom property (simple checkbox) to be admin controlled when editing a webform component, i.e. an extra setting for webform components. Using hook_form_builder_properties() to add the property works fine, as does adding it onto new custom webform components in _form_builder_webform_form_builder_map_CUSTOM_COMPONENT(). The property is displayed, mapped (to the serialized extra column) and saved correctly.

However when trying to add that same new property onto an existing webform component (not a custom component this time) saving just doesn't work. I've added the property onto other components in hook_form_builder_types_alter() and everything is fine apart from saving the value.

If I alter _form_builder_webform_property_map() to manually include the new property (and it's storage_parents) it saves fine. It appears _form_builder_webform_property_map() doesn't take into account the alterations to components made in fs_webform_form_builder_types_alter() and thus ignores custom/altered mappings.

Am I missing something simple here?

CommentFileSizeAuthor
#2 1569850-2-webform_property_map-d7.patch533 bytesfenstrat

Comments

quicksketch’s picture

It appears _form_builder_webform_property_map() doesn't take into account the alterations to components made in fs_webform_form_builder_types_alter() and thus ignores custom/altered mappings.

Indeed, this is the case. The _form_builder_webform_property_map() function is the source of information that is put into hook_form_builder_types(), not the other way around. I don't think modifying the types through hook_form_builder_types_alter() would have enough information to function properly. Perhaps we need a new hook for what you're trying to accomplish?

fenstrat’s picture

Category: support » feature
Status: Active » Needs review
StatusFileSize
new533 bytes

Thanks for clearing that up @quicksketch.

How about this? Too simple?

It certainly achieves what I'm trying to do, i.e. add and save custom properties on webform components.

quicksketch’s picture

Seems like that's the ticket, though I have to lament the length of our function names. My fault for "form_builder_webform_*" prefixing.

versantus.nik’s picture

I had exactly the same problem, and the patch works perfectly for me. Thanks @fenstrat

versantus.nik’s picture

hmm... I was a bit premature. With this patch the data is being saved correctly, but I can't figure out how to load it back, and so when I view the formbuilder edit screen by new property field is empty

My extra data is used in various components, but taking a "grid" as an example, the data is saved in the $element['#webform_component']['extra']['mydata'] field, but the only way I have found to retrieve it is to add in code into
_form_builder_webform_form_builder_load_grid($element) function:

function _form_builder_webform_form_builder_load_grid($element) {
  // Convert properties used only in Form Builder.
  $element['#custom_grid_option_keys'] = !empty($element['#webform_component']['extra']['custom_option_keys']);
  $element['#custom_grid_question_keys'] = !empty($element['#webform_component']['extra']['custom_question_keys']);

  // added
  $element['#mydata'] = $element['#webform_component']['extra']['mydata'];

  return $element;
}

Is there already hook I should be implementing to load this extra data in the right way?

versantus.nik’s picture

hmm... I was a bit premature. With this patch the data is being saved correctly, but I can't figure out how to load it back, and so when I view the formbuilder edit screen by new property field is empty

My extra data is used in various components, but taking a "grid" as an example, the data is saved in the $element['#webform_component']['extra']['mydata'] field, but the only way I have found to retrieve it is to add in code into
_form_builder_webform_form_builder_load_grid($element) function:

function _form_builder_webform_form_builder_load_grid($element) {
  // Convert properties used only in Form Builder.
  $element['#custom_grid_option_keys'] = !empty($element['#webform_component']['extra']['custom_option_keys']);
  $element['#custom_grid_question_keys'] = !empty($element['#webform_component']['extra']['custom_question_keys']);

  // added
  $element['#mydata'] = $element['#webform_component']['extra']['mydata'];

  return $element;
}

Is there already hook I should be implementing to load this extra data in the right way?

fenstrat’s picture

@versantus have you implemented hook_form_builder_properties() to tell form builder about your form to edit your custom property?

/**
 * Implementation of hook_form_builder_properties().
 */
function mymodule_form_builder_properties($form_type) {
  return array(
    'newproperty' => array(
      'form' => 'mymodule_property_newproperty_form',
    ),
  );
}

/**
 * Configuration form for the "newproperty" property.
 */
function mymodule_property_newproperty_form(&$form_state, $form_type, $element, $property) {
  $form = array();

  $form['newproperty'] = array(
    '#type' => 'textfield',
    '#title' => t('New property'),
    '#default_value' => $element['#prefilled'],
  );

  return $form;
}

In my implementation the '#default_value' => $element['#prefilled'] doesn't work with checkboxes, however it does for other fields. See how it goes for you.

fenstrat’s picture

Status: Needs review » Reviewed & tested by the community

Marking RTBC as per #3.

quicksketch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks @fenstrat! Committed in preparation for the 1.1 release. Appreciate your help!

fenstrat’s picture

Marvellous, thanks @quicksketch, great to see the 1.1 release.

p.s. Notice you're not using the Git attribution in your commits, something to consider as it's nice kickback for other contributors.

Status: Fixed » Closed (fixed)

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

sk2013’s picture

We also facing the problem above.... We have a custom property which contains a check box.. The value of the check box is saved, but while editing the form the check box is unchecked...

Please, Any help would be grateful.

fenstrat’s picture

@sk2013 I've also noticed issues with custom properties being reset when loaded for edit, but have not had any time to look further into it yet.

Could you please start a new issue here in the form_builder issue queue, add the details of how you can replicate the problem. If you link to it from here I'll look into it when I get a moment. Thanks.

sk2013’s picture

We have created a new issue for the custom property issue. The link is Custom properties being reset when loaded for edit