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' => '')); } } } }
/** * Implements EditMetadataGeneratorInterface::generate(). */ public function generate($entity_type, $entity, array $instance, $langcode, $view_mode) { $field_name = $instance['field_name']; // Early-return if user does not have access. $access = $this->accessChecker->accessEditEntityField($entity_type, $entity, $field_name); if (!$access) { return array('access' => FALSE); } // Early-return if no editor is available. if (!_edit_is_extra_field($entity_type, $field_name)) { $display = field_get_display($instance, $view_mode, $entity); $formatter_type = field_info_formatter_types($display['type']); $items = field_get_items($entity_type, $entity, $field_name, $langcode); $items = $items === FALSE ? array() : $items; $editor_id = $this->editorSelector->getEditor($formatter_type, $instance, $items); } else { // @see hook_edit_extra_fields_info() $extra = edit_extra_field_info($entity_type, $field_name); if (isset($extra['view mode dependent editor'][$view_mode])) { $editor_id = $extra['view mode dependent editor'][$view_mode]; } else { $editor_id = $extra['default editor']; } } if (!isset($editor_id)) { return array('access' => FALSE); } // Gather metadata, allow the editor to add additional metadata of its own. if (!_edit_is_extra_field($entity_type, $field_name)) { $label = $instance['label']; } else { $label = edit_extra_field_info($entity_type, $field_name, 'label'); } list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); $metadata = array('label' => check_plain($label), 'access' => TRUE, 'editor' => $editor_id, 'aria' => t('Entity @type @id, field @field', array('@type' => $entity_type, '@id' => $id, '@field' => $label))); if (!_edit_is_extra_field($entity_type, $field_name)) { $editor = edit_editor_get($editor_id); if (!empty($editor['metadata callback'])) { if ($editor['file']) { require_once $editor['file path'] . '/' . $editor['file']; } if (function_exists($editor['metadata callback'])) { $custom_metadata = $editor['metadata callback']($instance, $items); if (count($custom_metadata)) { $metadata['custom'] = $custom_metadata; } } } // Allow the metadata to be altered. $context = array('entity_type' => $entity_type, 'entity' => $entity, 'field' => $instance, 'items' => $items); drupal_alter('edit_editor_metadata', $metadata, $editor_id, $context); } return $metadata; }
/** * Constructor for the Add to cart form. Provide $nid as the id of the * product display node. Other optional arguments are product reference field * name, view mode of the product display node and language of the product * display node. If optional arguments are not provided, their default values * are assumed, which are "field_product", "default" and "en" respectively. * * @param int $nid * Product display node id. */ public function __construct($nid) { $args = func_get_args(); array_shift($args); $field_name = array_shift($args); $view_mode = array_shift($args); $language = array_shift($args); if (is_null($field_name)) { $field_name = 'field_product'; } if (is_null($view_mode)) { $view_mode = 'default'; } if (is_null($language)) { $language = 'en'; } $node = node_load($nid); $instance = field_info_instance('node', $field_name, $node->type); $display = field_get_display($instance, $view_mode, $node); $settings = array_merge(field_info_formatter_settings($display['type']), $display['settings']); $field_product = field_get_items('node', $node, $field_name); $product_id = $field_product[0]['product_id']; $products = commerce_product_load_multiple(array($product_id)); $type = !empty($settings['line_item_type']) ? $settings['line_item_type'] : 'product'; $line_item = commerce_product_line_item_new(commerce_product_reference_default_product($products), $settings['default_quantity'], 0, array(), $type); $line_item->data['context']['product_ids'] = array_keys($products); $line_item->data['context']['add_to_cart_combine'] = !empty($settings['combine']); $line_item->data['context']['show_single_product_attributes'] = !empty($settings['show_single_product_attributes']); $cart_context = array('entity_type' => 'node', 'entity_id' => $nid, 'display' => 'default', 'language' => $language); $cart_context['class_prefix'] = $cart_context['entity_type'] . '-' . $nid; $cart_context['view_mode'] = $cart_context['entity_type'] . '_' . $view_mode; $entity_uri = entity_uri($cart_context['entity_type'], $node); $arguments = array('form_id' => commerce_cart_add_to_cart_form_id(array($product_id)), 'line_item' => $line_item, 'show_quantity' => $settings['show_quantity']); // Add the display path and referencing entity data to the line item. if (!empty($entity_uri['path'])) { $arguments['line_item']->data['context']['display_path'] = $entity_uri['path']; } $arguments['line_item']->data['context']['entity'] = array('entity_type' => $cart_context['entity_type'], 'entity_id' => $cart_context['entity_id'], 'product_reference_field_name' => $field_name); // Update the product_ids variable to point to the entity data if we're // referencing multiple products. if (count($arguments['line_item']->data['context']['product_ids']) > 1) { $arguments['line_item']->data['context']['product_ids'] = 'entity'; } parent::__construct($arguments['form_id'], $arguments['line_item'], $arguments['show_quantity'], $cart_context); $this->cart_context = $cart_context; $this->arguments = $arguments; }
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; } } } } }
<?php $field_instances = field_info_instance('node', $settings['fieldName'], $settings['nodeType']); $display = field_get_display($field_instances, 'full', $node); $field_render = field_view_field('node', $node, $settings['fieldName'], $display); print render($field_render);