/[drupal]/contributions/modules/views_embed_form/views_embed_form.module
ViewVC logotype

Contents of /contributions/modules/views_embed_form/views_embed_form.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Sat Nov 15 10:12:02 2008 UTC (12 months, 1 week ago) by meba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +97 -3 lines
File MIME type: text/x-php
#333779 Patch to handle multiple instances of the form by wojtha
1 <?php
2 // $Id: views_embed_form.module,v 1.4 2008/11/03 15:36:58 meba Exp $
3 /**
4 This module creates a possibility for developers to embed a form as field in
5 Views. Due to security reasons, it's not possible to embed any form available
6 in Drupal, specific forms need to be enabled for this functionality by module
7 developers. Views embed form provides an API function which provides this ability.
8
9 See README.txt for an example.
10 */
11
12 /**
13 * Implementation of hook_views_api().
14 */
15 function views_embed_form_views_api() {
16 return array(
17 'api' => 2,
18 'path' => drupal_get_path('module', 'views_embed_form'),
19 );
20 }
21
22 /**
23 * Implementation of hook_views_handlers().
24 */
25 function views_embed_form_views_handlers() {
26 return array(
27 'info' => array(
28 'path' => drupal_get_path('module', 'views_embed_form'),
29 ),
30 'handlers' => array(
31 'views_handler_field_views_embed_form' => array(
32 'parent' => 'views_handler_field',
33 ),
34 ),
35 );
36 }
37
38 /**
39 * Implementation of hook_forms()
40 *
41 * http://www.gtrlabs.org/blog/dayre/drupal_5_how_to_process_multiple_instances_of_the_same_form_on_the_same_page
42 *
43 * How does drupal_get_form() know where to get the form data from ? For example,
44 * our view() function may call drupal_get_form() with a form id of
45 * mymodule_thing_form27. We don't have a function called mymodule_thing_form27(),
46 * so the forms API will call this form hook to get the name of a function that
47 * can provide form data. This is what prevents us from duplicating our _form()
48 * and _submit() functions for each unique form instance.
49 *
50 */
51 function views_embed_form_forms() {
52
53 // the form id Drupal plans on using will be the first
54 // element of the first array.
55 $args = func_get_args();
56 $form_id = $args[0];
57
58 // Given a form_id like mymodule_thing_form27 or mymodule_thing_form13,
59 // we look for familiar text to tell we are dealing with the correct
60 // form. Other functions will call this function as part of their form
61 // rendering (e.g. search form rendering) so we need to tell them apart.
62 $forms = array();
63
64 // Here we are telling the forms API that for any form ids starting
65 // with mymodule_thing_formX (X could be any unique id), callback to the
66 // function mymodule_thing_form to get the form data.
67 if ($callback = views_embed_form_is_embed_form($form_id)) {
68 $forms[$form_id] = array('callback' => $callback);
69 }
70
71 return $forms;
72 }
73
74 /**
75 * Implementation of hook_form_alter()
76 *
77 * Automatically repair callbacks to validate and submit form handlers
78 * if they are implemented
79 */
80 function views_embed_form_form_alter(&$form, $form_state, $form_id) {
81
82 if ($callback = views_embed_form_is_embed_form($form_id)) {
83 if (function_exists($callback . '_submit')) {
84 $form['#submit'] = array($callback . '_submit');
85 }
86 if (function_exists($callback . '_validate')) {
87 $form['#validate'] = array($callback . '_validate');
88 }
89 }
90 }
91
92 /**
93 * Test if the form_id belongs to embed form.
94 *
95 * @param string $form_id
96 * @return form_callback | FALSE
97 */
98 function views_embed_form_is_embed_form($form_id) {
99 $embed_forms = views_embed_form_get_forms();
100
101 // maybe we can use rtrim and array_key_exists(), to avoid this iteration
102 foreach ($embed_forms as $key => &$val) {
103 if (strpos($form_id, $key) === 0) {
104 return $key;
105 }
106 }
107
108 return FALSE;
109 }
110
111 /**
112 * Returns all embedded forms accessible for current user.
113 * Uses static caching.
114 *
115 * @return array
116 */
117 function views_embed_form_get_forms() {
118 static $forms = array();
119
120 if (!count($forms)) {
121 foreach (module_implements('views_embed_form') as $module) {
122 $function = $module . '_views_embed_form';
123 $form_definiton = $function();
124 $forms = array_merge($forms, $form_definiton);
125 }
126 }
127
128 return $forms;
129 }
130
131
132 /**
133 * Test form for Views Embed Form. Doesn't do anything, just print a Node ID.
134 */
135
136 /**
137 * Implementation of hook_views_embed_form().
138 *
139 * Example of views_embed_form hook. You should always honor permissions during
140 * the hook implementation. This hook is checked during view editing and also
141 * when displaying every row.
142 */
143 function views_embed_form_views_embed_form() {
144 if (user_access('access content')) {
145 return array('vef_test_form' => 'Views Embed Form Test');
146 }
147 }
148
149 function vef_test_form(&$form_state, $row) {
150 $form = array();
151 $form['submit'] = array(
152 '#type' => 'submit',
153 '#name' => 'submit' . intval($row->nid),
154 '#value' => 'Click me!',
155 );
156 $form['nid'] = array(
157 '#type' => 'hidden',
158 '#value' => intval($row->nid),
159 );
160 return $form;
161 }
162 function vef_test_form_submit($form, &$form_state) {
163 drupal_set_message(t('You clicked a Views Embed Form Test for Node #@nid', array('@nid' => $form_state['values']['nid'])));
164 }
165

  ViewVC Help
Powered by ViewVC 1.1.2