function bunsen_field_attach_view_alter(&$output, $context) { // We proceed only on nodes. if ($context['entity_type'] != 'node' || $context['view_mode'] != 'full') { return; } $node = $context['entity']; // Load all instances of the fields for the node. $instances = _field_invoke_get_instances('node', $node->type, array('default' => TRUE, 'deleted' => FALSE)); foreach ($instances as $field_name => $instance) { // Only work with fields that we display or that have empty values $access = !empty($output[$field_name]['#access']) ? $output[$field_name]['#access'] : FALSE; if ($access || empty($node->{$field_name})) { // Set content for fields if they are empty. if (empty($node->{$field_name})) { $display = field_get_display($instance, 'full', $node); // Do not add field that is hidden in current display. if ($display['type'] == 'hidden') { continue; } // Load field settings. $field = field_info_field($field_name); // Set output for field. $output[$field_name] = array('#theme' => 'field', '#title' => $instance['label'], '#label_display' => 'above', '#field_type' => $field['type'], '#field_name' => $field_name, '#bundle' => $node->type, '#object' => $node, '#items' => array(array()), '#entity_type' => 'node', '#weight' => $display['weight'], 0 => array('#markup' => '')); } } } }
function cubert_field_attach_view_alter(&$output, $context) { if (!empty($context['entity']) && !empty($context['entity']->type) && $context['entity']->type == 'project_issue') { global $user; // Some fields can only be edited by admin users $admin_fields = array('field_issue_priority', 'field_issue_component', 'field_task_type', 'field_issue_status', 'milestone_fieldset', 'field_issue_assigned', 'field_due_date', 'time_tracker', 'time_estimate'); // We proceed only on nodes. if ($context['entity_type'] != 'node' || $context['view_mode'] != 'full') { return; } $node = $context['entity']; $is_admin = user_access('administer projects'); $can_edit = node_access('update', $node); // Load all instances of the fields for the node. $instances = _field_invoke_get_instances('node', $node->type, array('default' => TRUE, 'deleted' => FALSE)); // Load extra fields $extra_fields = field_info_extra_fields('node', $node->type, 'form'); if (!empty($extra_fields['title'])) { $title['label'] = 'Title'; $instances['title'] = $title; } foreach ($instances as $field_name => $instance) { // Only work with fields that we display or that have empty values $access = !empty($output[$field_name]['#access']) ? $output[$field_name]['#access'] : FALSE; if ($field_name == 'title') { $access = TRUE; } $value_empty = FALSE; if (!empty($node->{$field_name}[LANGUAGE_NONE])) { if (array_key_exists('value', $node->{$field_name}[LANGUAGE_NONE][0])) { $value_empty = empty($node->{$field_name}[LANGUAGE_NONE][0]['value']); } } if ($access || empty($node->{$field_name}) || $value_empty) { // Set content for fields if they are empty. if ((empty($node->{$field_name}) || $value_empty) && $field_name != 'title') { $display = field_get_display($instance, 'full', $node); // Do not add field that is hidden in current display. if ($display['type'] == 'hidden') { continue; } // Load field settings. $field = field_info_field($field_name); // Set output for field. $output[$field_name] = array('#theme' => 'field', '#title' => $instance['label'], '#label_display' => 'above', '#field_type' => $field['type'], '#field_name' => $field_name, '#bundle' => $node->type, '#object' => $node, '#items' => array(array()), '#entity_type' => 'node', '#weight' => $display['weight'], '#prefix' => '<div class="mode-read">', '#suffix' => '</div>', 0 => array('#markup' => 'N/A')); } // Embed the editable widget on the page if user can edit - // Normal users: has node access and the field is not an admin-only field // Admin users: has node access, field is an admin-only field, and user is admin $is_admin_field = in_array($field_name, $admin_fields); if ($can_edit && !$is_admin_field || $can_edit && $is_admin_field && $is_admin) { $form = array('#type' => 'form', '#attributes' => array('data-edit' => $field_name, 'class' => array('field', 'mode-edit'))); $form_state = array(); $values = field_get_items('node', $node, $field_name); $field_val = ''; if (!empty($values[0]['value'])) { $field_val = $values[0]['value']; } if (!empty($values[0]['target_id'])) { $field_val = $values[0]['target_id']; } // A few fields are handled a bit differently - create their form elements manually if (in_array($field_name, array('title', 'body', 'field_project'))) { if ($field_name == 'title') { $form['title'][LANGUAGE_NONE][0] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $node->title, '#value' => $node->title, '#required' => TRUE); } if ($field_name == 'body') { $form['body'][LANGUAGE_NONE][0] = array('#type' => 'textarea', '#title' => t('Body'), '#format' => 'full_html', '#value' => $field_val, '#rows' => 25); } // Use a select rather than the standard autocomplete if ($field_name == 'field_project') { drupal_add_library('system', 'drupal.ajax'); $projects = node_load_multiple(array(), array('type' => 'project')); $project_options = array(); $field_val_title = ''; foreach ($projects as $project) { // Only include project if user has access to it if (node_access('view', $project)) { $project_options[$project->nid] = $project->title; } if ($project->nid == $field_value) { $field_val_title = $project->title; } } // Show dropdown if the Project value is in the list of allowed options if (array_key_exists($field_val, $project_options)) { $form['field_project'][LANGUAGE_NONE][0] = array('#type' => 'select', '#title' => t('Project'), '#options' => $project_options, '#default_value' => $field_val, '#required' => TRUE, '#chosen' => FALSE, '#attributes' => array('class' => array('select-project'))); } else { $form['field_project'][LANGUAGE_NONE][0] = array('#type' => 'hidden', '#value' => $field_val); $form['project_readonly'] = array('#markup' => $field_val_title); } } } else { cubert_attach_form($field_name, 'node', $node->type, $node, $form, $form_state, LANGUAGE_NONE); $form[$field_name][LANGUAGE_NONE]['#value'] = $field_val; $form[$field_name][LANGUAGE_NONE]['#default_value'] = $field_val; if (!empty($form[$field_name][LANGUAGE_NONE][0]['value'])) { $form[$field_name][LANGUAGE_NONE][0]['value']['#value'] = $field_val; } if ($field_name == 'field_issue_component') { $form[$field_name][LANGUAGE_NONE]['#attributes']['class'] = array('select-dept'); } } if ($field_name == 'field_due_date') { $form[$field_name][LANGUAGE_NONE][0] = array('#type' => 'textfield', '#title' => t('Due Date'), '#value' => $field_val); } // Hide the default label and add a Bootstrap-wrapped label instead $form[$field_name]['#label_display'] = 'hidden'; $form[$field_name]['#prefix'] = '<div class="field-label"><label>' . $instance['label'] . '</label></div><div class="field-items">'; $form[$field_name]['#suffix'] = '</div>'; $form[$field_name][LANGUAGE_NONE]['#attributes']['class'][] = 'form-control'; $form[$field_name][LANGUAGE_NONE][0]['#attributes']['class'][] = 'form-control'; // Add classes to Time Estimate field if ($field_name == 'time_estimate') { $form[$field_name][LANGUAGE_NONE][0]['value']['#attributes']['class'][] = 'form-control'; $form[$field_name][LANGUAGE_NONE][0]['value']['#attributes']['class'][] = 'textbox-med'; } $output[$field_name]['edit_form'] = $form; } } } } }