Community

Update: Server error rectified and its drupal illegal string offset warnings now for my code

Hi,

I am writing a very simple custom module to test Filed API module. Once i know how to work with it, i am planning to create my own custom modules.

The code i wrote below does not work properly. Using the module i can create a field but when i go the manage fields page and try to save or make any changes it will give me a server error 500. The same when i try to create content with this field.

I though some problem is there with hook_field_widget_form() but i am not unable to find any. I took this example from some of basic field api tutorials that are present.

Can anyone please point out, where its going wrong ?

Edit: I have rectified the error the below but have got a new error .
The error is rectified by turning on error reporting and i got to know that hook_field_is_empty() function is not defined. I have did that and got some new errors when defining content

The errors are like this

Warning: Invalid argument supplied for foreach() in element_children() (line 6282 of /var/www/html/drupal/includes/common.inc).
Warning: Illegal string offset '#children' in drupal_render() (line 5761 of /var/www/html/drupal/includes/common.inc).
Warning: Illegal string offset '#children' in drupal_render() (line 5771 of /var/www/html/drupal/includes/common.inc).
Warning: Illegal string offset '#children' in drupal_render() (line 5809 of /var/www/html/drupal/includes/common.inc).
Warning: Illegal string offset '#printed' in drupal_render() (line 5816 of /var/www/html/drupal/includes/common.inc).

Does anyone know about these errors ? Is it because of the widget form ?

Edit2
The problems is with p tags in hook_field_formatter_view, i have changed that and its working

Many Thanks,
Kiran

<?php
/**
* Implements hook_field_info()
*/

function event_webpages_field_info() {
  return array(
   
'math_square' => array(
     
'label' => t('Square'),
     
'description' => t('STores small text data'),
     
'default_widget' => 'math_squarefield',
     
'default_formatter' => 'math_squareformat',
      ),
  );
}

/**
* Implements hook_field_schema()
*/
function event_webpages_field_schema($field) {
 
$columns  = array(
   
'math_square' => array(
     
'type' => 'varchar',
     
'length' => 10,
     
'not null' => FALSE,
    ),
  );

return array(
'columns' => $columns);
}

/**
* Implements hook_field_widget_info()
*/
function event_webpages_field_widget_info() {
  return array(
   
'math_squarefield' => array(
      
'label' => t('Text field'),
      
'field types' => array('math_square'),
       ),
    );
}

/**
* Implements hook_field_widget_form()
*/
function event_webpages_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base){
 
$element = $base;
 
$element['math_square'] = $base + array(
   
'#type' => 'textfield',
   
'#default_value' => isset($items[$delta]['math_square']) ? $items[$delta]['math_square'] : NULL,
   
'#title' => t('this is a text field'),
   
'#label' => t('Enter a number'),
  );
 
  return
$element;
}

/**
* Implements hook_field_formatter_info()
*/
function event_webpages_field_formatter_info() {
  return array(
   
'math_squareformat' => array(
      
'label' => t('Default'),
      
'field types' => array('math_square'),
    ),
);
}

/**
*  Implements hook_field_formatter_view()
*/
function event_webpages_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
 
$element = array();

  foreach (
$items as $delta => $item) {
   
$output = $item['math_square'] * $item['math_square'];
   
$element[$delta] = '<p>'. $output .'</p>';
  }
return
$element;
}
?>
nobody click here