Does not work for content types with "_" in name

jrb - December 4, 2008 - 17:22
Project:Theme setter
Version:6.x-2.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:postponed (maintainer needs more info)
Description

This module will not work for content types with underscores in the name (e.g. "job_posting"). This is because Drupal converts "_" to "-" when making the content type part of the URL. Theme Setter was searching for a match for "job-posting", but it wouldn't find it because "job_posting" was the value it had saved.

To fix this problem, I changed this in theme_setter.admin.inc:

foreach($types as $oType) {
    $content_types[$oType->type] = $oType->name;
}

To this:

foreach($types as $oType) {
    $content_types[str_replace('_', '-', $oType->type)] = $oType->name;
}

Now it works.

Drupal may have a function specifically made to do this conversion, but I'm new to Drupal and I don't know what it is.

#1

VeryMisunderstood - December 4, 2008 - 17:39
Status:active» postponed (maintainer needs more info)

Hmmm, I think there is a larger problem here with the above and that those who follow drupal best practices and use the - instead of the _ will be wondering why it's being changed, no?

ie: node-audio.tpl.php will be changed to node_audio.tpl.php and now no longer work correctly.

Or am I not undertanding this correctly? The way I am viewing it now is that you'ev chnaged this to fit your personal preference and that this isn't actually a bug in the module or in drupal.

#2

jrb - December 4, 2008 - 19:13

No, the problem is with difference in the way that URLs are formed by Drupal and how Theme Setter stores its administrative preferences.

Let's say we have a module called "Job Posting" that creates a custom content type with a "type" of "job_posting". When a user goes to create a new node with this content type, the URL will be this:

http://example.com/node/add/job-posting

Note that the underscore was converted to a hyphen. From the help text on the "Add content type" tab:

The machine-readable name of this content type. This text will be used for constructing the URL of the create content page for this content type. This name must contain only lowercase letters, numbers, and underscores. Underscores will be converted into hyphens when constructing the URL of the create content page. This name must be unique.

When Theme Setter stores the administrative preferences on its admin page, it stores the "type" with the underscore-- in my example, it stores "job_posting". Now, when the user goes to the above URL to create a new "Job Posting", Theme Setter takes the arguments from the URL ("job-posting"), checks the administrative preferences, doesn't find "job-posting" (because it stored "job_posting"), and Drupal will not use the default theme.

All my change does is make the Theme Setter administrative preferences admin page store "job-posting" instead of "job_posting". This way, when Theme Setter looks at the URL to see if it should use the default theme, it will find it.

Hope this is more clear.

#3

VeryMisunderstood - December 4, 2008 - 19:16

ahhhh gotcha!

 
 

Drupal is a registered trademark of Dries Buytaert.