summaryrefslogtreecommitdiffstats
path: root/fvl.module
blob: 48c68635153fc2049989750c738a5f6f63a55dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * @file
 * FVL module file.
 */

/**
 * Implements hook_menu().
 */
function fvl_menu() {
  $items = array();
  $items['admin/config/content/fvl'] = array(
    'title' => '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'] = '<div id="' . $wrapper_id . '" class="fvl-ajax-container">';
        $entity->content[$field]['#suffix'] = '</div>';
        // 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 = '<front>';
  $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 <a href="@how-to-yahoo-link" target="_blank">enable JavaScript</a> 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);
}