0

Display custom output of field in hook_field_formatter_view() if field is empty.

function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  //My Snippet to display custom output
  // Here $items variable is empty.
  $element = array();
  switch ($instance ['widget'] ['type']) {
  case 'my_widget' :
    if (!isset($items[0])) {
      $output = generate_custom_output();
      $element [0] = array ('#markup' => $output );
    }
    break;
  }
  return $element;
}

my custom field is empty, no $item is available. Now I want to return custom output. But above code doesn't work.

Please suggest any solution.

1 Answer 1

0

I am able to do this as below in hook_field_formatter_view().

switch ($instance ['widget'] ['type']) {
  case 'my_widget' :
    if (empty ( $items )) {
      $output = generate_custom_output();
      $element[]['#markup'] = $output;
      $element ['#items'][] = array('value' => $output);
      $items[] = array('value' => $output);
    }
  break;
}

If anyone have better solution please provide.

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.