If I add a file field to a webform and my mytheme_form_element() checks for $element['#type'] to add some classes to specific types I get a notice

Notice: Undefined index: #type in mytheme_form_element_label() (line 430 of sites\all\themes\mytheme\theme.form.inc).

All other fields have the type defined.

Repro case:

  1. Create web form
  2. Add file field and press Add.
  3. Save
  4. Notice, node/xxx/webform/components/y?destination=node/xx/webform

Repro case 2:

  1. Edit
  2. Notice, node/xxx/webform/components/y?destination=node/xx/webform

I do not think that I need to expect that ['#type'] is undefined. Please make sure every form element has type defined.

CommentFileSizeAuthor
#1 2013-06-26_233118.png36.16 KBhass
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hass’s picture

FileSize
36.16 KB
hass’s picture

Version: 7.x-3.9 » 7.x-3.19
quicksketch’s picture

Priority: Major » Minor

Thanks, yeah this element is kind of weird. It's not really worth making a real element via hook_element_info(), but it in itself is not an element by itself. Maybe we just make up a #type name? That seems like it might cause other problems. Maybe just setting #type = NULL would be the easiest fix, at least it would eliminate notices if checking #type.

hass’s picture

No, we need to set a proper #type. For being able to inject classes via preprocess functions to the fields I need to know what form field type this is. At least this is the only reliable context that I have for adding classes to form fields.

e.g. #type = webform_file_extensions

hass’s picture

A code example may be of interest. Same type of code exist for core theme_form_element and tons of other form theme functions.

/**
 * Implementation of theme_webform_element().
 */
function foo_webform_element($variables) {
  // Ensure defaults.
  $variables['element'] += array(
    '#title_display' => 'before',
  );

  $element = $variables['element'];

  // All elements using this for display only are given the "display" type.
  if (isset($element['#format']) && $element['#format'] == 'html') {
    $type = 'display';
  }
  else {
    $type = (isset($element['#type']) && !in_array($element['#type'], array('markup', 'textfield', 'webform_email', 'webform_number'))) ? $element['#type'] : $element['#webform_component']['type'];
  }

  // Convert the parents array into a string, excluding the "submitted" wrapper.
  $nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0;
  $parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level)));

  $wrapper_classes = array(
   'form-item',
   'webform-component',
   'webform-component-' . $type,
  );

  // Custom theming start

  // #2029113: Webform module causes a notice here, if a webform file field is added/edited.
  if (isset($element['#type'])) {
    // Add custom form classes to form elements.
    switch ($element['#type']) {
      case 'date':
      case 'emailfield': // D8: email
      case 'item';
      case 'file':
      case 'managed_file':
      case 'numberfield': // D8: number
      case 'rangefield': // D8: range
      case 'searchfield': // D8: search
      case 'telfield': // D8: tel
      case 'textarea':
      case 'textfield':
      case 'password':
      case 'urlfield': // D8: url
        $wrapper_classes[] = 'theme-foo-text';
        break;

      case 'select':
        $wrapper_classes[] = 'theme-foo-select';
        break;

      case 'checkbox':
      case 'radio':
        $wrapper_classes[] = 'theme-foo-check';
        break;
    }
  }
  // Custom theming end

  if (isset($element['#title_display']) && strcmp($element['#title_display'], 'inline') === 0) {
    $wrapper_classes[] = 'webform-container-inline';
  }
  $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="webform-component-' . $parents . '">' . "\n";

  // If #title is not set, we don't display any label or required marker.
  if (!isset($element['#title'])) {
    $element['#title_display'] = 'none';
  }
  $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . _webform_filter_xss($element['#field_prefix']) . '</span> ' : '';
  $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . _webform_filter_xss($element['#field_suffix']) . '</span>' : '';

  switch ($element['#title_display']) {
    case 'inline':
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;

    case 'after':
      $output .= ' ' . $prefix . $element['#children'] . $suffix;
      $output .= ' ' . theme('form_element_label', $variables) . "\n";
      break;

    case 'none':
    case 'attribute':
      // Output no label and no required marker, only the children.
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }

  if (!empty($element['#description'])) {
    // Custom theming
    $output .= ' <div class="description theme-foo-message">' . $element['#description'] . "</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}

sonicthoughts’s picture

Version: 7.x-3.19 » 7.x-4.x-dev

+1 for this error with artisteer generated theme.

sonicthoughts’s picture

Issue summary: View changes

a

DanChadwick’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

I am going to close this for lack of activity. If there is still an issue with file components not having a #type, then please post a patch that you would like to see committed. Otherwise, I suggest you use isset to test before accessing it.

hass’s picture

Priority: Minor » Normal
Status: Closed (won't fix) » Active

The bug seems not fixed and every element need to have a type with no exception.

DanChadwick’s picture

Status: Active » Closed (works as designed)

See #3. Quicksketch is aware of this and won't be changing the design. Your isset workaround will have to do.

hass’s picture

Status: Closed (works as designed) » Active

Nope, the bug need to be fixed in the module.

DanChadwick’s picture

Status: Active » Postponed

Patches welcome.

hass’s picture

Status: Postponed » Active

  • DanChadwick committed f4c2256 on 7.x-4.x
    Issue #2029113: Fixed provide element type for file extensions.
    
  • DanChadwick committed d82fc3d on 8.x-4.x
    Issue #2029113: Fixed provide element type for file extensions.
    
DanChadwick’s picture

Status: Active » Fixed

Committed to 7.x-4.x and 8.x.

Status: Fixed » Closed (fixed)

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