1

I have a specific node-type: resource. The default display of a resource node is not quite what I want, so I need to over ride the themeable output - so far so good. The problem is, I've created my resource node-type through the Drupal 7 GUI. How do I write a module that just over rides the HTML output for this one content-type?

...and, in piggyback to that question - I will have many other node-types that I've created through the GUI, but need custom output for. Is there a way to create just a single module that will handle the custom output for all of these types?

Thanks.

1

I would use a template override on node.tpl.php for the resource type. Just copy it to node--resource.tpl.php and make your changes. Here is a good tutorial.

1

IMO, a very good approach would be using Display Suite to generate the basic markup/layout and then add the appropriate styling to the theme.

1

You can use hook_view() and hook_node_view() in your module to customize your node display, or hook_field_prepare_view() to alter individual fields.

On your module within hook_view() and hook_node_view(), you should use a control statement like switch to place the code for each type:

switch ($node->type) {
  case 'resource':
    ...
    break;
  case 'other':
    ...
    break;
}

To see what is inside $node, you can use the Drupal 7 debug() function, like this:

debug($node);
  • Would you consider it an inappropriate design pattern to create a single module that handles the node display of all of the content types? I should add that there are a large number of content types, each of which has a very particular display format. – rybosome Nov 1 '11 at 1:23
  • No, I wouldn't consider it inappropriate unless there are so many other features for each one that it makes sense to bundle them into different modules. If you have maybe a lot of custom code for each type but just need it all for node display, you can still make the code more tidy by separating it into functions and reuse some of them. You can also use include files to group functions for the same content type. Adding so many more modules will somewhat affect performance too, so keep it as simple as possible. – 5t4rdu5t Nov 1 '11 at 1:51
  • Just a note, that hook_view() will not work for node types setup via the UI, it only works for node types specified with hook_node_info() and it's more or less, not even a hook, but a named callback based on the "base" key from hook_node_info(). – Mikey P Apr 8 '12 at 20:07
0

To answer the question about managing the display of all the content types, you may also want to look into adding custom view modes. Display Suite itself may give you what you need, but keep view modes in mind if you need to display several of the node types in a certain fashion or want to display different fields on different tabs in conjunction with Panels or Display Suite.

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.