Display custom output if field doesn't have value in hook_field_formatter_view 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' =&gt; $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.
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' =&gt; $output);
      $items[] = array('value' =&gt; $output);
    }
  break;
}


If anyone have better solution please provide.