'Field Value Loader', 'description' => 'Configure which fields should be hidden when viewing entity for manual loading them later (using AJAX). Separately for each bundle of an entity.', 'page callback' => 'drupal_get_form', 'page arguments' => array('fvl_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'fvl.admin.inc', ); $items['fvl/%/%/%/%/%/%'] = array( 'page callback' => 'fvl_get_field_value', 'page arguments' => array(1, 2, 3, 4, 5, 6), 'access arguments' => array('access content'), ); return $items; } /** * Implements hook_entity_view(). */ function fvl_entity_view($entity, $type, $view_mode, $langcode) { $settings = variable_get('fvl_fields', array()); $ids = entity_extract_ids($type, $entity); if (isset($settings[$type][$ids[2]])) { foreach (array_keys($settings[$type][$ids[2]]) as $field) { if (isset($entity->content[$field])) { if (isset($entity->content[$field][1])) { foreach (array_keys($entity->content[$field]) as $delta) { if (is_numeric($delta)) { unset($entity->content[$field][$delta]); } } } // If revision is null set it as zero. $ids[1] = is_null($ids[1]) ? 0 : $ids[1]; // Adding wrapper for ajax. $wrapper_id = "fvl-$type-$ids[0]-$field-$view_mode"; $entity->content[$field]['#prefix'] = '
'; $entity->content[$field]['#suffix'] = '
'; // Replacement of the field data to a link(like "Show"). $replacement_text = empty($settings[$type][$ids[2]][$field]) ? variable_get('fvl_show_link_text', 'Show') : $settings[$type][$ids[2]][$field]; $entity->content[$field][0] = array( '#type' => 'link', '#title' => t($replacement_text), '#href' => "fvl/$type/" . implode('/', $ids) . "/$field/$view_mode/nojs", '#ajax' => array( 'wrapper' => $wrapper_id, ), '#attributes' => array( 'rel' => 'nofollow', 'class' => array('fvl-ajax-link'), ), ); } } } } /** * Handler for ajax-request on field value. */ function fvl_get_field_value($entity_type, $entity_id, $rev_id, $bundle, $field, $view_mode, $mode = '') { if ($mode != 'ajax') { return fvl_nojs(); } $settings = variable_get('fvl_fields', array()); $conditions = $rev_id && $entity_type == 'node' ? array('vid' => $rev_id) : array(); if (isset($settings[$entity_type][$bundle][$field]) && $entity = entity_load($entity_type, array($entity_id), $conditions)) { $entity = $entity[$entity_id]; // Checking access to the entity and the field (protection for bypass attacks). $field_info = field_info_field($field); if (!field_access('view', $field_info, $entity_type, $entity) || !fvl_entity_view_access($entity_type, $entity)) { return MENU_ACCESS_DENIED; } // Getting display settings for field. $instance = field_info_instance($entity_type, $field, $bundle); // Assign default display. $view_mode = isset($instance['display'][$view_mode]) ? $view_mode : 'default'; $display = $instance['display'][$view_mode]; $output = field_view_field($entity_type, $entity, $field, $display); ajax_deliver($output); exit; } return MENU_NOT_FOUND; } /** * Getting access flag for an entity. */ function fvl_entity_view_access($entity_type, $entity) { if (module_exists('entity')) { global $user; return entity_access('view', $entity_type, $entity, $user); } switch ($entity_type) { case 'comment': return user_access('access comments'); case 'node': return node_access('view', $entity); case 'user': return user_view_access($entity); default: return user_access('access content'); } } /** * Helper function that generates the response when * JavaScript is disabled. */ function fvl_nojs() { $path = ''; $base_url = str_replace('/', '\/', $GLOBALS['base_url']); if (isset($_SERVER['HTTP_REFERER']) && preg_match("/$base_url/", $_SERVER['HTTP_REFERER'])) { $path = $_SERVER['HTTP_REFERER']; } drupal_set_message(t('Please enable JavaScript in your browser to view this data.', array('@how-to-yahoo-link' => 'http://help.yahoo.com/l/us/yahoo/help/faq/browsers/browsers-63474.html')), 'warning'); drupal_goto($path); }