Project

General

Profile

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

root / drupal7 / sites / all / modules / ctools / plugins / contexts / node_edit_form.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a node_edit_form context.
6
 */
7

    
8
/**
9
 * Plugins are described by creating a $plugin array which will be used
10
 * by the system that includes this file.
11
 */
12
$plugin = array(
13
  'title' => t("Node edit form"),
14
  'description' => t('A node edit form.'),
15
  'context' => 'ctools_context_create_node_edit_form',
16
  'edit form' => 'ctools_context_node_edit_form_settings_form',
17
  'defaults' => array('nid' => ''),
18
  'keyword' => 'node_edit',
19
  'context name' => 'node_edit_form',
20
  'convert list' => 'ctools_context_node_edit_convert_list',
21
  'convert' => 'ctools_context_node_edit_convert',
22
  'placeholder form' => array(
23
    '#type' => 'textfield',
24
    '#description' => t('Enter the node ID of a node for this argument:'),
25
  ),
26
);
27

    
28
/**
29
 * It's important to remember that $conf is optional here, because contexts
30
 * are not always created from the UI.
31
 */
32
function ctools_context_create_node_edit_form($empty, $node = NULL, $conf = FALSE) {
33
  static $creating = FALSE;
34
  $context = new ctools_context(array('form', 'node_edit', 'node_form', 'node_edit_form', 'node', 'entity:node'));
35
  $context->plugin = 'node_edit_form';
36

    
37
  if ($empty || ($creating)) {
38
    return $context;
39
  }
40
  $creating = TRUE;
41

    
42
  if ($conf) {
43
    // In this case, $node is actually our $conf array.
44
    $nid = is_array($node) && isset($node['nid']) ? $node['nid'] : (is_object($node) ? $node->nid : 0);
45

    
46
    if (module_exists('translation')) {
47
      if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
48
        $nid = $translation;
49
        $reload = TRUE;
50
      }
51
    }
52

    
53
    if (is_array($node) || !empty($reload)) {
54
      $node = node_load($nid);
55
    }
56
  }
57

    
58
  if (!empty($node)) {
59
    $form_id = $node->type . '_node_form';
60

    
61
    $form_state = array('want form' => TRUE, 'build_info' => array('args' => array($node)));
62

    
63
    $file = drupal_get_path('module', 'node') . '/node.pages.inc';
64
    require_once DRUPAL_ROOT . '/' . $file;
65
    // This piece of information can let other modules know that more files
66
    // need to be included if this form is loaded from cache:
67
    $form_state['build_info']['files'] = array($file);
68

    
69
    $form = drupal_build_form($form_id, $form_state);
70

    
71
    // Fill in the 'node' portion of the context.
72
    $context->data     = $node;
73
    $context->title    = isset($node->title) ? $node->title : '';
74
    $context->argument = isset($node->nid) ? $node->nid : $node->type;
75

    
76
    $context->form       = $form;
77
    $context->form_state = &$form_state;
78
    $context->form_id    = $form_id;
79
    $context->form_title = isset($node->title) ? $node->title : '';
80
    $context->node_type  = $node->type;
81
    $context->restrictions['type'] = array($node->type);
82
    $context->restrictions['form'] = array('form');
83

    
84
    $creating = FALSE;
85
    return $context;
86
  }
87
  $creating = FALSE;
88
}
89

    
90
function ctools_context_node_edit_form_settings_form($form, &$form_state) {
91
  $conf = &$form_state['conf'];
92

    
93
  $form['node'] = array(
94
    '#title' => t('Enter the title or NID of a node'),
95
    '#type' => 'textfield',
96
    '#maxlength' => 512,
97
    '#autocomplete_path' => 'ctools/autocomplete/node',
98
    '#weight' => -10,
99
  );
100

    
101
  if (!empty($conf['nid'])) {
102
    $info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
103
    if ($info) {
104
      $link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
105
      $form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
106
    }
107
  }
108

    
109
  $form['nid'] = array(
110
    '#type' => 'value',
111
    '#value' => $conf['nid'],
112
  );
113

    
114
  $form['set_identifier'] = array(
115
    '#type' => 'checkbox',
116
    '#default_value' => FALSE,
117
    '#title' => t('Reset identifier to node title'),
118
    '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
119
  );
120

    
121
  return $form;
122
}
123

    
124
/**
125
 * Validate a node.
126
 */
127
function ctools_context_node_edit_form_settings_form_validate($form, &$form_state) {
128
  // Validate the autocomplete.
129
  if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
130
    form_error($form['node'], t('You must select a node.'));
131
    return;
132
  }
133

    
134
  if (empty($form_state['values']['node'])) {
135
    return;
136
  }
137

    
138
  $nid          = $form_state['values']['node'];
139
  $preg_matches = array();
140
  $match        = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
141
  if (!$match) {
142
    $match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
143
  }
144

    
145
  if ($match) {
146
    $nid = $preg_matches[1];
147
  }
148
  if (is_numeric($nid)) {
149
    $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
150
  }
151
  else {
152
    $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
153
  }
154

    
155
  // Do not allow unpublished nodes to be selected by unprivileged users.
156
  if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
157
    form_error($form['node'], t('Invalid node selected.'));
158
  }
159
  else {
160
    form_set_value($form['nid'], $node->nid, $form_state);
161
  }
162
}
163

    
164
function ctools_context_node_edit_form_settings_form_submit($form, &$form_state) {
165
  if ($form_state['values']['set_identifier']) {
166
    $node = node_load($form_state['values']['nid']);
167
    $form_state['values']['identifier'] = $node->title;
168
  }
169

    
170
  // This will either be the value set previously or a value set by the
171
  // validator.
172
  $form_state['conf']['nid'] = $form_state['values']['nid'];
173
}
174

    
175
/**
176
 * Provide a list of ways that this context can be converted to a string.
177
 */
178
function ctools_context_node_edit_convert_list() {
179
  // Pass through to the "node" context convert list.
180
  $plugin = ctools_get_context('node');
181
  return ctools_context_node_convert_list();
182
}
183

    
184
/**
185
 * Convert a context into a string.
186
 */
187
function ctools_context_node_edit_convert($context, $type) {
188
  // Pass through to the "node" context convert list.
189
  $plugin = ctools_get_context('node');
190
  return ctools_context_node_convert($context, $type);
191
}