Monday, March 1, 2010

Alphabetical Sorting and HTML-Output of Drupal Glossary Module fixed - code example

The glossary module is still at an early stage in its development. It has 2 major weaknesses.
  • The alphabetical sorting function does not work correctly.
  • The Module does not generate correct html.
We have analyzed both: Have a look at our fixes with concise code-examples.

Drupal Glossary Module Does Not Sort Alphabetically Correct

A look into the bug list of the Glossary Drupal module displays this error to be known, but still open. A source code check of Drupal glossary module quickly showed the bug. The Glossary module gets its list of items from the Drupal Taxonomy Module. In the Taxonomy module alphabetical sorting is subordinated to editorial weights given to the items. We have added this code to the Drupal Glossary module: Here the Items are turned over to the Glossary

$tree = taxonomy_get_tree($vid); $ tree = taxonomy_get_tree ($ vid);
// Now the code of Arlmedia:
usort($tree, '_glossary_cmp'); usort ($ tree, '_glossary_cmp');

// We add the following function.

function _glossary_cmp($a, $b) { _glossary_cmp function ($ a, $ b) (
return strcasecmp ($a->name , $b->name); return strcasecmp ($ a-> name, $ b-> name);
} )
The solution is in the Drupal glossary module: we will have to adjust every module upgrade, until the problem is fixed in the module.

The Glossary Module produces no valid Html.

W3C check of the http://www.gelenk-doktor.de code revealed the problem: Drupal Glossary module does not generate valid HTML. See also http://www.w3.org/TR/html401/struct/lists.html # h-10.3) For listing content the glossary module uses DL, DD und DT Tags. The anchors for page navigation are interrupted by the DL, DD and DT tags. That is not W3C standard code.
So we manipulate HTML output not in the glossary module but in the theme display module.
We have added a new template called page-glossary.tpl.php and registered it with Drupal. Within the theme we manipulate the output generated from Drupal glossary module to become valid html:

$contentnew = str_replace(array(" $ contentnew = str_replace (array ( '
", " ""
"),"", $contentneu); "),"", $ Contentneu);
$contentnew = str_replace(" $ contentnew = str_replace ( "
$contentnew = str_replace(" $ contentnew = str_replace ( "
", " ""
", $contentneu); ", $ Contentneu);
After applying the patch the list is right between the anchor tags. Until the Drupal glossary module repairs this bug our solution will at least not break the upgrade path: It simply changes the Drupal glossary module output by applying an new theme template.

No comments: