Custom Drupal 7 Field only saves the first character
Asked Answered
B

3

4

I'm essentially trying to create my first field type module using the new Drupal 7 Field API. I've managed to get it to display correctly in the "edit" view.
However, when I try to save some data it only saves the first character.

Here's the module:

<?php

function youtubefield_field_info() {
  return array(
    'youtubefield_video' => array(
      'label' => t('Youtube video'),
      'description' => t('This field stores a youtube video ID and displays the video associated with it'),
      'settings' => array(
        'max_length' => 11,
      ),
      'instance_settings' => array(
        'text_processing' => false,
      ),
      'default_widget' => 'youtubefield_video_widget',
      'default_formatter' => 'youtubefield_video_formatter',
    ),
  );
}

function youtubefield_field_widget_info() {
  return array(
    'youtubefield_video_widget' => array(
      'label' => t('Default'),
      'field types' => array('youtubefield_video'),
    ),
  );
}

function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element['#type'] = 'textfield';
  $element['#default_value'] = isset($items[$delta]) ? $items[$delta] : '';
  return $element;
}

function youtubefield_field_is_empty($item, $field) {
  return !$item;
}

function youtubefield_field_formatter_info() {
  return array(
    'youtubefield_video_formatter' => array(
      'label' => t('Youtube video'),
      'field types' => array('youtubefield_video'),
    ),
  );
}

And here's the installer:

<?php

function youtubefield_field_schema($field) {
  return array(
    'columns' => array(
      'value' => array(
        'type' => 'varchar',
        'length' => 11,
        'not null' => true,
      ),
    ),
    'indexes' => array(),
  );
}

EDIT: The problem seems to be related to the widget, as it works fine when it gets the data programmatically.

Bulb answered 21/6, 2011 at 13:48 Comment(2)
@Laxman13, wouldn't it be better to prefix the field-api tag with drupal-?Schwaben
Yes, wasn't thinking, thanks.Cameleer
M
6

This widget form worked for me:

function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta,\
 $element) {
  $main_widget = array();
  switch ($instance['widget']['type']) {
  case 'youtubefield_video_widget':
    $main_widget = $element + array(
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
      );
    break;
  }
  $element['value'] = $main_widget;
  return $element;
}
Metanephros answered 27/6, 2011 at 15:22 Comment(1)
Thanks! After some debugging I found that the solution from this answer is the render array returning the $element array with value as keyvalueSoggy
D
1

I had the same problem and discoverd hook_field_widget_form() needs to return: "$element['value']"

function field_youtube_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

    $element['value'] = array(
        '#type' => 'textfield', 
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
    );

    return $element;
}
Disseminule answered 3/12, 2011 at 17:0 Comment(0)
S
0

Change your widget form to:

function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { 
  switch ($instance['widget']['type']) {  
      case 'youtubefield_video_widget':  
        $element['youtubefield_video'] = array(
            '#type' => 'textfield',  
            '#title' => $element['#title'],  
            '#description' => $element['#description'], 
            '#default_value' => isset($items[$delta]['youtubefield_video']) ? $items[$delta]['youtubefield_video'] : NULL,  
            '#required' => $element['#required'],  
            '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,  
            '#delta' => $delta,  
        ); 

      break; 
  } 
  return $element;
}
Spadework answered 27/6, 2011 at 1:49 Comment(1)
Thanks for the response. Can you offer an explanation, please? :)Cookgeneral

© 2022 - 2024 — McMap. All rights reserved.