Does someone know why xmlsitemap.module implements every hook_node_comment_ hooks, with a StdClass casting for the object passed in param ? example :

 * Implements hook_comment_delete().
function xmlsitemap_node_comment_delete(stdClass $comment) {

(xmlsitemap_node.module l.95)

A teammate overrides some Drupal default comment behaviours by declaring a new entity, with CustomComment as "entity class" name.

Then, when we want to delete a comment, this xmlsitemap's casting fires an error because the loaded comment is not anymore a StdClass object, but a CustomComment object.

Do I have to hack that module ? Any Idea ?

  • Could you elaborate on what you teammate has done to "override" Drupal's default comment behaviour? I'm not sure how this would be possible without hacking at core. If he's simply sidestepped the current comment functionality, then you can't expect contrib modules that have been built to interact with the core comment module to still work with your new entity type. – Clive May 18 '12 at 8:49
  • In fact, we extended the Entity Class with our CustomComment Class in a custom module, and our CustomCommentController extends EntityApiController. We didn't hack the Core. I'm sorry for coming back on these xmlsitemap modules' hooks implementations, but I think that if the standards had been correctly respected, our development would have work fine... So I mean... My question is not really about what my mate did, but Why this module implements hooks this way. (btw, sorry about my english ^^) – Julien May 18 '12 at 10:10
  • The entity class definition comes from entity.module, there is no entity_create() function in Drupal core. What this means is that Drupal core does not and can not respect that setting, so you can't rely on it being a CustomClass, e.g. when creating/saving comments. I would recommend against doing that. – Berdir Jul 23 '12 at 8:13

It seems like the author of that function might just have been a tiny bit over-zealous in trying to strongly-type the $comment parameter.

The hook_comment_delete() docs don't really give any indication that it should be implemented in this manner; but if you look at the signature for the Drupal 8 version of the function, you'll see the parameters are typed. It might be that the author was following that 'pattern' when creating the function (rightly or wrongly). That's a complete guess though.

I haven't seen hook parameters being typed like this in any other contrib modules that I can think of, so it's probably an isolated incident (although this format is consistent across most of the hooks for the xmlsitemap node module).

I think the best way forward would be to create a patch that fixes the issue, and perhaps submit it to the XmlSitemap issue queue for others to review.

| improve this answer | |
  • Thanks for your answer. It was my opinion too, about the fact that the developer who wrote these functions had been over-zealous. I will submit an issue for that on Drupal.org Thanks again. Regards. -- Now, I think we must take care about what will become our developments when D8 will come out... – Julien May 18 '12 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.