Project

General

Profile

Paste
Download (6.01 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / file_entity / plugins / tasks / file_view.inc @ 1f142f4f

1
<?php
2

    
3
/**
4
 * @file
5
 * Handle the 'file view' override task.
6
 *
7
 * This plugin overrides file/%file and reroutes it to the page manager, where
8
 * a list of tasks can be used to service this request based upon criteria
9
 * supplied by access plugins.
10
 */
11

    
12
/**
13
 * Specialized implementation of hook_page_manager_task_tasks().
14
 */
15
function file_entity_file_view_page_manager_tasks() {
16
  return array(
17
    // This is a 'page' task and will fall under the page admin UI
18
    'task type' => 'page',
19
    'title' => t('File template'),
20
    'admin title' => t('File template'),
21
    'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying files at <em>file/%file</em>. If you add variants, you may use selection criteria such as file types or user access to provide different views of files. If no variant is selected, the default Drupal file view will be used. This page only affects files viewed as pages, it will not affect files viewed in lists or at other locations. Also please note that if you are using Pathauto, aliases may make a file to be available somewhere else, but as far as Drupal is concerned, they are still at file/%file.'),
22
    'admin path' => 'file/%file',
23

    
24
    // Menu hooks so that we can alter the file/%file menu entry to point to us.
25
    'hook menu' => 'file_entity_file_view_menu',
26
    'hook menu alter' => 'file_entity_file_view_menu_alter',
27

    
28
    // This is task uses 'context' handlers and must implement these to give the
29
    // handler data it needs.
30
    'handler type' => 'context',
31
    'get arguments' => 'file_entity_file_view_get_arguments',
32
    'get context placeholders' => 'file_entity_file_view_get_contexts',
33

    
34
    // Allow this to be enabled or disabled:
35
    'disabled' => variable_get('file_entity_file_view_disabled', TRUE),
36
    'enable callback' => 'file_entity_file_view_enable',
37
    'access callback' => 'file_entity_file_view_access_check',
38
  );
39
}
40

    
41
/**
42
 * Callback defined by page_manager_file_view_page_manager_tasks().
43
 *
44
 * Alter the file view input so that file view comes to us rather than the
45
 * normal file view process.
46
 */
47
function file_entity_file_view_menu_alter(&$items, $task) {
48
  if (variable_get('file_entity_file_view_disabled', TRUE)) {
49
    return;
50
  }
51

    
52
  // Override the node view handler for our purpose.
53
  $callback = $items['file/%file']['page callback'];
54
  if ($callback == 'file_entity_view_page' || variable_get('page_manager_override_anyway', FALSE)) {
55
    $items['file/%file']['page callback'] = 'file_entity_file_view_page';
56
    $items['file/%file']['file path'] = $task['path'];
57
    $items['file/%file']['file'] = $task['file'];
58
  }
59
  else {
60
    // automatically disable this task if it cannot be enabled.
61
    variable_set('file_entity_file_view_disabled', TRUE);
62
    if (!empty($GLOBALS['page_manager_enabling_file_view'])) {
63
      drupal_set_message(t('Page manager module is unable to enable file/%file because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
64
    }
65
  }
66
}
67

    
68
/**
69
 * Entry point for our overridden file view.
70
 *
71
 * This function asks its assigned handlers who, if anyone, would like
72
 * to run with it. If no one does, it passes through to File entity's
73
 * file view, which is file_entity_view_page().
74
 */
75
function file_entity_file_view_page($file) {
76
  // Load my task plugin:
77
  $task = page_manager_get_task('file_view');
78

    
79
  // Load the account into a context.
80
  ctools_include('context');
81
  ctools_include('context-task-handler');
82
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($file));
83

    
84
  // We need to mimic Drupal's behavior of setting the file title here.
85
  drupal_set_title($file->filename);
86
  $uri = entity_uri('file', $file);
87
  // Set the file path as the canonical URL to prevent duplicate content.
88
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
89
  // Set the non-aliased path as a default shortlink.
90
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
91
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($file));
92

    
93
  $output = ctools_context_handler_render($task, '', $contexts, array($file->fid));
94
  if ($output != FALSE) {
95
    return $output;
96
  }
97

    
98
  $function = 'file_entity_view_page';
99
  foreach (module_implements('page_manager_override') as $module) {
100
    $call = $module . '_page_manager_override';
101
    if (($rc = $call('file_view')) && function_exists($rc)) {
102
      $function = $rc;
103
      break;
104
    }
105
  }
106

    
107
  // Otherwise, fall back.
108
  return $function($file);
109
}
110

    
111
/**
112
 * Callback to get arguments provided by this task handler.
113
 *
114
 * Since this is the file view and there is no UI on the arguments, we
115
 * create dummy arguments that contain the needed data.
116
 */
117
function file_entity_file_view_get_arguments($task, $subtask_id) {
118
  return array(
119
    array(
120
      'keyword' => 'file',
121
      'identifier' => t('File being viewed'),
122
      'id' => 1,
123
      'name' => 'entity_id:file',
124
      'settings' => array(),
125
    ),
126
  );
127
}
128

    
129
/**
130
 * Callback to get context placeholders provided by this handler.
131
 */
132
function file_entity_file_view_get_contexts($task, $subtask_id) {
133
  return ctools_context_get_placeholders_from_argument(page_manager_file_view_get_arguments($task, $subtask_id));
134
}
135

    
136
/**
137
 * Callback to enable/disable the page from the UI.
138
 */
139
function file_entity_file_view_enable($cache, $status) {
140
  variable_set('file_entity_file_view_disabled', $status);
141

    
142
  // Set a global flag so that the menu routine knows it needs
143
  // to set a message if enabling cannot be done.
144
  if (!$status) {
145
    $GLOBALS['page_manager_enabling_file_view'] = TRUE;
146
  }
147
}
148

    
149
/**
150
 * Callback to determine if a page is accessible.
151
 *
152
 * @param $task
153
 *   The task plugin.
154
 * @param $subtask_id
155
 *   The subtask id
156
 * @param $contexts
157
 *   The contexts loaded for the task.
158
 * @return
159
 *   TRUE if the current user can access the page.
160
 */
161
function page_manager_file_view_access_check($task, $subtask_id, $contexts) {
162
  $context = reset($contexts);
163
  return file_entity_access('view');
164
}