By piquef on
I have placed a function declaration in the template.php file for my theme (Garland), but the function is not getting 'picked up' or recognized when I invoke it within a Page node. I have tried clearing the cache, performing an update.php and have run out of ideas!
As I understand it, a function that is declared in template.php should then be invokable from within a node that has the PHP filter enabled. Is there something I'm missing? Any further help greatly appreciated!
Comments
Hi Charles, Saw your post in
Hi Charles,
Saw your post in the dynamic table thread, as well as this one...
From my understanding, placing code inside your theme's template.php file is not a good way to solve your situation. If (for some reason) you ever change themes, any logic inside your template.php file (which is specific to that theme) will break. I'm not really sure why your declared function is not being picked up, but that's not a good method anyway... (don't bother solving problems for issues you should not be doing anyway)
You need to create a module, which is actually so simple it's almost a joke...
Follow these steps, and you should be able to call your function from any node, any block, regardless of what theme you're using...
1) Create a directory inside http://www.yoursite.com/sites/all/modules that will hold your custom module. The name of the new directory needs to be the same as the name of your custom module. For clarity, I'll use the name 'piquef_logic' as the directory and module name during my explanation.
2) Create a text file inside http://www.yoursite.com/sites/all/modules/piquef_logic named 'piquef_logic.info' and paste this into it:
(note: I'm assuming you're working with Drupal 6, and with a host that supports php 5.1...)
3) Create a 2nd text file inside http://www.yoursite.com/sites/all/modules/piquef_logic named 'piquef_logic.module' and paste your PHP function you're previously had inside your template.php file.
(Make sure you've deleted that other version inside template.php...)
Also, I suggest you rename this function to something like piquef_[previous_function_name] to insure that this function name is unique to all functions in the environment.
4) Navigate on your site to admin/build/modules and locate your new module's entry... click the checkbox to activate it and click the "Save configuration" button.
5) Navigate to your 'create content' section (or the node where you've already been trying to call this function) and try viewing it again. If you've changed the function name, as I suggest above, then make sure you're calling the function by its new name.
Viewing the node now should be calling your function. If not, check to be sure your input filter for the node is set to PHP.
You should be good to go...
-Blake
www.BlakeSenftner.com
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Fantastic - back to Plan A!
Blake,
That's awesome - thank you so much for taking the time to lay that out for me. I have spent the past couple of hours working through CCK and VIEWS, as a "plan B". It looks like the module route you have suggested will enable me to stick with my "plan A", which has made my day! I'll have to pick it up tomorrow - I'm burned for today ...
I'll report back tomorrow, but your directions look clear and perfectly doable. What a nice and cleaner approach - I was never too keen on messing with the template.php file for the reason you state - it makes no sense to me to have to bury site-specific code in a theme-specific location!
Charles
Success!
Blake,
That was an excellent tip - I have it all working, plus I now have somewhere to add any other borrowed or 'roll-my-own' routines I may come up with. Now I need to figure out a mechanism to let my non-techy users add a row's worth of info via the displayed table directly into the SQL table of data that my new routine displays. My Plan B used CCK and Views to enable the inputting, but that is cumbersome and doesn't directly feed into this nice dynamic, sortable table. I want my users to be able to enter directly into the fields in, say, the first (empty) row of my table.
Incidentally, I have NancyDru to thank and acknowledge for the php code for the "Generic Table Display".
Thanks again for your help.
Charles
I'm glad it worked.
Charles,
I'm glad it worked.
Now that you've seen how easy it is to create a module, perhaps you can solve your non-techy users' need to add rows to your table with a custom form you create in Drupal's FormAPI. That is also so simple, it's crazy. It is a bit more complicated than simply making a module to paste code snippets into, but it's really not that bad.
Although, I would recommend you get a book to guide you, something like "Front End Drupal"; that's the first Drupal book I read, and I feel its the best one I've encountered.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Good Drupal Book
Thanks for another couple of suggestions - I'll take a look at FormAPI. That sounds very appealing as a solution for the data input phase.
I have been using an O'Reilly book "Using Drupal", but that hasn't really addressed some of my needs. Are you familiar with that book too? I'll look at "Front End Drupal" now.
(I'm now trying to make that routine display the table initially in reverse sort order by something other than the Primary index. I'm having to delve deeper into the php than I intended!)
Charles
Yeah, I have the "Using
Yeah, I have the "Using Drupal" from O'Reilly too - it's more of a cookbook than detailed explanations - although it does have one of the better documentations of Ubercart out there...
Also, if you don't have experience with the Panels module, that may also be a good one for you to use within your overall solution. The panels module allows you to create a "page" that contains multiple nodes. Essentially, panels gives you the following concepts to break up a page:
What I'm thinking here is that you'd have a Panels Page with one column, and two rows. The top row is the table generated by that generic table snippet, and the bottom row is a single row custom form (node) you create with the FormAPI. You already know the table's name and fields, since you're supplying that to the generic table snippet... so your custom form simply has text fields for each field of your table, the "validation callback" of your form simply verifies that the user supplied numbers where expected and text where expected to the various fields of your form, and your "submission callback" simply adds the new (verified) data to the table with a drupal_write_record() call.
If you visit my www.BlakeSenftner.com web site, that home page is built using Panels. I wanted flexibility, so it's one column with each individual section of the page being a separate node. That made editing the various sections more atomic, and allows me to easily rearrange them.
BTW, I downloaded that "Site Documentation" module, as mentioned in the Generic Table snippet. I'll probably experiment with it sometime later today...
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Got the book
I got the Front End Drupal book - that looks helpful - I wish I had bought that months ago. I plan to check out Panels, too - I like the modular approach to page design that it affords.
Charles
Stuck at Submit
The Forms API is really stretching my abilities ... I've installed a function to display a form with the requisite fields for the user to enter (based on the example test_form() here.) I have three entry fields:
a selection menu field (one of 5 text choices)
and two short text fields.
That appears to work fine.
I am stuck on the next step - how and where do I add a function call to capture the user input when they press the Submit button, then the verify and the write record? I also want to add programmatically three further fields to the record before I write it: the login user name/id, an incremented unique record number and a timestamp.
Thus, my table has a record structure:
integer auto-increment record number
varchar user login name
varchar text field
fixed set (one of 5 text values)
varchar text field
timestamp
Of course, then I want to have the ability to update an existing record and delete a record!! I guess that I should be able to figure out how to do those once I have the insert a new record working. I'm fine with adapting existing code from elsewhere - I just haven't been able to find existing code that shows me where to place the calls I need. If you (or anyone) have a few minutes to point me in the right direction, I'd very much appreciate it.
Oh - I'm now using the Panels module, too - great tip - thanks, Blake.
Charles
your next step
Charles,
Hopefully this example will help you:
A menu hook that implements a function callback:
Note that in the above, I set a callback function to be called when a user clicks that menu item. I could have used 'drupal_get_form' as the callback, but by having my own routine, I am free to insert additional logic if needed. However, for this example, none is needed, so that callback's logic looks like this:
I began the callback function with 'YMN' for 'your module name' - this insures that I'm not accidentally creating a function name that matches some other function from some other module, and I checked the possible hooks, and there are no hooks named 'hook_TestFormCB()'. This issue bit me once, I had a function that matched a drupal hook, and it took some time to figure out what was going on when my site behaved strangely...
Now, that call to drupal_get_form() has some special characteristics. By calling that routine and passing that string, you are telling Drupal that you have the following functions available:
With that explanation, here's an example form builder creating a minimal form:
When the user clicks the submit button, this gets called automatically to verify the input:
If the above routine does not call form_set_error(), then the form's data is fine, and the submit routine gets called:
That should do it.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Thanks again!
That's awesome - thank you. I'll work on that over the next day or so. I very much appreciate your taking the time to do that - I'm looking forward to getting it all working. This morning I'm registered for the Acquia Webinar on Drupal 7, so I plan to take a closer look at this all later today.
Charles
I'm stuck!
Hi Blake,
Despite my best efforts, I have not been able to get this to work - I'm clearly missing something - probably something pretty basic about how to put this together. I have created a table in my SQL database called caftesttable that has two fields: Field 1 is named id; type is int(4), auto_increment; Field 2 is named Stuff; type is varchar(48).
I've adapted your functions and tried to make sense of it all! They are in the file my_routines.module at http://idisk.mac.com/charlesfrean-Public/
When that module gets parsed by Drupal I get an error objecting to T_RETURN, which doesn't mean anything to me.
I'm not sure how I am meant to proceed once I have fixed these functions, in any case. Do I create a page and issue a PHP call to one of the functions in the body of the page, to display the test form? If so, which function am I meant to call? I am confused by the need to have a menu hook, when I am not using a menu. I've tried reading up on hooks etc. but I get down too deep!
I have not been able to find an overview document that explains the structure of what I am shooting for in a way that I can understand it (or even a working example with all the elements in place). I am hesitant to impose further on your generosity and time, so please point me elsewhere if you know of a good way to get me beyond this point, as it were.
Charles
Charles & others reading this
Charles & others reading this thread: due to the obvious need for better documentation, I've started a step-by-step Drupal documentation web site at www.MissingUbercartManual.com. I've got the first few tutorials written, but not yet open to the public. I'm shooting for later this week to open the door for the first 2-3 tutorials - and this subject should be one of them.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Much appreciated!
Blake,
That's wonderful - thank you. Drupal is such a satisfying tool to use and extend - your making it clearer how to incorporate these kinds of PHP extensions will significantly enhance our experience and our sites. I'd be happy to help/play guinea pig for the documentation, if I can be useful.
Charles
hope this works for me!
I've been driving myself nut trying to modify my template.php file and getting nowhere, it just returns "unexpected T_RETURN" no matter what I try. When I get back into it I'll try this method to theme a CCK input form, which is the goal I'm trying to accomplish. Any reasons it shouldn't work?
I really like the logic behind not wasting time on a method that isn't right to begin with, ill let you know how it goes.
-JB
Came up with the same problem
Came up with the same problem using the module idea, but hey, at least i know now how to make a module! my question though is about what you said in the initial reply that he needs to make sure the input filter for the node is set to PHP. I thought creating a module was a way around having to turn on the PHP evaluator, or I am thinking something different? anyway, I'm wondering if this might be the root of my issue, can you please explain how to turn on PHP for a node type?
thanks!
-JB
JB, I'll have a write up
JB,
I'll have a write up walking through the steps online in a few days at www.MissingUbercartManual.com.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
thanks
sounds good blake, i'll keep an eye out for that. im going to look through your manual as well, ubercart is the next thing i have to tackle for my site, and after getting into it a little today i realized im going to need some help to customize it how i need!
-JB
If course, it's taking me
If course, it's taking me longer than I'd like to get all the tutorials up on the missing ubercart manual site. But you can read the work in progress by becoming a beta-reader...
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
My bit about needing to turn
My bit about needing to turn on the PHP filter is only when wanting to call PHP from your module inside a node. This may be used to display the contents of a database table, or to display the result of some calculating logic. The PHP filter is only used when calling PHP from inside a node's content. With a module, you can create content, such as pages and forms, without a node as the container of the page content.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com