Skip to main content

Working with Drupal fields

Fields in Drupal are used as the building blocks for your content, fields can be added to various entity types to allow you complete control over the data that you store. Drupal offers a wide range of field types both out of the box and through a number of contributed modules so you can be very specific over the types of data each field should use. In addition to this you can also control the type of widget used to collect the data and how this data gets displayed to your users using field formatters. See below for some of our article on Drupal fields....

Read some of our articles about Drupal fields

Add stuff to a node and configure it like fields

28th Feb 2012

UPDATE: The Extra Field and Extra Field Settings Provider modules facilitate doing this sort of thing in Drupal 8. Read on for older Drupal sites, and for the concepts behind this.

We often want to add things to the content of a node or any other entity in Drupal 7 using hook_node_view(), hook_node_view_alter() or a similar hook in a custom module. This could be anything from a custom social media link, a field rendered in a custom way, additional author information or virtually anything else.

The rendered node with our pseudo-field

Here's the code used to add that Facebook like button:


/**
 * Implements...
Read more

Rendering Drupal 7 fields (the right way)

Stephen Tweeddale
7th Sep 2011

Drupal 7 brought us Entities, and with them the powerful Field API for 'storing, loading, editing, and rendering field data.' attached to them. If you're managing everything through 'manage fields' and 'manage display' tabs of your content type, then every part of that process is rather wonderfully taken care of for you.

We often, however, come across the need to render a field outside the context of it's entity. A common example might include rendering a node's author in a sidebar block. Sure, modules like Panels and CCK Blocks will do this for you, but doing it manually is actually not that hard.

Read more

Changing Display Suite fields in update hooks

10th Feb 2011

We've been using Display Suite with Node displays on one of our latest sites. It was an experiment, and possibly not something we'll do again - DS offers some great functionality with build modes and ways to re-arrange fields and all sorts on nodes, but since we end up theming most nodes quite a lot anyway, it's not worth the extra hassle that Display Suite can add when you want to change the theming on DS fields, layouts, change the fields' settings etc...

We use update hooks to change most things on sites for clients, since this allows us to...

Read more

Creating new field formatters in Drupal 7

3rd Feb 2011

Creating extra field formatters in Drupal 7 is fairly simple, but it can take some poking around in the new Fields code. So here is some sample code to make it easier for you! You can add formats to your own fields -- or existing fields that are defined by other Drupal modules. This is the case for the following example - a formatter for a link field to display the URL as an absolute URL.

First, implement hook_field_formatter_info() to declare your field formatter to the fields API, and implement hook_field_formatter_view() which tells Drupal what to do when viewing the...

Read more

Certified to Rock field

19th Jan 2011

Certified to Rock is a site that allows you to look-up Drupal.org user names, and
see their 'certified to rock' score.

CertifiedToRock.com is a way that community members and employers can get a
sense of someone's involvement with the Drupal project. The site uses a custom
algorithm that gathers publicly available information which is then distilled
down into a score on a scale from one to eleven.

I've been wanting to build an easy way to bring this rating into a Drupal site to add an additional metric to user profiles. Until recently this wouldn't have been a simple undertaking...

Read more

Ensuring a CCK field has unique values

27th Jan 2009

Super quick one this: say you have a CCK field on a particular node whose values you want to ensure are unique across all nodes on your site, you could write yourself a nice little helper module, or some PHP directly into the CCK field validation section of the admin form, but, a helper module already exists!
Unique field is a lovely little module that allows you to say that a particular text, number or date field should have unique values across the site, or indeed in that node.

I used this on a user's profile node for a 'alias'...

Read more