2

In a module I'm going to implement some Field types using Field API. this is how hook_field_info() looks like in my module.

function myModule_field_info()
{
  return array(
    'type1' => array(
      'label' => t('Custom type 1'),
      'description' => t('Just for test'),
      'default_widget' => 'type1_textbox',
      'default_formatter' => 'type1_rawview',
    ),
    'type2' => array(
      'label' => t('Custom type 2'),
      'description' => t('Just for test'),
      'default_widget' => 'type2_textbox',
      'default_formatter' => 'type2_rawview',
    ),
  );
}

and the hook_field_widget_info() implementation is :

function myModule_field_widget_info()
{
  return array(
    'type1_textbox' => array(
      'label' => t('Text field'),
      'field types' => array('type1'),
    ),
    'type2_textbox' => array(
      'label' => t('Text field'),
      'field types' => array('type2'),
    ),
  );
}

off-course this is just an example and each widget has its especial form . now consider that each of my field types has more than one element .

  • Field type 1
    • first name
    • last name
  • Field type 2
    • phone number
    • address
    • email

now the question is how should I implement widget form for a multiple field type like this?

I tried this code but no chance :

function myModule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
{
  if($field['cardinality'] == 1){
    $element['#type'] = 'fieldset';
  }
  switch ($instance['widget']['type']) {
    case 'type1_textbox' :
      $type1 = array();
      $type1['firstName'] = array(
        '#type' => 'textfield',
        '#field_prefix' => t('First Name : '),
        '#size' => 20,
        '#default_value' => isset($items[$delta]['firstName']) ? $items[$delta]['firstName'] : '',
      );
      $type1['lastName'] = array(
        '#type' => 'textfield',
        '#field_prefix' => t('Last Name : '),
        '#size' => 20,
        '#default_value' => isset($items[$delta]['lastName']) ? $items[$delta]['lastName'] : '',
      );
      $element += $type1;
      break;
    case 'type2_textbox' :
      $type2 = array();
      $type2['phoneNumber'] = array(
        '#type' => 'textfield',
        '#field_prefix' => t('Phone Number : '),
        '#size' => 20,
        '#default_value' => isset($items[$delta]['phoneNumber']) ? $items[$delta]['phoneNumber'] : '',
      );
      $type2['address'] = array(
        '#type' => 'textarea',
        '#field_prefix' => t('Address : '),
        '#size' => 60,
        '#default_value' => isset($items[$delta]['address']) ? $items[$delta]['address'] : '',
      );
      $type2['email'] = array(
        '#type' => 'textfield',
        '#field_prefix' => t('Email : '),
        '#size' => 20,
        '#default_value' => isset($items[$delta]['email']) ? $items[$delta]['type2']['email'] : '',
      );
      $element += $type2;
      break;
  return $element;
}

The code was edited and now no widget is shown in node-edit form and when I went to field management section this error is appearing

enter image description here

  • What you have looks correct...what do you mean by "no chance" exactly? – Clive Sep 17 '15 at 12:05
  • @Clive default_value does not show up on edit form :( – Alireza Tabatabaeian Sep 17 '15 at 12:32
  • Oh right, $items[$delta]['type2']['address'] needs to be $items[$delta]['address'], and similar for the rest. It's so important to include what's actually wrong in your question – Clive Sep 17 '15 at 12:35
0

the name of the form in myModule_field_widget_form must be the same column in your .install file make sure it matches the same column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.