Hi,

firstable, thanks for the huge job done on this module which is absolutely great and must have feature for Drupal.

I try to customize the element's configuration forms by hiding the fields I do not need. I can do it for basically all the desired fields/details components, but unfortunately, I have an issue with the element attributes (classes, style and element custom attribute (YAML)) form element.

I would like to alter the display of this component by hiding the style textfield, but it's impossible to target it through the following hooks :

- hook_form_webform_ui_element_form_alter
- hook_preprocess_form_element
- hook_preprocess_webform_element_attributes

I can see the form Element add his own Preprocess methods inside the form element itself, so maybe it is not "hookable", or I'm just missing something.

Tank you in advance for your help.

Comments

grumpy74 created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review

Use the below code to hide elements from global configuration and element settings.

 * Implements hook_form_FORM_ID_alter().
 *
 * @see /admin/structure/webform/manage/{webform}/settings
 */
function CUSTOM_MODULE_form_webform_settings_form_alter(array &$form, FormStateInterface $form_state) {
  $elements = WebformFormHelper::flattenElements($form);

  $settings = [
    // Validation is disabled via the theme.
    'form_novalidate',
    // Details toggle should never be display.
    'form_details_toggle',
    // Don't allow an attributes to customized since this is not supported by
    // the current theme.
    'form_attributes',
    'form_submit_attributes',
    'wizard_prev_button_attributes',
    'wizard_next_button_attributes',
    'draft_button_attributes',
    'confirmation_attributes',
    'confirmation_back_attributes',
  ];
  foreach ($settings as $setting) {
    $elements[$setting]['#access'] = FALSE;
  }

  // DEBUG: List all settings.
  // print '<pre>' . \Drupal\Component\Utility\Variable::export(array_keys($elements)) . '<pre>'; exit;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see admin/structure/webform/manage/{webform}/element/{element_key}/edit
 */
function CUSTOM_MODULE_form_webform_ui_element_form_alter(array &$form, FormStateInterface $form_state) {
  $elements = WebformFormHelper::flattenElements($form);

  $properties = array(
    // Disable flexbox support since it is not supported by
    // the current theme.
    'flex',
  );
  foreach ($properties as $property) {
    if (isset($elements[$property])) {
      $elements[$property]['#access'] = FALSE;
    }
  }

  // DEBUG: List all properties.
  // print '<pre>' . \Drupal\Component\Utility\Variable::export(array_keys($elements)) . '<pre>'; exit;
}
grumpydev74’s picture

Thank you @jrockowitz, but I already tried that solution from the documentation and it does not allow to do what I want.

This way, I can't hide only the element attributes style field as the webform element because it is created in is own preprocess Method from it's own class. So I can't reach the sub keys through the hooks given in the exemple.

Obviously I'm able to hide the complete fieldset "element attributes", but it's not my goal here, I just want to hide the field "element attributes style" included in this fieldset.

Finally, I succeeded to do it using the hook_preprocess_form_element, and by adding the hidden class on the desired elements. But I'm still assuming I could be better ti be able to manage fields visibility from the hook_form_webform_ui_element_form_alter to keep a consistency. Just my opinion by the way.

jrockowitz’s picture

I think I will need to add support for '#attributes__access': FALSE.

An immediate workaround is to add a '#process' hook and alter the $element['#attributes'];

jrockowitz’s picture

I just reviewed \Drupal\webform\Element\WebformElementAttributes and something like $element['wrapper_attributes']['#attributes__access'] = FALSE; should work as expected.

grumpydev74’s picture

Thank you @jrockowitz, this quick fix will do the trick.

jrockowitz’s picture

Status: Needs review » Fixed

I think that is the long term solution.

Status: Fixed » Closed (fixed)

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