By Pimmy on
Hi,
I have written the simplest possible module for a content type which has only one single field - the 'Title' which I renamed to 'Full Name' (I have removed the body as I dont need it).
All good - I can add content and view it just fine: For each added node it displays what I have specified in the 'Full Name' field.
Then I tried to extend this by using the hook_view() to display some additional data (first and last name) when viewing each node but so far I had no joy. In the example below I try to display simple string as additional data, but even that doesnt work:
/**
* Implementation of hook_view().
*/
function person_view($node, $view_mode = 'full') {
// Use Drupal's default node view.
$node = node_load($node->nid);
// Add first and last names.
$node->content['name_first'] = array(
'#value' => theme('person_name_first', $node),
'#weight' => 2
);
$node->content['name_last'] = array(
'#value' => theme('person_name_last', $node),
'#weight' => 3
);
return $node;
}
/**
* Implementation of hook_theme().
*/
function person_theme() {
return array(
'person_name_first' => array(
'arguments' => array('node'),
),
'person_name_last' => array(
'arguments' => array('node'),
),
);
}
function theme_person_name_first($node) {
return '<div class="person-name_first">John</div><br />';
}
function theme_person_name_last($node) {
return '<div class="person-name_last">Smith</div>';
}
Could someone tell me what am I doing wrong here?
Thanks very much in advance,
Pimmy
Comments
Two questions, why not just
Two questions, why not just use the Content Construction Kit (CCK).
And I do not see a instance of hook_node_info() or any of the other hooks needed for defining a content type.
Hi Nevets, I have decided to
Hi Nevets,
I have decided to go the node API path because I am Drupal newbie anyway and this way I would learn the core Drupal which would be more of a benefit I think. I have started with CCK, but it is lacking any useful documentation and I was going nowhere for a couple of days. Most importantly with node API, I would be able to create a module which would do exactly what I need.
I have posted only the part which I thought would be of interest (not the entire .module file). Do you want me to post the entire file?
Personally (though others
Personally (though others disagree) I feel that understand what you can do with Drupal (including contributed modules) is of more benefit that writing your own module, there is no practical reason for re-inventing the wheel.
As for posting the whole file, no that will not help. If adding/editing nodes of your content type works and the only thing that does not is theming the output, I would suggest clearing the theme registry.
I cannot disagree on what you
I cannot disagree on what you are saying, its just that I have chosen that path as it seems easier to me. Fiddling with CCK blindly without any decent documentation did not get me anywhere for two days, while writing my own module did (up to this point).
My problem here is not actually theming the output but adding more information to the actual output contained by the node. In my example I try to show on the page static text (John Smith) but it doesnt work although I follow the API description. For the time being although I use kind of theming (as this is how the API show the example), I dont really care how the output would look like, I only want to make the static text to pop out on the page. Any suggestions?
Thanks very much for the help so far,
Pimmy
I am really unclear what you
I am really unclear what you are trying to do. person_view() is one of many functions a module must implement in order to define a custom content type. So does your module implement person_node_info()?
I agree nevets but to truly
I agree nevets but to truly understand what you can do with Drupal is to know Drupal inside and out, which means a person should know how to create modules and know about Drupal core a long with the modules that are available . It is then they can harness everything Drupal has to offer and its true power, which is immense.
Yes we should not re-invent the wheel but we should know how that wheel works because if it pops, who is going to fix it? Well, you could pay someone and they could charge you a nice little price or you could save yourself a lot of money and fix it. I for one would take the second choice especially if it is a quick fix.
One should always be informed
OK, lets start from zero as I
OK, let me start from zero as I might have confused you with my explanation and part of the .module file.
I did create a module for adding people of content type 'Person'. The module has only one single field - 'Full Name'. So far it works OK as I can add people, show a list of people, and view single person. My task now is when I view a particular person (The node would contain only the full name), the node to request further data (relative to the full name stored by the node) from a legacy database and show it under the 'Full Name'. Before jumping into writing the database layer (db_query) I have tried first to display basic string under the 'Full Name', however unsuccessful. I cannot make any string to appear on the view node page.
Below is my full .module file with all the hooks which I have implemented. Please tell me what am I doing wrong.
Of course ideally for me is to learn how to achieve the same functionality with CCK as well.
I could actually go a bit
I could actually go a bit further by implementing hook_load to get for example persons first name:
I have also modified hook_view() to use an array in the theme function which eliminated one of the errors I am getting:
Unfortunately I get two problems which I cannot overcome:
1. I get this error:
In person_load() I have verified that $node->title does contain a value and yet it throws this error.
2. I understand that if I remove the hardcoded assignment:
$node->name_first and $node->name_last would not be populated, but I really cannot figure out how to do that correctly with hook_load. Generally I cannot find much information of how hook_load works and I would really appreciate any help.
...
You need to use #markup, not #value.
(It was #value in Drupal 6, but it was renamed to #markup to make its purpose clearer.)
That's only one third of the story. You also need to change the theme functions to accept $variables, and your hook_theme() to have
'variables' => array('node' => NULL).I like your stages tactics. You should have tried
$node->content['name_first'] = array('#value' => 'bobo')first.Mooffie, This was one of the
Mooffie,
This was one of the most precise and valuable advices I have received so far in this forum. I did get it working at the end and now I can happily display formatted text in addition to the node data.
For all muppets like me who are just starting with Drupal and jump directly onto D7, here is how the .module file looks like:
Thanks a million for your help. Jumping now onto the db layer...
Cheers,
Pimmy
You need to use #markup, not
You need to use #markup, not #value.Yup!