You are here

function fieldgroup_nodeapi in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 5 fieldgroup.module \fieldgroup_nodeapi()
  2. 6.3 modules/fieldgroup/fieldgroup.module \fieldgroup_nodeapi()
  3. 6.2 modules/fieldgroup/fieldgroup.module \fieldgroup_nodeapi()

Implementation of hook_nodeapi().

File

modules/fieldgroup/fieldgroup.module, line 415
Create field groups for CCK fields.

Code

function fieldgroup_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      $context = $teaser ? 'teaser' : 'full';
      foreach (fieldgroup_groups($node->type) as $group_name => $group) {
        $label = $group['settings']['display']['label'] == 'above';
        $element = array(
          '#title' => $label ? t($group['label']) : '',
          '#description' => $label ? t($group['settings']['display']['description']) : '',
          '#weight' => $group['weight'],
        );
        switch ($group['settings']['display'][$context]['format']) {
          case 'simple':
            $element['#type'] = 'fieldgroup_simple';
            $element['#group_name'] = $group_name;
            break;
          case 'hidden':
            $element['#access'] = FALSE;
            break;
          case 'fieldset_collapsed':
            $element['#collapsed'] = TRUE;
          case 'fieldset_collapsible':
            $element['#collapsible'] = TRUE;
          case 'fieldset':
            $element['#type'] = 'fieldset';
            $element['#attributes'] = array(
              'class' => 'fieldgroup ' . strtr($group['group_name'], '_', '-'),
            );
            break;
        }
        $node->content[$group_name] = $element;
        foreach ($group['fields'] as $field_name => $field) {
          if (isset($node->content[$field_name])) {
            $node->content[$group_name][$field_name] = $node->content[$field_name];
            unset($node->content[$field_name]);
          }
        }
      }
      break;
    case 'alter':

      // Add back the formatted values in the 'view' element,
      // so that node templates can use it.
      $context = $teaser ? 'teaser' : 'full';
      foreach (fieldgroup_groups($node->type) as $group_name => $group) {
        foreach ($group['fields'] as $field_name => $field) {
          if (isset($node->content[$group_name][$field['field_name']])) {
            $element = $node->content[$group_name][$field['field_name']];
            if ($element['#single']) {

              // Single value formatter.
              foreach (element_children($element['items']) as $delta) {

                // Use isset() to avoid undefined index message on #children when field values are empty.
                $node->{$field_name}[$delta]['view'] = isset($element['items'][$delta]['#children']) ? $element['items'][$delta]['#children'] : '';
              }
            }
            else {

              // Multiple values formatter.
              $node->{$field_name}[0]['view'] = $element['items']['#children'];
            }
          }
          else {
            $node->{$field_name}[0]['view'] = '';
          }
        }
      }
      break;
  }
}