Categories
Drupal Planet

Drupal 7 Theming Tip: Any Link on an Image Field with Drupal 7

[Dec. 8,2012 – Thanks to Blair Wadman for  noticing that WordPress ate my – – and turned them into en-dashes, thus confusing anyone reading this!]

I’m moving Clutter Puppy – Daily Clutter Tips from static HTML to Drupal 7, but there are quite a few problems I’m running into with theming for Drupal 7, mostly because the documentation is pretty unorganized for Drupal 7 theming. I’m pretty familiar with theming nodes and fields for Drupal 6 with CCK – now that CCK has moved into core for Drupal 7, the API has changed.

In my case, I’m trying to do something simple – on my node, I have two fields that I’d like to use together – “field_url”, which is a text field, and “field_pictures”, which holds an unlimited number of Drupal 7 Image fields. I’d like each image to link to the URL in field_url – that’s not the node URL, it’s just an arbitrary URL that gets entered at content creation.

One thing that really frustrates me about Drupal is how easy this would be with other web application frameworks. Most of them, you’d simply add a foreach loop to your template, and write out the a href and img tags with tip.url and tip.imageUrl. Done!

With Drupal, things get more complicated and the documentation gets horrible as soon as you want to change the default rendering for a Drupal node. With Drupal 7 to do this simple task, you’ll need to modify node.tpl.php, and create a field- -field_pictures.tpl.php template.

In node.tpl.php, put:

<?php print render($content['field_pictures']); ?>

where you want the pictures – this by itself isn’t too bad, but because every field wants to render itself, you’ll need to replace the default field rendering template for field_pictures. Inside of field- -field_pictures.tpl.php, put:

<ul>

<?php foreach ($items as $delta => $item) : ?>

<li><a target="_blank" 

href="<?php print $element['#object']->field_url['und'][0]['value']; 

?>"><?php print render($item); ?></a></li>

<?php endforeach; ?>

</ul>

for a nice unordered list that you can plug into a jQuery plugin, for instance.

The key here was to use the un-Googleable $element[‘#object’] to represent the parent node, and then to retrieve the field_url’s value from the node.

If there’s a better way to do this, I’d love to hear it – the D7 theming guide seems to be missing a discussion of the new D7 fields and how to theme them.

Categories
iPhone Development

Three20 for iPhone: Add JSON support to a new project

Working on a new iPhone app, everything was going smoothly with Three20, following their guide to add Three20 to your application with a Python script. Everything was working smoothly until I wanted to parse some JSON. The ttmodule.py script added the frameworks for the other Three20 modules (considered core) to my XCode project, but didn’t include the JSON+SBJSON module (considered external or optional) in the linker flags. This was a little confusing, as the JSON library was listed as a direct dependency.

I had to add the following lines to my linker flags (from the info window for your target, go to “Build”, then look under the “Linking” header, and then “Other Linker Flags”. You’ll see some Three20 linker flags there, for the SBJSON library, add two lines:

-force_load
../../three20/Build/Products/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/libextThree20JSON+SBJSON.a

Where ../../three20 matches the location of your other Three20 libraries (this will depend on where you put the Three20 source code).

How it looks in XCode:

I hope this helps other iPhone developers who are using Three20! Mostly I put these tips together for my future reference.

Categories
Drupal Planet

Clash between JSON out of Drupal with Youtube Embedded Field and MongoDB Key Names

(Quick background) As part of a much larger project, I’m pulling content out of Drupal as JSON-formatted nodes into MongoDB. The plan is to use Drupal as the content creation platform, and MongoDB as the database store for the content delivery platform, with JSON as the data format.

Importing JSON content from Drupal into MongoDB is actually very easy – I’ve got Drupal 6 already configured to serve JSON through the REST Service (for more, read my post Using Drupal’s Views as a JSON Web Service with the REST Server)

I installed MongoDB on Mac OS X, installed the mongo ruby gem, and then wrote a quick ruby script to pull JSON off my server and dump it into mongo. This worked great – it throws errors when it hits nodes that aren’t published, but that’s fine. What didn’t work was pulling in anything that used the Embedded Media Framework CCK field for Youtube – one of the JSON tags that gets embedded uses the string “http://www.w3.org/2005/Atom” as a key, and the periods aren’t legal in a key name in MongoDB.

Luckily, this was just the one key name in my JSON output, so I added a quick gsub call to replace www.w3.org with www-w3-org, and all was well. I don’t plan to use the Youtube Atom feed anyway, but if I do, I’ll just have to remember to use my key, not the Drupal 6 CCK key.