#1 November 15, 2012 9:49am

philp
Member
Registered: April 5, 2012
Posts: 25

Retrieving a field type on front-end

Is there any easy way to retrieve the field type for each resource when displaying a module or page? I can't see any obvious way to do it. Reason I want to do this is so that I can use modules for defining forms to appear on the front-end of the site, and want to be able to display different form controls depending on the type of field.

Offline

#2 November 15, 2012 11:25am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Retrieving a field type on front-end

Unfortunately, it doesn't look like there are any simple calls to get that info on the front end right now. I'm going to tweak BigTreeCMS::getTemplate to make it easier in RC2, but for now:

$resource_types = array();
$template = $cms->getTemplate($bigtree["page"]["template"]);
foreach ($template["resources"] as $resource) {
    $resource_types[$resource["id"]] = $resource["type"];
}
foreach ($bigtree["page"]["resources"] as $key => $val) {
    echo "Resource $key has a type of ".$resource_types[$key]."";
}

That would show you all the types of a given page. It probably makes sense to give the resource IDs as the keys of $template["resources"] enabling us to skip the whole $resource_types array so I'll make that change in RC2 (which is long coming, but will be here soon hopefully).

Ok, now for modules! For this you'll need to do a straight up SQL query to get the form ID first (which assumes you only have one form for your module data).

$module = new WhateverModule;
$form = sqlfetch(sqlquery("SELECT id FROM bigtree_module_forms WHERE `table` = '".sqlescape($module->Table)."'"));
$form = BigTreeAutoModule::getForm($form["id"]);

Once you have $form you can loop through $form["fields"] and grab the types. The keys of that array are the resource IDs and there's the "type" column for the type of resource.

Offline

#3 November 15, 2012 11:45am

philp
Member
Registered: April 5, 2012
Posts: 25

Re: Retrieving a field type on front-end

Thanks Tim,


I was thinking about this a bit more, and occurred to me that querying the module tables, as you've mentioned, would be the best solution. I'm thinking that I can even extend the module class to do all the heavy lifting, and just call a single method.


I'm going to play around with this and see how I get on with using the built in getForm() stuff.

Offline

#4 November 15, 2012 11:47am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Retrieving a field type on front-end

Let me know if you come up with any cool method for doing it more easily. It's not something I've needed to do before so I haven't thought about it all that much!

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.