Hi

I am using the following to render a field :

$field = field_get_items('node', $proposal_node, 'field_working_title_of_award');
$field_working_title_of_award_value = field_view_value('node', $proposal_node, 'field_working_title_of_award', $field[0]);
drupal_render($field_working_title_of_award_value);

However this bypasses the empty field settings on that field. Is there a way to programmatically deliver the field with the appropriate empty text returned if appropriate ?

Thanks

Comments

Alan D.’s picture

Status: Active » Fixed

A fully integrated method is a bit involved.

So if the field is empty, you can either manually update the output in the template or integrate via (untested):

$field_name = 'field_working_title_of_award'; // change as required
$view_mode = 'full'; // change as required
if (field_get_items('node', $node, $field_name)) {
  // there are items  
}
else {
  // empty
  $instance = field_info_instance('node', $node, $field_name);
  $display = field_get_display($instance, $view_mode, $node);
  
  if (!empty($display['settings']['empty_fields_handler'])) {
    $plugin_type = $display['settings']['empty_fields_handler'];
    
    $field = field_info_field($field_name);

    // Provide additional context for the handlers.
    $context['field_name'] = $field_name;
    $context['field'] = $field;
    $context['instance'] = $instance;

    if ($plugin = empty_fields_load_plugin($instance, $display['settings'])) {
      $markup = $plugin->react($context);
      // Do not render empty values.
      if (!empty($markup) || drupal_strlen($markup)) {
        $output = array(
          '#theme' => 'field',
          '#title' => $instance['label'],
          '#label_display' => $display['label'],
          '#weight' => $display['weight'],
          '#field_type' => $field['type'],
          '#field_name' => $field_name,
          '#bundle' => $bundle,
          '#object' => $entity,
          '#entity_type' => $entity_type,
          '#items' => array(
            0 => array('value' => $markup),
          ),
          0 => array(
            '#markup' => $markup,
          ),
        );

        // Allow plugins to alter output.
        $plugin->alterOutput($output, $context);

        // Print to the screen.
        print drupal_render($output);
      }
    }
  }
}

Reopen if you have any issues. Personally, I would just manually insert the empty text unless it is important to link to the view display settings

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

missing word