自定义复合CCK字段 - 如何在视图中显示各个子字段?

时间:2010-08-30 20:01:15

标签: drupal module views cck

在Drupal 6中,我基于这个优秀的教程创建了一个简单的定价CCK复合字段模块,其中包含“成本”和“产品”子字段:http://www.poplarware.com/articles/cck_field_module

function usginpricing_field_settings($op, $field) {
  switch ($op) {
    case 'database columns':
      $columns['cost'] = array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE, 'default' => '', 'views' => TRUE);
      $columns['product'] = array('type' => 'varchar', 'length' => 1000, 'not null' => FALSE, 'sortable' => TRUE, 'default' => '', 'views' => TRUE);
      return $columns;
  }
}

我可以在视图中检索复合值但是如何拉出各个子字段?到目前为止,我只知道在哪里修改了视图的响应(CCK hook_field()实现 - case 'views data':),但是否则难以接受:

function usginpricing_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    // Do validation on the field values here. The widget
    // will do its own validation and you cannot make any
    // assumptions about what kind of widget has been used,
    // so don't validate widget values, only field values.
    case 'validate':
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          if ($item['cost'] != '' && !is_numeric(trim($item['cost']))) {
            form_set_error($field['field_name'],t('"%name" is not a valid numeric value',array('%name' => $item['cost'])));
          }
        }
     }
     break;

    case 'sanitize':
      foreach ($items as $delta => $item) {
        foreach ($item as $col => $dat) {
          $items[$delta]['safe_' . $col ] = check_plain($item[ $col ]);
        }
      }
      break;

    // Optional: Make changes to the default $data array
    // created for Views. Omit this if no changes are
    // needed, use it to add a custom handler or make
    // other changes.
    case 'views data':
  // UNFINISHED
      // Start with the $data created by CCK
      // and alter it as needed. The following
      // code illustrates how you would retrieve
      // the necessary data.
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);
      $field_data = $data[$table_alias][$field['field_name'] .'_value'];

      // Make changes to $data as needed here.

      return $data;

  }
}

我很感激任何建议或示例代码!

0 个答案:

没有答案