Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 301255

Duplicate HTML IDs are created for file_managed_file fields (w3c validator error)

$
0
0

API page: https://api.drupal.org/api/drupal/modules%21file%21file.module/function/...

Enter a descriptive title (above) relating to function theme_file_managed_file, then describe the problem you have found:

Hello,

The code creates two html elements with duplicate id (forbiden by the w3c validator).

First, the code creates $attributes array and set the same id than the file input id :

<?php
  $attributes = array();
  if (isset($element['#id'])) {
    $attributes['id'] = $element['#id'];
  }
?>

And after, it creates a div elem with this id before rendering the file input element :

<?php
  $output = '';
  $output .= '<div' . drupal_attributes($attributes) . '>';
  $output .= drupal_render_children($element);
  $output .= '</div>';
  return $output;
?>

So it produce two html elements with the same id.

To temporary fixe this issue, i have done a module (%module_name%) and put this code into the %module_name%.module file :

<?php
function %module_name%_theme($existing, $type, $theme, $path) {
    $return = array();
    $return['file_managed_file'] = $existing['file_managed_file'];
    $return['file_managed_file']['function'] = '%module_name%_theme_file_managed_file';
    return $return;
}
function %module_name%_theme_file_managed_file($variables) {
  $element = $variables['element'];
  $attributes = array();
  if (isset($element['#id'])) {
    $attributes['id'] = 'div-'.$element['#id'];
  }
  if (!empty($element['#attributes']['class'])) {
    $attributes['class'] = (array) $element['#attributes']['class'];
  }
  $attributes['class'][] = 'form-managed-file';
  $output = '';
  $output .= '<div' . drupal_attributes($attributes) . '>';
  $output .= drupal_render_children($element);
  $output .= '</div>';
  return $output;
}
?>

I just change

<?php
 $attributes['id'] = $element['#id'];
?>

by
<?php
 $attributes['id'] = 'div-'.$element['#id'];
?>

Best regards.


Viewing all articles
Browse latest Browse all 301255

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>