Projet

Général

Profil

Paste
Télécharger (6,91 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panelizer / plugins / export_ui / panelizer_defaults_ui.class.php @ 651307cd

1
<?php
2
/**
3
 * @file
4
 * Contains the administrative UI for selectable panelizer defaults.
5
 */
6

    
7
class panelizer_defaults_ui extends ctools_export_ui {
8
  function init($plugin) {
9
    ctools_include('export');
10

    
11
    $this->plugin = $plugin;
12
    // Get rid of the list parent.
13
    unset($this->plugin['menu']['items']['list callback']);
14
  }
15

    
16
  function hook_menu(&$items) {
17
    // Change the item to a tab.
18
    $this->plugin['menu']['items']['list']['type'] = MENU_LOCAL_TASK;
19
    $this->plugin['menu']['items']['list']['weight'] = -6;
20
    $this->plugin['menu']['items']['list']['title'] = 'List defaults';
21

    
22
    // menu local actions are weird.
23
    $this->plugin['menu']['items']['add']['path'] = 'list/add';
24
    $this->plugin['menu']['items']['import']['path'] = 'list/import';
25

    
26
    // Edit is being handled elsewhere.
27
    unset($this->plugin['menu']['items']['edit callback']);
28
    unset($this->plugin['menu']['items']['access']);
29
    foreach (panelizer_operations() as $path => $operation) {
30
      $location = isset($operation['ui path']) ? $operation['ui path'] : $path;
31
      if (isset($this->plugin['menu']['items'][$location])) {
32
        unset($this->plugin['menu']['items'][$location]);
33
      }
34
    }
35

    
36
    // Change the callbacks for everything.
37
    foreach ($this->plugin['menu']['items'] as $key => $item) {
38
      // The item has already been set; continue to next item to avoid shifting
39
      // items onto the page arguments array more than once.
40
      if ($this->plugin['menu']['items'][$key]['access callback'] == 'panelizer_has_choice_callback') {
41
        continue;
42
      }
43

    
44
      $this->plugin['menu']['items'][$key]['access callback'] = 'panelizer_has_choice_callback';
45
      $this->plugin['menu']['items'][$key]['access arguments'] = array(3, 4, '');
46
      $this->plugin['menu']['items'][$key]['page callback'] = 'panelizer_export_ui_switcher_page';
47
      array_unshift($this->plugin['menu']['items'][$key]['page arguments'], 4);
48
      array_unshift($this->plugin['menu']['items'][$key]['page arguments'], 3);
49
    }
50

    
51
    parent::hook_menu($items);
52
  }
53

    
54
  function list_page($js, $input) {
55
    if ($substitute = $this->entity_handler->get_substitute($this->entity_view_mode, $this->entity_bundle)) {
56
      $url = $this->plugin['menu']['menu prefix'] . '/' . $substitute;
57
      drupal_set_message(t('This display is managed by the !view_mode display.', array('!view_mode' => l($substitute, $url))), 'status', FALSE);
58
      return '';
59
    }
60

    
61
    drupal_set_title($this->entity_handler->get_bundle_title($this->entity_bundle));
62
    return parent::list_page($js, $input);
63
  }
64

    
65
  function list_filter($form_state, $item) {
66
    // Reminder: This returns TRUE to exclude the item.
67
    if ($this->entity_handler->entity_type != $item->panelizer_type) {
68
      return TRUE;
69
    }
70
    if ($this->entity_bundle != $item->panelizer_key) {
71
      return TRUE;
72
    }
73

    
74
    if ($this->entity_view_mode != $item->view_mode) {
75
      return TRUE;
76
    }
77

    
78
    if (!$this->entity_handler->access_default_panelizer_object($item)) {
79
      return TRUE;
80
    }
81

    
82
    if (empty($item->title) && $item->name == implode(':', array($this->entity_handler->entity_type, $this->entity_bundle, 'default'))) {
83
      $item->title = t('Default');
84
    }
85

    
86
    return parent::list_filter($form_state, $item);
87
  }
88

    
89
  /**
90
   * Extends ctools_export_ui::edit_form().
91
   *
92
   * Change the 'exists' callback so that we can build the actual export object
93
   * name before checking if it exists.
94
   */
95
  function edit_form(&$form, &$form_state) {
96
    parent::edit_form($form, $form_state);
97
    foreach ($form['info'] as $export_key => $settings) {
98
      if (!empty($form['info'][$export_key]['#machine_name']['exists'])) {
99
        $form['info'][$export_key]['#machine_name']['exists'] = 'panelizer_defaults_ui_edit_name_exists';
100
      }
101
    }
102
  }
103

    
104
  function edit_execute_form_standard(&$form_state) {
105
    if ($form_state['form type'] == 'clone') {
106
      $form_state['item']->title = t('Clone of') . ' ' . $form_state['item']->title;
107
      $form_state['item']->name = '';
108
    }
109
    else if ($form_state['op'] == 'add') {
110
      $form_state['item']->panelizer_type = $this->entity_handler->entity_type;
111
      $form_state['item']->panelizer_key = $this->entity_bundle;
112
      $form_state['item']->view_mode = $this->entity_view_mode;
113
      $form_state['item']->display = $this->entity_handler->get_default_display($this->entity_bundle, $this->entity_view_mode);
114
    }
115
    return parent::edit_execute_form_standard($form_state);
116
  }
117

    
118
  function edit_form_validate(&$form, &$form_state) {
119

    
120
    // Build the actual name of the object for ctools
121
    $export_key = $this->plugin['export']['key'];
122
    $name = panelizer_defaults_ui_build_export_name($form_state['values'][$export_key], $this);
123

    
124
    form_set_value($form['info'][$export_key], $name, $form_state);
125
  }
126

    
127
  // Simplest way to override the drupal_goto from parent.
128
  // Why isn't delete using the redirect system everything else is?
129
  function delete_page($js, $input, $item) {
130
    $clone = clone $item;
131
    // Change the name into the title so the form shows the right value.
132
    // @todo file a bug against CTools to use admin title if available.
133
    $clone->name = $clone->title;
134
    $form_state = array(
135
      'plugin' => $this->plugin,
136
      'object' => &$this,
137
      'ajax' => $js,
138
      'item' => $clone,
139
      'op' => $item->export_type & EXPORT_IN_CODE ? 'revert' : 'delete',
140
      'rerender' => TRUE,
141
      'no_redirect' => TRUE,
142
    );
143

    
144
    $output = drupal_build_form('ctools_export_ui_delete_confirm_form', $form_state);
145
    if (!empty($form_state['executed'])) {
146
      ctools_export_crud_delete($this->plugin['schema'], $item);
147
      $export_key = $this->plugin['export']['key'];
148
      drupal_set_message(t($this->plugin['strings']['confirmation'][$form_state['op']]['success'], array('%title' => $item->title)));
149
      drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
150
    }
151

    
152
    return $output;
153
  }
154
}
155

    
156
/**
157
 * Test for #machine_name type to see if an export exists.
158
 */
159
function panelizer_defaults_ui_edit_name_exists($name, $element, &$form_state) {
160

    
161
  $export_key = $form_state['plugin']['export']['key'];
162
  $name = panelizer_defaults_ui_build_export_name($form_state['values'][$export_key], $form_state['object']);
163

    
164
  // Pass it on to the original ctools function
165
  return ctools_export_ui_edit_name_exists($name, $element, $form_state);
166
}
167

    
168
/**
169
 * Build the actual name of the ctools export from a machine name.
170
 */
171
function panelizer_defaults_ui_build_export_name($machine_name, $object) {
172

    
173
  // When adding a machine name, the entity/bundle are left off so the user
174
  // does not have to deal with it. We put it back here behind the scenes.
175
  $name = implode(':', array(
176
    $object->entity_handler->entity_type,
177
    $object->entity_bundle,
178
    $machine_name
179
  ));
180

    
181
  // The page_manager view mode is the pre-view mode support method; we have
182
  // to add this view mode as the name if it's a different view mode.
183
  if ($object->entity_view_mode != 'page_manager') {
184
    $name .= ':' . $object->entity_view_mode;
185
  }
186

    
187
  return $name;
188
}