Yesterday I posted a quick test post from Windows Live Writer as a proof of concept that I could make WLW better support Drupal categories. Well as promised I am now going to tell you all how I got support from the Drupal BlogAPI module.
Yesterday I posted a quick test post from Windows Live Writer as a proof of concept that I could make WLW better support Drupal categories. Well as promised I am now going to tell you all how I got support from the Drupal BlogAPI module.
Here is the problem I was out to fix…
The BlogAPI module and WLW supports Drupal taxonomy out of ‘the box’ as it were. In WLW when you click the “Set Categories” box and refresh the list Drupal returns a full list of all relevant taxonomy terms. So any taxonomy type associated with the Blog Entry type will be returned, from categories to the full list tags on your site. Now I only want Drupal to return one set of terms, in my case only tags from the ‘Categories’ term set.
So how did I limit Drupal to only return one set of taxonomy terms? Well like a lot of things it involved some trial-and-error style source hacking, do not panic! I have attached a patch file to this post so you don't need to do the work your self.
In the basic setup to code modifications were simple to do I just added a PHP if statement that will check for the correct term name before returning the containing taxonomy. What you do need to do however is create a new taxonomy type Drupal called “Categories” you can change this name bug if you do you will need to edit the attached patch file before you try using it!
So what have I do with my source? Let me show you, first here is the original function ‘blogapi_metaweblog_get_category_list’ function from the “modules/blogapi/blogapi.module” file…
$user = blogapi_validate_user($username, $password);
if (!$user->uid) {
return blogapi_error($user);
}
if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) {
// Return an error if not configured type.
return $error;
}
$vocabularies = module_invoke('taxonomy', 'get_vocabularies', $blogid, 'vid');
$categories = array();
if ($vocabularies) {
foreach ($vocabularies as $vocabulary) {
$terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1);
foreach ($terms as $term) {
$term_name = $term->name;
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
$term_name = $parent->name .'/'. $term_name;
}
$categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid);
}
}
}
return $categories;
}
Now here is my edited function…
$user = blogapi_validate_user($username, $password);
if (!$user->uid) {
return blogapi_error($user);
}
if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) {
// Return an error if not configured type.
return $error;
}
$vocabularies = module_invoke('taxonomy', 'get_vocabularies', $blogid, 'vid');
$categories = array();
if ($vocabularies) {
foreach ($vocabularies as $vocabulary) {
if ($vocabulary->name == "Categories") { // New Insert Code
$terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1);
foreach ($terms as $term) {
$term_name = $term->name;
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
$term_name = $parent->name .'/'. $term_name;
}
$categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid);
}
} // New Insert Code
}
}
return $categories;
}
As you can see all I did was an PHP if statement after the foreach command, but like I said before I have attached a patch file to this post. If you are concerned about applying the patch you can just edit the file your self as shown, but Drupal already has some fantastic documentation on applying patches to its source. If you have any problems or thing of improvements please pop me a line as I would love to hear from anyone who makes use of this adaption.
| Attachment | Size |
|---|---|
| blogapi.module.patch | 1.26 KB |


