August Drupal Meetup (Madison)

Events happening in the community are now at Drupal community events on www.drupal.org.
blakehall's picture
Start: 
2008-08-12 18:00 - 20:00 US/Central
Event type: 
User group meeting

We're meeting early this month...

Neil's giving a talk on Developing for CCK. Here's what he has to say:

CCK has become the preferred way to create new node types in Drupal. Instead of writing node modules, Drupal developers are now writing field modules to be used to build the nodes they need. This discussion will cover the basics of the CCK Field API for Drupal 5 and discuss the Drupal Form API. We will walk through a simple field module and cover some of the resources that are available for CCK development. While this discussion is focused on CCK for Drupal 5, the basics about the CCK framework can be applied to Drupal 6. CCK in Drupal 6 has undergone some changes which will be touched on briefly if we have time.

We'll meet at the usual place, Netconcepts: 2820 Walton Commons

RSVP here, or on Meetup

Comments

Field module code

posco's picture

Here is the working version of the code I wrote tonight. Like I said at the meeting, the problem was that I did not implement hook_widget_info(). The hook_widget_info() is also used to create multiple widgets for one field module, which is something that came up, but I didn't have an answer for. Look at the $field array in hook_widget(). You should be able to see what type of widget was selected by the user in there.

Also, note this new line of code in the form case for field_module_widget():

[...]
case 'form':
      $form = array();
-->   $form[$field['field_name']] = array('#tree' => TRUE);
      $form[$field['field_name']][0]['value'] = array(
        '#type' => 'textfield',
        '#title' => t('Test Field Module'),
      );
[...]

You must declare this line for all widget forms or your module will not work. You can safely copy and paste this line as it is the same for all field modules.

I implemented the hook_field_formatter_info() and hook_field_formatter() in the last lines of the module. I was also mistaken when I said that hook_field_formatter() is called for every field type. This was a misconception I had early on when I first developed a CCK field and for some reason it stuck in my head. hook_field_formatter() pertains to your field module only. The if statement is only there to make sure the array key is set. You can also use the $formatter argument in a switch statement to determine which formatter is being used.

If anyone has any question, please ask them in this thread.

I hope everyone was able to take away something from the discussion tonight. I know I rambled on a little bit.

field_module.module:

<?php
// $Id$

/**
* @file
* This is a test field module using CCK and Drupal 5.x.
*/

function field_module_field_info() {
  return array(
    'field_module' => array('label' => t('Field Module')),
  );
}

function field_module_field_settings($op, $field) {
  switch($op) {
    case 'form':
      $form = array();
      $form['type'] = array(
        '#type' => 'textfield',
        '#title' => t('Type'),
      );
      return $form;
   
    case 'save':
      return array('type');
     
    case 'validate':
      drupal_set_message($field['type']);
     
    case 'database columns':
      return array(
        'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
      );
  }
}

function field_module_widget_settings($op, $widget) {
  switch($op) {
    case 'form':
      $form = array();
      $form['type2'] = array(
        '#type' => 'textfield',
        '#title' => t('Type 2'),
      );
      return $form;
   
    case 'validate':
      break;
   
    case 'save':
      return array('type2');
  }
}

function field_module_widget_info() {
  return array(
    'text' => array(
      'label' => t('Text Field'),
      'field types' => array('field_module'),
    ),
  );
}

function field_module_widget($op, &$node, $field, &$node_field) {
  switch($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array('#tree' => TRUE);
      $form[$field['field_name']][0]['value'] = array(
        '#type' => 'textfield',
        '#title' => t('Test Field Module'),
      );
      return $form;
   
    case 'validate':
      if (!is_numeric($node_field[0]['value'])) {
        form_set_error($field['field_name'], t('Your value wasn\'t a number.'));
      }
      break;
  }
}

function field_module_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array('field_module'),
    ),
  );
}

function field_module_field_formatter($field, $item, $formatter, $node) {
  if (!isset($item['value'])) {
    return '';
  }
  else {
    return check_plain($item['value']);
  }
}

field_module.info:

; $Id$
name = Field Module
description = "Test field module"

; Required core version (Drupal 6+; no effect in Drupal 5)
core = 6.x


; Package name (see http://drupal.org/node/101009 for a list of names)
; package =


; PHP version requirement (Drupal 6+)
; php = 5.2


; Module dependencies (Drupal 6+ style)
; dependencies[] = mymodule
; dependencies[] = theirmodule

; Module dependencies (Drupal 5 style)
; dependencies = mymodule theirmodule



; For further information about configuration options, see
; - http://drupal.org/node/101009 (Drupal 5)
; - http://drupal.org/node/231036 (Drupal 6)

Introduction

sambrown's picture

Hay guys!!!
This is sambrown. Iam a junior developer in dotnet technologies. Will you please tell me about this , what type of language is this. i cant understand this, is this php or what.
Sambrown
Wisconsin Treatment Centers

Wordpress Site

dckantor's picture

Are you asking about the site at Wisconsin Treatment Centers?
Thats a Wordpress site (PHP, MySQL).

Wisconsin

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: