Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a node_add_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 add form'),
14
  'description' => t('A node add form.'),
15
  'context' => 'ctools_context_create_node_add_form',
16
  'edit form' => 'ctools_context_node_add_form_settings_form',
17
  'defaults' => array('type' => ''),
18
  'keyword' => 'node_add',
19
  'context name' => 'node_add_form',
20
  'convert list' => array('type' => t('Node type')),
21
  'convert' => 'ctools_context_node_add_form_convert',
22
  'placeholder form' => array(
23
    '#type' => 'textfield',
24
    '#description' => t('Enter the node type this context.'),
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_add_form($empty, $data = NULL, $conf = FALSE) {
33
  static $creating = FALSE;
34
  $context = new ctools_context(array('form', 'node_add', 'node_form', 'node', 'entity:node'));
35
  $context->plugin = 'node_add_form';
36

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

    
42
  if ($conf && (isset($data['types']) || isset($data['type']))) {
43
    // Holdover from typo'd config.
44
    $data = isset($data['types']) ? $data['types'] : $data['type'];
45
  }
46

    
47
  if (!empty($data)) {
48
    $types = node_type_get_types();
49
    $type = str_replace('-', '_', $data);
50

    
51
    // Validate the node type exists.
52
    if (isset($types[$type]) && node_access('create', $type)) {
53
      // Initialize settings:
54
      global $user;
55
      $node = (object) array(
56
        'uid' => $user->uid,
57
        'name' => (isset($user->name) ? $user->name : ''),
58
        'type' => $type,
59
        'language' => LANGUAGE_NONE,
60
      );
61

    
62
      $form_id = $type . '_node_form';
63

    
64
      $form_state = array(
65
        'want form' => TRUE,
66
        'build_info' => array(
67
          'args' => array($node),
68
        ),
69
      );
70

    
71
      // Use module_load_include so that caches and stuff can know to load this.
72
      form_load_include($form_state, 'inc', 'node', 'node.pages');
73

    
74
      $form = drupal_build_form($form_id, $form_state);
75

    
76
      // In a form, $data is the object being edited.
77
      $context->data     = $node;
78
      $context->title    = $types[$type]->name;
79
      $context->argument = $type;
80

    
81
      // These are specific pieces of data to this form.
82
      // All forms should place the form here.
83
      $context->form                 = $form;
84
      $context->form_id              = $form_id;
85
      $context->form_title           = t('Submit @name', array('@name' => $types[$type]->name));
86
      $context->node_type            = $type;
87
      $context->restrictions['type'] = array($type);
88
      $context->restrictions['form'] = array('form');
89

    
90
      $creating = FALSE;
91
      return $context;
92
    }
93
  }
94
  $creating = FALSE;
95
}
96

    
97
function ctools_context_node_add_form_settings_form($form, &$form_state) {
98
  $conf = $form_state['conf'];
99

    
100
  $form['type'] = array(
101
    '#title' => t('Node type'),
102
    '#type' => 'select',
103
    '#options' => node_type_get_names(),
104
    '#default_value' => $conf['type'],
105
    '#description' => t('Select the node type for this form.'),
106
  );
107

    
108
  return $form;
109
}
110

    
111
function ctools_context_node_add_form_settings_form_submit($form, &$form_state) {
112
  $form_state['conf']['type'] = $form_state['values']['type'];
113
}
114

    
115
/**
116
 * Convert a context into a string.
117
 */
118
function ctools_context_node_add_form_convert($context, $type) {
119
  switch ($type) {
120
    case 'type':
121
      return $context->data->type;
122
  }
123
}