3

I'm kind of new to this formatter, widget system, so i hope you can help me out. I'm trying to add a new widget to field type list_text, so i have added a info hook like this

function market_field_widget_info() {
  return array(
    'product_list' => array(
    'label' => t('Product List'),
    'field types' => array('list_text'),
    'settings' => array('size' => 40),
    'behaviors' => array(
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
  ),
);

And to my widget i have added a widget form hook like this

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

  if ($instance['widget']['type'] == 'product_list' && arg(0) == 'node' ) {

  $node = node_load(arg(1));

  $options = array();
  foreach($node->field_products['und'] as $item ) {
    $entity = entity_load('field_collection_item', array($item['value']));
    $options[$item['value']] = $entity[$item['value']]->field_title['und'][0]['value'];
  }

  $element['product'] = array(
    '#type' => 'select',
    '#title' => t('Vare'),
    '#options' => $options,
  ); 
return $element;
}

And this works great, i get the information into the selector that i want, but when it is posted nothing is displayed, so i added a view to it

function market_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {
    // This formatter simply outputs the field as text and with a color.
    case 'default_list':
      foreach ($items as $delta => $item) {
        $element[$delta]['#markup'] = "TEST";
      }
      break;
  }

  return $element;
}

But still nothing, am i missing something here? I have tried putting in a die, but its like the view hook is never called, hope you can help.

Best regards

4
  • Have you cleared Drupal's cache since adding the new hook?
    – Clive
    Apr 17, 2012 at 10:44
  • Many many times Apr 17, 2012 at 10:47
  • 1
    Since you don't say what you actually want to do, I want to point out that a widget is used for controlling input to a field, while a formatter controls output from a field. You may not need both.
    – Letharion
    Apr 17, 2012 at 11:01
  • Well actually i just wanted to set the values in the select list, depending on users content in the node. so i thought i could just add a new widget with the values, which i could, but it doesn't show. Apr 17, 2012 at 13:47

1 Answer 1

7

In order for hook_field_formatter_view() to be called you also need to implement hook_field_formatter_info, to let the field system know about your custom formatter.

There's a good article called Creating new field formatters in Drupal 7 that has a step by step guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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