Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-06-10 10:00:24

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Multiple data items in custom fields

This is one of those ‘brain freeze’ moments, where I need some advice on the best way to achieve something…

During my time in Geneva, I’ve been updating my website with articles about the places we’ve been to. Each article has a custom field for the lattitude and longitude of a place and another custom field for the name of the place (which might be different from the title of the article). I’ve just managed to pull it all together so that they display on a Google map (see here). It’s still a work in progress eg. I need to include article titles, link to the article, possibly an image etc.

Looking back over the articles, for some trips I’ve included multiple places in one article. For example, when we went to Belgium we stayed in Nieuwpoort but Ypres and Bruges are included in the same article. Ideally, I would like to have each of those places shown on the map too.

Some options:

  • break those articles into separate ones ie. one article per place,
  • additional custom fields for each extra place (that would be two additional fields each, for who knows how many places I might mention in a single article),
  • use a single custom field but ‘packed’ using some sort of standard format so that it can hold more than one item.

Does anyone have suggestions on what would be best practice? As always, I want to implement something that doesn’t just work now but will also be easier for maintenance into the future as well.

Thanks.

Offline

#2 2015-06-10 10:46:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Multiple data items in custom fields

Separate articles is the cleanest solution, but if for stylistic reasons you would prefer to keep the locations together, you could do worse than give sed_pcf a spin.

As long as you have enough space in the custom field’s 255 char limit, that’ll work a treat. The plugin allows you to split and iterate over the packed field content via some handy tags. The syntax takes a bit of getting used to, but it’s fairly readable / maintainable.

Alternatively, you may be able to roll your own system and take advantage of the newer <txp:if_custom_field> features for pattern matching. Or smd_if / rah_repeat can also help if you happen to have those installed.

Tangentially, fwiw, have you considered MarkerClusterer. It not only speeds up mapping by only showing aggregate pins at high zoom levels, it’s also pretty easy to generate the required JSON data from Txp article tags and custom fields. Worth a look in my opinion.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2015-06-10 11:27:03

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Multiple data items in custom fields

Bloke wrote #291475:

it’s also pretty easy to generate the required JSON data from Txp article tags and custom fields.

Why not simply store these data in JSON format in one custom field, e.g.

"Nieuwpoort ":[1.22,3.45], "Ypres":[6.56,7.45]

and then pass {<txp:custom_field name="places" />} to some js map handler?

Offline

#4 2015-06-10 12:08:07

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Multiple data items in custom fields

etc wrote #291477:

Why not simply store these data in JSON format in one custom field, e.g.

Umm, I think we’re talking about the same thing. How you store your data is a matter of preference: JSON, packed array, whatever. The point I was making (that may have got lost, sorry) is that you can just use something like this if you wish:

{
"locations":
   <txp:article_custom section="places" limit="1000">
      {
      "name": "<txp:title />",
      "URL": "<txp:permlink />",
      "latitude": "<txp:whatever split="|" piece="1"><txp:custom_field name="location" /></txp:whatever>",
      "longitude": "<txp:whatever split="|" piece="2"><txp:custom_field name="location" /></txp:whatever>",
      <txp:if_article_image>"image": <txp:images><txp:image_url /></txp:images>,</txp:if_article_image>
      "...": ...
      },
   </txp:article_custom>
}

With a bit of trailing-comma jiggery pokery, that can be stashed in a txp:variable for later use or just injected into an on-page JS array that MarkerClusterer can use. It needs all your marker data in one big JSON block so it can aggregate it, but you can design the output however you like.

In one implementation, we did this with a custom hook: whenever you saved an article it CURLed a hidden Txp section that had the above as a Page template, which wrote the JSON to a file. That file was then read in on public page views to build the on-page JS array. It was faster to pre-build the data file than calling article_custom on every page. And that system gave the client an ability to tweak what data was written to the file by changing the Page template, rather than hard-coding it in a plugin.

[Edited spurious code example and added example]

Last edited by Bloke (2015-06-10 12:19:12)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2015-06-10 12:21:35

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: Multiple data items in custom fields

Bloke wrote #291475:

have you considered “MarkerClusterer”

Oh yes, that’s definitely on my list of things to do. But I was pretty chuffed to get the thing working in the first place!

etc wrote #291477:

Why not simply store these data in JSON format in one custom field.

This too is on my list. But now you’ve shown me that by ‘compressing’ the data in a single field it becomes JSON-friendly it makes more sense to look into that.

I guess from a data-purity perspective, it makes sense to split the articles apart. When I started writing, I had no real plans and therefore the articles are all over the place (eg. one city split into multiple articles, one article covering multiple places). I know that I need to go back and clean up a lot of the data anyway.

At least when I go home, my wings will be clipped – lot’s of time to fix this stuff up then.

Offline

Board footer

Powered by FluxBB