Problem creating a CCK field - doesn't appear in node view! (no problem)
danyalejandro - June 30, 2009 - 20:58
Im currently creating a module for a new cck field. It does appear in the form, and LOOKS like it's saving data to the database, but it won't show in the node view! I've researched a lot, with no good reasults..
what am I doing wrong? here's my code:
<?php
// $Id: image_ref.module,v 0.3 2009/04/29 20:51:53 danyalejandro Exp $
function image_ref_field_info() {
return array(
'image_ref_number' => array(
'label' => t('Referencia a imagen'),
'description' => t('Guarda una referencia a un nodo tipo imagen.')
),
);
}
function image_ref_install() {
content_notify('install', 'image_ref');
}
function image_ref_uninstall() {
content_notify('uninstall', 'image_ref');
}
function image_ref_enable() {
content_notify('enable', 'image_ref');
}
function image_ref_disable() {
content_notify('disable', 'image_ref');
}
/**
* Implementación de hook_field_settings().
*/
function image_ref_field_settings($op, $field) {
switch ($op) {
case 'database columns':
if ($field['type'] == 'image_ref_number') {
return array(
'value' => array('type' => 'int', 'not null' => FALSE, 'sortable' => TRUE),);
}
case 'views data':
$allowed_values = content_allowed_values($field);
if (count($allowed_values)) {
$data = content_views_field_views_data($field);
$db_info = content_database_info($field);
$table_alias = content_views_tablename($field);
// Filter: Add a 'many to one' filter.
$copy = $data[$table_alias][$field['field_name'] .'_value'];
$copy['title'] = t('@label (!name) - Allowed values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
$copy['filter']['handler'] = 'content_handler_filter_many_to_one';
$copy['filter']['numeric'] = TRUE;
unset($copy['field'], $copy['argument'], $copy['sort']);
$data[$table_alias][$field['field_name'] .'_value_many_to_one'] = $copy;
// Argument: swap the handler to the 'many to one' operator
$data[$table_alias][$field['field_name'] .'_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
$data[$table_alias][$field['field_name'] .'_value']['argument']['numeric'] = TRUE;
return $data;
}
break;
}
}
/**
* Implementación de hook_field().
*/
function image_ref_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
/*
case 'view':
foreach ($items as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $items, $teaser, $page);
break;*/
case 'sanitize':
foreach ($items as $delta => $item) {
foreach ( $item as $col => $dat ) {
$items[$delta]['safe_' . $col ] = check_plain($item[ $col ]);
}
}
break;
}
}
/**
* Implementación de hook_content_is_empty().
*/
function image_ref_content_is_empty($item, $field) {
if (empty($item['value']) && (string)$item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* Implementación de FAPI hook_elements().
*/
function image_ref_elements() {
return array(
'image_ref' => array(
'#input' => TRUE,
'#columns' => array('value'), '#delta' => 0,
'#process' => array('image_ref_process'),
),
);
}
/**
* Process an individual element.
*
* Build the form element. When creating a form using FAPI #process,
* note that $element['#value'] is already set.
*
* The $fields array is in $form['#field_info'][$element['#field_name']].
*/
function image_ref_process($element, $edit, $form_state, $form) {
drupal_add_js(drupal_get_path('module', 'image_ref') .'/image_ref_js.js');
$field_name = $element['#field_name'];
$field = $form['#field_info'][$field_name];
$field_key = $element['#columns'][0];
$value = isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : '';
$value = isset($field['decimal']) ? str_replace('.', $field['decimal'], $value) : $value;
$element[$field_key] = array(
'#type' => 'textfield',
'#default_value' => $value,
'#size' => isset($field['precision']) ? $field['precision'] + 2 : 12,
'#maxlength' => isset($field['precision']) ? $field['precision'] : 10,
'#attributes' => array('class' => 'image_ref'),
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
'#field_name' => $element['#field_name'],
'#type_name' => $element['#type_name'],
'#delta' => $element['#delta'],
'#columns' => $element['#columns'],
);
$prefixes = array();
$suffixes = array();
// Make sure we don't wipe out element validation added elsewhere.
if (empty($element['#element_validate'])) {
$element['#element_validate'] = array();
}
$element['#element_validate'][] = 'image_ref_number_validate';
// Used so that hook_field('validate') knows where to flag an error.
$element['_error_element'] = array(
'#type' => 'value',
'#value' => implode('][', array_merge($element['#parents'], array($field_key))),
);
return $element;
}
/**
* Implementación de hook_widget_info().
*/
function image_ref_widget_info() {
return array(
'image_ref' => array(
'label' => t('Selector de imagen (IB)'),
'field types' => array('image_ref_number'),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
/**
* Implementación de hook_widget().
*/
function image_ref_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
);
return $element;
}
/**
* Implementación de hook_field_formatter_info().
*/
function image_ref_field_formatter_info() {
return array(
'default' => array(
'label' => t('ID del nodo imagen'),
'field types' => array('image_ref_number')
),
);
}
/**
* Implementación de hook_theme().
*/
function image_ref_theme() {
return array(
'image_ref' => array('arguments' => array('element' => NULL)),
'image_ref_formatter_default' => array('arguments' => array('element' => NULL)),
);
}
/**
* Proxy theme function for formatters.
*/
function theme_image_ref_formatter_default($element) {
if( empty( $element['#item'] )) {
return '';
}
$text = check_plain($element['#item']['data']['value']);
return $text;
}
/**
* FAPI validation of an individual integer element.
*/
function image_ref_number_validate($element, &$form_state) {
$field_name = $element['#field_name'];
$type_name = $element['#type_name'];
$field = content_fields($field_name, $type_name);
$field_key = $element['#columns'][0];
$value = $element['#value'][$field_key];
if (($element[$field_key]['#required'] || !empty($value))) {
$start = $value;
$value = preg_replace('@[^-0-9]@', '', $value);
if ($start != $value) {
$error_field = implode('][', $element['#parents']) .']['. $field_key;
form_set_error($error_field, t('Only numbers are allowed in %field.', array('%field' => t($field['widget']['label']))));
}
else {
form_set_value($element[$field_key], $value, $form_state);
}
}
}
/**
* FAPI theme for an individual element.
*
* theme and the HTML output lives in $element['#children'].
* Override this theme to make custom changes to the output.
*
* $element['#field_name'] contains the field name
* $element['#delta] is the position of this element in the group
*/
function theme_image_ref($element) {
return $element['#children'];
}
?>
the module had no problems...
the module had no problems... i just needed to add it to my theme..
oh well, at least it should be a good example for a cck field...
Hi, can you be more specific
Hi,
Was wondering, can you be more specific about how you resolved this?
I seem to be having problems creating a node too. Just all of a sudden I get "Page not found" after clicking Save..