Projet

Général

Profil

Paste
Télécharger (7,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ds / modules / ds_extras / includes / ds_extras.vd.inc @ 6e9292aa

1
<?php
2

    
3
/**
4
 * @file
5
 * Views displays functions.
6
 */
7

    
8
/**
9
 * Show the overview form and the selection list.
10
 */
11
function ds_extras_vd_overview() {
12

    
13
  $entity_info = entity_get_info('ds_views');
14
  $build = $rows = array();
15

    
16
  ctools_include('export');
17
  $vd_settings = ctools_export_crud_load_all('ds_vd');
18

    
19
  if ($entity_info) {
20
    foreach ($entity_info['bundles'] as $key => $value) {
21

    
22
      // If no initial bundles were created, entity API will create
23
      // one by default, so make sure we do not list that one.
24
      if ($key == 'ds_views') {
25
        continue;
26
      }
27

    
28
      $row = array();
29
      $row[] = $value['label'];
30
      $operations = l(t('Manage layout'), 'admin/structure/ds/vd/manage/' . $key . '/display');
31

    
32
      if (isset($vd_settings[$key]) && $vd_settings[$key]->export_type == 1) {
33
        $operations .= ' - ' . l(t('Remove'), 'admin/structure/ds/vd/manage/' . $key . '/remove');
34
      }
35
      $row[] = $operations;
36

    
37
      $rows[$key] = $row;
38
    }
39
  }
40

    
41
  if (empty($rows)) {
42
    $rows = array(
43
      array(array('data' => t('No views selected.'), 'colspan' => '2')),
44
    );
45
  }
46

    
47
  $variables = array(
48
    'header' => array(t('Title'), t('Operations')),
49
    'rows' => $rows,
50
  );
51
  $build['list'] = array('#markup' => theme('table', $variables));
52
  $build['form'] = drupal_get_form('ds_extras_vd_bundle_form', $rows);
53

    
54
  return $build;
55
}
56

    
57
/**
58
 * Return the views select form to create a bundle.
59
 */
60
function ds_extras_vd_bundle_form($form, $form_state, $rows) {
61

    
62
  $options = array();
63
  $views = views_get_all_views();
64
  foreach ($views as $view_key => $view) {
65

    
66
    // Ignore disabled views.
67
    if (isset($view->disabled) && $view->disabled) {
68
      continue;
69
    }
70

    
71
    $get_view = views_get_view($view->name);
72

    
73
    // Loop through all displays.
74
    foreach ($view->display as $display_key => $display) {
75
      // Ignore default displays.
76
      if ($display_key == 'default') {
77
        continue;
78
      }
79

    
80
      $key = $view_key . '-' . $display_key;
81
      $name = drupal_ucfirst($view->name) . ': ' . $display->display_title;
82
      if (!isset($rows[$key])) {
83
        $options[$key] = $name . ' (Views template)';
84
      }
85
      $get_view->set_display($display_key);
86
      if ($get_view->display_handler->uses_fields() && !isset($rows[$key . '-fields'])) {
87
        $options[$key . '-fields'] = $name . ' (Fields)';
88
      }
89
    }
90
  }
91

    
92
  $form['vd'] = array(
93
    '#title' => t('Select view'),
94
    '#description' => t('Select a View that you want to manage with Display Suite. If a View uses fields you can also select that view to select a layout and position the fields. Note that html for the label and field are limited.'),
95
    '#type' => 'select',
96
    '#options' => $options,
97
  );
98

    
99
  $form['submit'] = array(
100
    '#type' => 'submit',
101
    '#value' => t('Add'),
102
  );
103

    
104
  return $form;
105
}
106

    
107
/**
108
 * Submit callback: save the new bundle.
109
 */
110
function ds_extras_vd_bundle_form_submit($form, &$form_state) {
111

    
112
  // Save new bundle.
113
  $record = new stdClass();
114
  $record->vd = $form_state['values']['vd'];
115
  $record->label = $form['vd']['#options'][$record->vd];
116
  drupal_write_record('ds_vd', $record);
117

    
118
  // Clear entity cache and field info fields cache.
119
  cache_clear_all('field_info_fields', 'cache_field');
120
  cache_clear_all('entity_info', 'cache', TRUE);
121

    
122
  // Message and redirect.
123
  drupal_set_message(t('Bundle @label has been added.', array('@label' => $record->label)));
124
  $form_state['redirect'] = 'admin/structure/ds/vd';
125
}
126

    
127
/**
128
 * Edit the display or remove a views display.
129
 *
130
 * @param $bundle
131
 *   The name of the bundle
132
 * @param $action
133
 *   The action to take (edit or remove)
134
 */
135
function ds_extras_vd_manage($bundle = '', $action = '') {
136
  $entity_info = entity_get_info('ds_views');
137

    
138
  if (!empty($bundle) && isset($entity_info['bundles'][$bundle]) && $action == 'remove') {
139
    return drupal_get_form('ds_extras_vd_bundle_remove', $bundle, $entity_info['bundles'][$bundle]['label']);
140
  }
141

    
142
  if (!empty($bundle) && isset($entity_info['bundles'][$bundle]) && $action == 'display') {
143
    return ds_extras_vd_field_ui($bundle);
144
  }
145

    
146
  // Redirect to overview.
147
  drupal_set_message(t('No view found to layout.'));
148
  drupal_goto('admin/structure/ds/vd');
149
}
150

    
151
/**
152
 * Return Field UI display screen for a view and bundle.
153
 *
154
 * @param $bundle
155
 *   The name of the bundle
156
 */
157
function ds_extras_vd_field_ui($bundle) {
158

    
159
  global $conf;
160

    
161
  // Use drupal_build_form instead of drupal_get_form.
162
  $form_state = array();
163
  $arguments = array('ds_views', $bundle, 'default');
164
  $form_state['build_info']['args'] = $arguments;
165
  $form_state['no_view_mode_suggestions'] = TRUE;
166
  $form_state['no_panels'] = TRUE;
167
  form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
168
  form_load_include($form_state, 'inc', 'ds_extras', 'includes/ds_extras.vd');
169

    
170
  // Deny access to field_group if it exists.
171
  if (module_exists('field_group')) {
172
    $form_state['no_field_group'] = TRUE;
173
  }
174

    
175
  // Build form.
176
  $build = drupal_build_form('field_ui_display_overview_form', $form_state);
177

    
178
  // Deny access to view modes.
179
  $build['additional_settings']['modes']['#access'] = FALSE;
180

    
181
  // Deny access to disabling blocks and regions.
182
  $build['additional_settings']['ds_layouts']['hide_sidebars']['#access'] = FALSE;
183

    
184
  // Deny access to fields table if there's no layout.
185
  if (!ds_get_layout('ds_views', $bundle, 'default')) {
186
    $build['fields']['#access'] = FALSE;
187
  }
188

    
189
  // Add additional validate function so we can remove notices.
190
  if (module_exists('field_group')) {
191
    array_unshift($build['#validate'], 'ds_vd_field_ui_fix_notices');
192
  }
193

    
194
  return $build;
195
}
196

    
197
/**
198
 * Implements hook_ds_fields_ui_alter().
199
 */
200
function ds_extras_ds_fields_ui_alter(&$fields, $context) {
201

    
202
  // Check on the $bundle string to see if the key 2 exists. If it exists,
203
  // we'll call the view to put the fields of this view on and remove
204
  // the fields which comes default by ds_extras_field_extra_fields()
205
  // and only work for the views template.
206
  $bundle = $context['bundle'];
207
  $fields_check = explode('-', $bundle);
208

    
209
  if (isset($fields_check[2])) {
210

    
211
    $field_copy = array(
212
      'title' => 'dummy',
213
      'field_type' => DS_FIELD_TYPE_IGNORE,
214
    );
215

    
216
    // Remove the view template fields.
217
    $remove_fields = ds_extras_ds_fields_info('ds_views');
218
    foreach ($remove_fields['ds_views'] as $key => $properties) {
219
      unset($fields[$key]);
220
    }
221

    
222
    // Get the view and its fields.
223
    $view = views_get_view($fields_check[0]);
224
    $view->set_display($fields_check[1]);
225
    $fields = $view->display_handler->get_field_labels();
226
    foreach ($fields as $field_key => $field_label) {
227
      $field_properties = $field_copy;
228
      $field_properties['title'] = $field_label;
229
      $fields[$field_key] = $field_properties;
230
    }
231
  }
232
}
233

    
234
/**
235
 * Return confirm form to remove a views bundle
236
 */
237
function ds_extras_vd_bundle_remove($form, $form_state, $bundle, $label) {
238
  $form['#bundle'] = $bundle;
239
  $form['#label'] = $label;
240
  return confirm_form($form, t('Are you sure you want to remove bundle @label ?', array('@label' => $label)), 'admin/structure/ds/vd');
241
}
242

    
243
/**
244
 * Submit callback: remove a views bundle
245
 */
246
function ds_extras_vd_bundle_remove_submit($form, &$form_state) {
247

    
248
  $bundle = $form['#bundle'];
249
  $label = $form['#label'];
250

    
251
  // Remove bundle.
252
  db_delete('ds_vd')
253
    ->condition('vd', $bundle)
254
    ->execute();
255

    
256
  // Remove layout.
257
  db_delete('ds_layout_settings')
258
    ->condition('bundle', $bundle)
259
    ->execute();
260

    
261
  // Remove settings.
262
  db_delete('ds_field_settings')
263
    ->condition('bundle', $bundle)
264
    ->execute();
265

    
266
  // Remove from bundle settings.
267
  variable_del('field_bundle_settings_ds_views__' . $bundle);
268

    
269
  // Clear entity cache and field info fields cache.
270
  cache_clear_all('field_info_fields', 'cache_field');
271
  cache_clear_all('entity_info', 'cache', TRUE);
272

    
273
  drupal_set_message(t('Bundle @label has been removed.', array('@label' => $label)));
274
}