For Activists
Comments
Very nice writeup ! Thanks
Very nice writeup ! Thanks for sharing :-)
There's an issue in the way you detect whether the widget is displayed in a regular 'entity edit' form or on the 'field configuration' form (for 'default value') :
$is_settings_form_type = isset($form['type']['#type']) ? FALSE : TRUE;
$form['type']['#type'] is only present for node edit forms. That is, your widget will incorrectly assume it's displayed on the 'field configuration' form when the field is used on users, taxo terms, or any other entity type.
Besides, beware that the $form param received by hook_field_widget_form() might not be the 'full' form, but just a subpart of a larger form. For instance, that's how http://drupal.org/project/field_collection does to provide 'fields within fields' (or 'combo fields'). In this case you wouldn't have access to the form elements that live on the top-level.
I'm actually not sure that there's a safe way to tell the difference. Widgets are supposed to be unaware where they're being included : an edit form, the field config form, ... - could even be displayed as an exposed Views filter (although not currently ;-)
Comments
Mh, mh... - You want to
Mh, mh...
- You want to compare it with a static db_query(). What you are comparing here is Drupal's query builder with a prepared statements. It's quite obvious that db_select() is a lot slower. It's built for creating dynamic queries and should only be used when necessary.
- Your code for calling the prepared statement is imho way too complex than it needs to be. A simple "$result = db_query('Call GetNodeList(:type,:state)', array(':type' => 'article', ':state' => 1));" should be all you need.
- There are some handy helper methods for fetching data which can replace your fetch loop. "$rows = $result->fetchCol()" should be all you need. The 'data' key is only necessary when you have other things you want to apply like classes.
- This is a bad example to start with, because when querying {node} tables, you should *always* use $query->addTag('node_access') for security reasons so that node access is applied correctly.
Proof of Concept
Hi,
See my comment below titled Network Traffic. In the real world a legacy scientific calculation that's been in place to 15 years is not going to be written with the Drupal node table? Perhaps the author used the node table because most Drupal installations have one. The article is not about how to select nodes??? It's as stated, a proof-of-concept?
I tried your example and it was slower than the stored procedure. Have you tried your db_query mechanism with a stored procedure that contains out parameters? Are you certain that db_query binds the parameters like a PDOStatement? A stored procedure is pre-compiled executable in the database. The bind process is there for a reason.
Devel Generate
Try running Devel Generate (http://drupal.org/project/devel) to create a whole boatload of nodes and see what performance is like with different sized sites.
I suspect the difference will be much smaller for large data sets.
Network Traffic
Often a business method is a mix of application language code and calls to the database. It's not uncommon to have your web server and database server running on different hosts. Thus a stored procedure that performs all of the business logic in the database, by definition, reduces network traffic. A reduction is network traffic is a performance boost. More records in a typical complex proc would produce a much wider performance gap.
The stored procedure used in this example doesn't represent a typical "proc". As alluded in the article, stored procedures are normally complex processes that have many steps. I'm guessing the example used in the article was used just as a succinct example. Something that folks could easily paste in to the command line.
I've worked in places where stored procedures are created by a team of DBA's. You don't have a choice, they require you to use their centralized procs.
Comments
Few remarks
Hi,
Nice script and basis to build on. A few remarks:
I'd rather use the current date instead of a default date and want to be able to retrieve a previous saved date from the db, so I changed news_date_field_widget_form() from:
$default_date_int = $instance['default_value'][0][$field_type][$field_type];
$default_date_array = _intToDateArray($default_date_int);
$news_data_value = isset($date_array) ? $date_array : _intToDateArray(time());
to
$default_date_int = isset($items[$delta][$field_type]) && !is_array($items[$delta][$field_type]) ? $items[$delta][$field_type] : time();
$news_data_value = _intToDateArray($default_date_int);
But also, now I load previous set dates, the did not get selected in the HTML select element, because _intToDateArray() returned month and day numbers with leading zeros. So I changed this function from:
$month = date('m',$timestamp);
$day = date('d',$timestamp);
to
$month = date('n',$timestamp);
$day = date('j',$timestamp);
Nice way to learn how to make custom fields! :-)
Cheers!
Laurens Meurs, Rotterdam
Nice tutorial
Nice tutorial, but how do you associate it to a node / entity? Could you add a tutorial on how to add this new field to something please. I assume you use the field_attach.api but I cant find any examples that go through the whole create field -> attach field to something. Cheers love the site :-)
My bad I see the follow-up blog has the answer :-)
I see the answer is here in this post.
http://public-action.org/content/drupal-7-field-api-drupal-7-adding-cust...
However how would you add it to say
user/%/edit page?
Change the entity type to user
If you takes the code in http://public-action.org/content/drupal-7-field-api-drupal-7-adding-cust... and change the entity type to user, wouldn't that do it?
In Drupal 7 both Nodes and User objects are specializations of Entity. Thus replacing an entity type of "node" with an entity type of "user" should suffice.
Comments
How do I clear the entries from the database
Hi,
Nice example. I tried it. Was able to enter and then get my system to display multiple entries in the report. Do I have to clear the cache to delete the records?
Comments
Nice and Easy
I just generated a Hello World module with your tool and installed it on a Drupal 6 development site (running on my laptop). Worked like a charm. Any thoughts on enhancing this tool to provide some of the standard node hooks?
How about the node hooks used in the "Examples for Developers"
Actually I am a software developer, not a "themer".
I would take a look hooks used in the node examples published at "Examples for Developers" http://drupal.org/project/examples. I think a lot of developers would find it useful to get a boilerplate of the common nook hook (over-rides).
Again thank you.

.






Comments
Table not created
Hi,
I followed your example, but i keep getting:
"Base table or view not found: 1146 Table 'gen_skudb.colordb' doesn't exist"
- gen_skuDB is my data base and colordb is the table that i am trying to create ( you called it folk in your example).
Of course i tried dis/enabling and Un/installing and flushing cache, but still i get that error, and phpMyAdmin does not show that table either.
any ideas where else i should look... i googled without any luck...
Correct function names
Hi,
Please take a look at the articles first code listing "function proto_schema() {". My guess is, your schema method is not called because your function name does not match your module name.
In the article, the module's names is "proto". Therefore, when an administrator enables the module Drupal looks for a function called MODULE_NAME_schema(). Thus the function name "proto_schema()". "proto_schema()" is an implementation of hook_schema. You replace the word "hook" with your module name.
Next, it sounds like you don't have a normal development environment. You should always install Drupal on your local machine (i.e. laptop or desktop). When you develop on your local machine, you have full access to the web server and database logs. I use linux and keep a terminal open which "tails" (displays) the last 40 messages from the web server and database. Many times, those logs have a precise message as to where the application exception is.
When you develop on a remote machine without real time access to the logs, you guarantee that a simple 30 second diagnosis and fix will take hours or days.
I always use a source code debugger. Drupal is written in PHP, thus I use xdebug. I highly recommend you learn how to use a source code debugger. Trying to diagnose a simple problem like "my function is not being called" is child's play with a source code debugger. It is highly unlikely that you will find the exact source code line which is the issue through google. Drupal is completely open source, thus you can set a debugger break point anywhere you want and peruse what Drupal is doing.
To summarize, with xdebug you set a break point on your hook_schema() implementation method. You then log on to your local site as an administrator. You install and enable your module. Verify your hook_schema method is called. If called, check the server logs to see if there is an issue (perhaps you don't have the proper database permissions, the log would show that).
Hope that helps.
Jason