Projet

Général

Profil

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

root / drupal7 / sites / all / modules / panelizer / plugins / entity / PanelizerEntityNode.class.php @ a2bb1a14

1
<?php
2
/**
3
 * @file
4
 * Class for the Panelizer node entity plugin.
5
 */
6

    
7
/**
8
 * Panelizer Entity node plugin class.
9
 *
10
 * Handles node specific functionality for Panelizer.
11
 */
12
class PanelizerEntityNode extends PanelizerEntityDefault {
13
  public $entity_admin_root = 'admin/structure/types/manage/%panelizer_node_type';
14
  public $entity_admin_bundle = 4;
15
  public $views_table = 'node';
16
  public $uses_page_manager = TRUE;
17
  public $supports_revisions = TRUE;
18

    
19
  public function entity_access($op, $entity) {
20
    // This must be implemented by the extending clas.
21
    return node_access($op, $entity);
22
  }
23

    
24
  /**
25
   * Implement the save function for the entity.
26
   */
27
  public function entity_save($entity) {
28
    node_save($entity);
29
  }
30

    
31
  public function entity_identifier($entity) {
32
    return t('This node');
33
  }
34

    
35
  public function entity_bundle_label() {
36
    return t('Node type');
37
  }
38

    
39
  /**
40
   * Determine if the entity allows revisions.
41
   */
42
  public function entity_allows_revisions($entity) {
43
    $retval = array();
44

    
45
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
46

    
47
    $node_options = variable_get('node_options_' . $bundle, array('status', 'promote'));
48

    
49
    // Whether or not the entity supports revisions.
50
    $retval[0] = TRUE;
51

    
52
    // Whether or not the user can control if a revision is created.
53
    $retval[1] = user_access('administer nodes');
54

    
55
    // Whether or not the revision is created by default.
56
    $retval[2] = in_array('revision', $node_options);
57

    
58
    return $retval;
59
  }
60

    
61
  /**
62
   * {@inheritdoc}
63
   */
64
  function get_default_display($bundle, $view_mode) {
65
    $display = parent::get_default_display($bundle, $view_mode);
66
    // Add the node title to the display since we can't get that automatically.
67
    $display->title = '%node:title';
68

    
69
    // Add the node links, they probably would like these.
70
    $pane = panels_new_pane('node_links', 'node_links', TRUE);
71
    $pane->css['css_class'] = 'link-wrapper';
72
    $pane->configuration['build_mode'] = $view_mode;
73
    $pane->configuration['context'] = 'panelizer';
74

    
75
    // @todo -- submitted by does not exist as a pane! That's v. sad.
76
    $display->add_pane($pane, 'center');
77

    
78
    unset($pane);
79

    
80
    // If the content type is enabled for use with Webform, add the custom
81
    // submission pane.
82
    if (module_exists('webform')) {
83
      if ($view_mode == 'page_manager') {
84
        if (variable_get('webform_node_' . $bundle)) {
85
          $pane = panels_new_pane('entity_field_extra', 'node:webform', TRUE);
86
          $pane->configuration['context'] = 'panelizer';
87
          $pane->configuration['view_mode'] = 'full';
88
          $display->add_pane($pane, 'center');
89
          unset($pane);
90
        }
91
      }
92
    }
93

    
94
    // Add a custom pane for the book navigation block for the Page Manager
95
    // display.
96
    if (module_exists('book')) {
97
      if ($view_mode == 'page_manager') {
98
        $pane = panels_new_pane('node_book_nav', 'node_book_nav', TRUE);
99
        $pane->configuration['context'] = 'panelizer';
100
        $display->add_pane($pane, 'center');
101
        unset($pane);
102
      }
103
    }
104

    
105
    return $display;
106
  }
107

    
108
  /**
109
   * Implements a delegated hook_page_manager_handlers().
110
   *
111
   * This makes sure that all panelized entities have the proper entry
112
   * in page manager for rendering.
113
   */
114
  public function hook_default_page_manager_handlers(&$handlers) {
115
    $handler = new stdClass;
116
    $handler->disabled = FALSE; /* Edit this to true to make a default handler disabled initially */
117
    $handler->api_version = 1;
118
    $handler->name = 'node_view_panelizer';
119
    $handler->task = 'node_view';
120
    $handler->subtask = '';
121
    $handler->handler = 'panelizer_node';
122
    $handler->weight = -100;
123
    $handler->conf = array(
124
      'title' => t('Node panelizer'),
125
      'context' => 'argument_entity_id:node_1',
126
      'access' => array(),
127
    );
128
    $handlers['node_view_panelizer'] = $handler;
129

    
130
    return $handlers;
131
  }
132

    
133
  /**
134
   * Implements a delegated hook_form_alter.
135
   *
136
   * We want to add Panelizer settings for the bundle to the node type form.
137
   */
138
  public function hook_form_alter(&$form, &$form_state, $form_id) {
139
    if ($form_id == 'node_type_form') {
140
      if (isset($form['#node_type'])) {
141
        $bundle = $form['#node_type']->type;
142
        $this->add_bundle_setting_form($form, $form_state, $bundle, array('type'));
143
      }
144
    }
145
  }
146

    
147
  public function hook_page_alter(&$page) {
148
    // Add an extra "Panelizer" action on the content types admin page.
149
    if ($_GET['q'] == 'admin/structure/types') {
150
      // This only works with some themes.
151
      if (!empty($page['content']['system_main']['node_table'])) {
152
        // Shortcut.
153
        $table = &$page['content']['system_main']['node_table'];
154

    
155
        // Operations column should always be the last column in header.
156
        // Increase its colspan by one to include possible panelizer link.
157
        $operationsCol = end($table['#header']);
158
        if (!empty($operationsCol['colspan'])) {
159
          $operationsColKey = key($table['#header']);
160
          $table['#header'][$operationsColKey]['colspan']++;
161
        }
162

    
163
        // Since we can't tell what row a type is for, but we know that they
164
        // were generated in this order, go through the original types list.
165
        $types = node_type_get_types();
166
        $names = node_type_get_names();
167
        $row_index = 0;
168
        foreach ($names as $bundle => $name) {
169
          $type = $types[$bundle];
170
          if (node_hook($type->type, 'form')) {
171
            $type_url_str = str_replace('_', '-', $type->type);
172
            if ($this->is_panelized($bundle) && panelizer_administer_entity_bundle($this, $bundle)) {
173
              $table['#rows'][$row_index][] = array('data' => l(t('panelizer'), 'admin/structure/types/manage/' . $type_url_str . '/panelizer'));
174
            }
175
            else {
176
              $table['#rows'][$row_index][] = array('data' => '');
177
            }
178
            // Update row index for next pass.
179
            $row_index++;
180
          }
181
        }
182
      }
183
    }
184
  }
185

    
186
  public function preprocess_panelizer_view_mode(&$vars, $entity, $element, $panelizer, $info) {
187
    parent::preprocess_panelizer_view_mode($vars, $entity, $element, $panelizer, $info);
188

    
189
    if (!empty($entity->promote)) {
190
      $vars['classes_array'][] = 'node-promoted';
191
    }
192
    if (!empty($entity->sticky)) {
193
      $vars['classes_array'][] = 'node-sticky';
194
    }
195
    if (empty($entity->status)) {
196
      $vars['classes_array'][] = 'node-unpublished';
197
    }
198
  }
199

    
200
  function render_entity($entity, $view_mode, $langcode = NULL, $args = array(), $address = NULL, $extra_contexts = array()) {
201
    $info = parent::render_entity($entity, $view_mode, $langcode, $args, $address, $extra_contexts);
202
    if (!empty($entity->promote)) {
203
      $info['classes_array'][] = 'node-promoted';
204
    }
205
    if (!empty($entity->sticky)) {
206
      $info['classes_array'][] = 'node-sticky';
207
    }
208
    if (empty($entity->status)) {
209
      $info['classes_array'][] = 'node-unpublished';
210
    }
211
    return $info;
212
  }
213

    
214
  /**
215
   * Implements hook_views_plugins_alter().
216
   */
217
  function hook_views_plugins_alter(&$plugins) {
218
    // While it would be nice to generalize this plugin, there is no generic
219
    // entity view. This means that to generalize it we'll still need to have
220
    // each entity know how to do the view individually.
221
    // @todo make this happen.
222
    $path = drupal_get_path('module', 'panelizer') . '/plugins/views';
223
    $plugins['row']['panelizer_node_view'] = array(
224
      'title' => t('Panelizer display'),
225
      'help' => t('Render entities using the panels display for any that have been panelized.'),
226
      'handler' => 'panelizer_plugin_row_panelizer_node_view',
227
      'parent' => 'node',
228
      'base' => array('node'),
229
      'path' => $path,
230
      'uses options' => TRUE,
231
      'module' => 'panelizer',
232
      'type' => 'normal',
233
      'register theme' => FALSE,
234
      'name' => 'panelizer_node_view',
235
    );
236
  }
237
}