Ive created a custom field type as a module using the Field API.
The field is a textfield that I then enhance with javascript to be hidden and its replaced with a custom world map where the user can drag a pin point around to mark a location (basic pixel coords - not google maps related).
It all works fine and the input's value is updated with a js array string of x/y pixel numbers ex: "[100,200]".
The problem is after submit the string is saved to the DB as "[" but the db field is a varchar(255).
I also tried making the string only numbers seperated with a comma like so "100,200" but only "1" is saved so its not an issue with the square brackets (its just a string after all) for some reason only the first chracter is saved.

Would really appreciate some advice on what could be going wrong

Cheers

Comments

blisteringherb’s picture

You can potentially run into this issue if you try to set a string $foo, to a value like $foo['bar'] = 'value';, so I'd check to make sure that any arrays that you're creating are properly declared before you're setting values in the array, i.e. $foo = array();. If that doesn't help, post the code.

Icon.Inc’s picture

Heres the .module code it may be Im missing something since I had to assemble it from various sources since no single example fully explains what each method is for:

function ubu_loc_pin_help($path, $arg)
{
	switch ($path) {
    	case "admin/help#ubu_loc_pin":
    		return '<p>'.  t("Custom UBU field to store map location data") .'</p>';
    		break;
	}
}

function ubu_loc_pin_field_info()
{
	return array(
    'ubu_loc_pin' => array(
      'label' => t('Map Pin Location'),
      'description' => t('Map Pin location coordinates relative to map image'),
      'default_widget' => 'ubu_loc_pin_widget',
      'default_formatter' => 'ubu_loc_pin_formatter',
    ),
  );
}

function ubu_loc_pin_field_widget_info()
{
	return array(
		'ubu_pin_loc' => array(
			'label' => t('Map location'),
			'field types' => array('ubu_loc_pin'),
			  'behaviors' => array(
	        	'multiple values' => FIELD_BEHAVIOR_DEFAULT, 
	        	'default value' => FIELD_BEHAVIOR_NONE,
	          ),
		),
	);
}



function ubu_loc_pin_field_formatter_info()
{
	return array(
      'ubu_loc_pin_formatter' => array(
          'label' => t('Map location formatter'),
          'description' => t('does stuff'),
		'field types' => array('ubu_loc_pin'),
		
      ),
  );
}



/*function ubu_loc_pin_field_settings_form($field, $instance, $has_data)
{
	//$settings = $field['settings'];
	
}*/

function ubu_loc_pin_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element)
{
	$element += array(
		'#type' => 'textfield',
		'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
		'#suffix' => '<div class="ubu_loc_pin_field"></div>',
        '#attributes' => array('class' => array('ubu_loc_pin_field')),
        '#attached' => array(
          'js' => array(drupal_get_path('module', 'ubu_loc_pin') . '/ubu_loc_pin.js'),
	'css' => array('assets/css/map.css',drupal_get_path('module', 'ubu_loc_pin') . '/ubu_loc_pin.css'),
        ),
	);
	drupal_add_library('system', 'ui.draggable');
	return $element;
}



function ubu_loc_pin_field_is_empty($item, $field)
{
	
}

function ubu_loc_pin_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display)
{
	$element = array();

	  switch ($display['type']) {
	    case 'ubu_loc_pin_formatter':
	      foreach ($items as $delta => $item) {
	      	
	           if ($item['ubu_loc_pin']) {
	                $element[$delta]['#markup'] = '<span>' . $item['ubu_loc_pin'] .'</span>';
	            }
	      }
	      break;
	  }
	  return $element;
}

The .install files schema

function ubu_loc_pin_field_schema($field)
{
	return array(
         'columns' => array(
	         'ubu_loc_pin' => array(
	            'type' => 'varchar',
	            'not null' => FALSE,
	            'length' => '100',
	            'default'  => '',
	            ),
	         ),
	     );
}