Hello,
I'm sorry if this question was asked before but I've been playing with Drupal for a few days and still not sure how to proceed with my task.
I need to develop a website for a real estate agent. I'm thinking about using Drupal (and I'm new). I need to write code to get property data from a web service (using XML script) and display onto the website. So far, creating a page/view with PHP-code input format is what I think I can do. Says, for page 'Property Search', I need to create a page/view, and in the body, put PHP code to retrieve the list of suburbs and generate the html. Same for every page (property list, property view etc.)
Is there any better way to do this? I just want to make sure I'm on the right track.
Thanks much.
DP.
[Moved to support - Heine]
Comments
Sorry, but putting php code
Sorry, but putting php code in a node is not the preferred method, so IMHO you're not on the right track.
Drupal makes (just like many other CMS'es) a separation between content, behavior and presentation. A node is a piece of content, but you are not writing content, you are creating functionality. In general, it's better to respect the system and keep behavior where it belongs: in modules. In other words, I advice you either find a module that can do what you need (try www.drupalmodules.com for easy searching) or write a custom module yourself (read the module developers guide on http://drupal.org/node/508, and check out the example modules on http://api.drupal.org).
I know that the Drupal learning curve can be steep, and I certainly didn't start writing my own modules after a couple of days. However, once I took the time to learn about writing modules, I was surprised how much you can do with a little code, and I regretted that I didn't try it before.
I hope this helps, if not, keep asking!
This is where I get confused.
This is where I get confused. What I want to display in a page is the property content. And it depends on the user input (select a suburb or a property to view) to get the right content to display. So I think it's more a content-formatted page than a module. It could be that I do not understand modules completely. I guess reading about developing a module will give me a better picture.
There must be someone doing the same thing and I wonder how they did it.
Before we get too
Before we get too theoretical, can you tell me some more about what you want to do? When I have to work with external data, one of the first things I ask myself is: Do I want to import the data and save it in the Drupal database, or do I want to pull data from the external database on every page load? Both approaches have their advantages... how exactly should your site work?
first of all, thank you for
first of all, thank you for your help, marcvangend!
About the site, I just want to pull data from the external database on every page load. This is what I imagine how the site will work. I'll need to use kind of 'curl' with parameters in the url, and the response is an xml file. I'll have functions to parse the xml file and different page will import different functions. Once the content is extracted from the xml file, it'll be wrapped in html code for display. It is simple in a standard php application, where a php page outputs html code from beginning to end. With Drupal, I think I can do this with the approach I mentioned. I just dont know how to implement this with modules and how modules would work in this case.
OK, that's clear. My next
OK, that's clear.
My next question would be: willl you be pulling in the same set of data every time, or do you need a variable to specify which record you want to show? In the first case, you would choose a url (ie. www.example.com/properties) and tell Drupal to run your php code when it's called. In the second case, you can use arguments in the url to pass those variables to your function, so your url could look like www.example.com/property/123 where 123 is the id of your property.
So how does this work? In short:
- you create a module (MODULENAME.module and a MODULENAME.info in the MODULENAME folder, use http://drupal.org/project/module_builder for a quick start)
- you implement hook_menu (= write a function called MODULENAME_menu), which will link a url to your own function and pass on url arguments
- you put your own functions in the .module file and make sure it returns the content (the complete html)
That's it! Drupal will now take your content and put it in a page, complete with headers, navigation, blocks and everything else, depending on your site configuration of course.
To get started, read a little about creating Drupal modules (http://drupal.org/node/231276) and make sure what it means to 'implement a hook'. Also have a look at this example module: http://api.drupal.org/api/file/developer/examples/page_example.module/6/....
ok, I start to see some light
ok, I start to see some light here. I'll go for the second case. I can have one module, choose a url, and pass in variables. It would give the same result as what my first approach would give. I've gone through the links you suggested and indeed creating a module is not that hard. But I guess it'll be a long way to become a Drupal master. Thanks for your help, marcvangend!
You're welcome, thanks for
You're welcome, thanks for the feedback. I'm not a Drupal master myself, but fortunately, you don't have to be one before you can help others :-)
While you can write a module,
While you can write a module, create a specialized page through the menu system and output whatever you want to your hearts content, the fact is you can do this through a node page.
I'm not trying to sound pretentious or create theory arguments. I am saying that at the end of the day, this is your project, you need to get something done. You aren't looking to create an elaborate system which needs a module and multiple hooks. I know how to write modules, but if I needed to make a quick view that is unique, I could do it with PHP/MySQL. See this page: http://www.thinklanguage.com/french/magazine
The page looks like this:
<?php$file = "." . base_path() . path_to_theme() . '/includes/magazine_teaser_page.inc';
if (file_exists($file )) {
include $file;
}
?>
In it, I query my database for the top 3 nodes of a certain type, then I call node_load() on those nodes and extract out the fields I want. In fact, I don't even need to use node_load(), I can just get the fields out of the database directly.
<?php
//.... more logic above here
$sql = db_query("SELECT nid FROM {node} WHERE type = 'magazine' ORDER BY created DESC LIMIT 3");
echo '<div id = "article-teaser-container">';
while ($result = db_result($sql)) {
$node = node_load($result); //Since only 3, it shouldn't be too hard to utalize node_load. But I could optimize it by creating a query for each item?
//But with cacheing enabled, it actually shouldn't matter I don't think... if the page turns out to be too slow, I will query the database directly for each variable...
echo <<<EOT
<div class = "article-teaser-outline">
<h2>
<a href = "$base{$node->path}" alt = "$node->title">
<img class = "teaser-img" src = "$base{$node->field_image[0][filepath]}" width = "125px" />
</a>
<a href = "$base{$node->path}" alt = "$node->title">$node->title</a>
</h2>
<p class = "teaser-contents">{$node->field_teaser[0][value]}</p>
<div class = "readon">$issue</div>
</div>
EOT;
}
//.... other logic/stuff below
?>
There is a lot more to that file, including logic to check if the user is a premium subscriber or not. For example the
<?php$issue
?>
Case in point, I got the job done, I did it in an hour, I barely had to debug it, and I have full control over it. So, if my work is say $75/hr, I got it done for $75. If I used a module, it would have taken me a few more hours.
But of course, I had to really learn about drupal, the database, how everything works, etc before I could "go about it the easy way". (How else can I get away with $75/hr :)
The fact is, even if I did write a module, it would do the same thing, the output would look just the same, I would make the same database calls. All that would be different would be I'd create the page directly and not use the node system... In fact, it would be such a benign module, using 3 or so hooks. It would almost be excessive to the system to add such a small module...
Of course you can do this in
Of course you can do this in a node with php code, I never said you can't. However it is not considered the right method by many developers. (In fact, I guess that some would find this just as evil as table based lay-outs.) If you want to read more about this, see http://2bits.com/articles/free-your-content-php-moving-php-code-out-bloc.... In short: modules are easier to maintain and extend.
If you never created a module before, then of course adding php to a node is the quickest way. However that is like saying "driving lessons will take me a month, so if I want to get to the other side of town, I rather walk for an hour". And trust me: learning to create a simple Drupal module will NOT take a month.
Points noted. I guess
Points noted. I guess esend7881 did not mean that I should skip learning & implementing modules, he just showed an alternative way to get the same result. Anyway, I will do my first Drupal project in the right way, i.e. implementing a module, and if time allows, I'll explore the alternative way. Having an alternative is better than none anyway.