4 // ---------------------------------------------------------------------------
8 * Implementation of hook_help()
10 function views_ui_help($section) {
12 case
'admin/help#views_ui':
13 return _views_ui_help_add();
14 case
'admin/build/views/import':
15 return t('You may import a view by cut-and-pasting the results of an export view. If the import is successful you will be taken to the Add View page with all of the settings of the imported view..');
16 case
'admin/build/views':
17 return t('This screen shows all of the views that are currently defined in your system. The default views are provided by Views and other modules and are automatically available. If a customized view of the same name exists, it will be used in place of a default view.');
19 if (!strncmp($section, 'admin/build/views', 11)) {
23 return t('Please see !s or the views documentation on drupal.org for help here.', array('!s' => l(t('the views help page'), 'admin/help/views_ui')));
25 return t('You may cut & paste this view into an import function on another system. The view will only work if all modules required by the view are installed on the target location.');
31 * Because the add/edit page is kind of complicated.
33 function _views_ui_help_add() {
34 $output = t('<p>A view retrieves some number of nodes from the database and displays them in a variety of formats.</p>');
35 $output .
= t("<h3>View Types</h3>
37 <dt><em>List View</em></dt><dd>A List View provides the data for each node retrieved in the form of an unordered list. Each item in the Fields section will be displayed; the Title will be displayed as a label. The order the items appear in the Fields list is the order the items will appear in the output. Leaving the title blank will cause the field to appear with no label (which is desirable in lists that just display titles, for example).</dd>
38 <dt><em>Table View</em></dt><dd>A Table View provides the data for each node as one row of a table. The Fields selected in the Fields list will be displayed in the order they are listed. The title column will be shown in the header. If you set the field to 'sortable' then the header will be click-sortable; be careful here as click-sorts will be processed after built-in sort criteria, and built-in sort criteria can easily make click-sorts invalid. If using click-sorts, choose a field to be the default sort; otherwise the first field presented will be the default sort.</dd>
39 <dt><em>Teaser List</em></dt><dd>A Teaser List will simply present the teaser of each node retrieved.</dd>
40 <dt><em>Full Nodes</em></dt><dd>This will show the full content of each node retrieved.</dd>
41 <dt><em>Random Teaser</em></dt><dd>This will show a single random teaser.</dd>
42 <dt><em>Random Node</em></dt><dd>This will show a single random node's full view.</dd>
46 $output .
= t("<h3>Fields</h3>\n");
47 $output .
= t("<p>When using List or Table view, it is necessary to choose which fields will be displayed to the user.</p><dl>\n");
48 $fields = _views_get_fields();
49 foreach ($fields as
$field) {
50 $output .
= "<dt><em>$field[name]</em></dt><dd>$field[help]</dd>\n";
54 $output .
= t("<h3>Arguments</h3>\n");
55 $output .
= t("<p>Arguments can be passed to the View through the URL, in order to create views that are configurable by the user. This is very useful to create views for taxonomy, or to sort by user. When using arguments, substitution is performed on the title. %1 will represent argument 1, %2 will represent argument 2. Each argument has a title field; this title is used if providing a summary view (which can matter because the argument is missing which could result in confusing phrases such as 'view for')</p><dl>\n");
56 $arguments = _views_get_arguments();
57 foreach ($arguments as
$argument) {
58 $output .
= "<dt><em>$argument[name]</em></dt><dd>$argument[help]</dd>\n";
62 $output .
= t("<h3>Filters</h3>\n");
63 $output .
= t("<p>Views may be filtered to restrict the view on a number of criteria.</p><dl>\n");
64 $filters = _views_get_filters();
65 foreach ($filters as
$filter) {
66 $output .
= "<dt><em>$filter[name]</em></dt><dd>$filter[help]</dd>\n";
70 $output .
= t("<h3>Sorting Critera</h3>\n");
71 $output .
= t("<p>The result set may be sorted on any of the following criteria.</p><dl>\n");
72 $sorts = _views_get_sorts();
73 foreach ($sorts as
$sort) {
74 $output .
= "<dt><em>$sort[name]</em></dt><dd>$sort[help]</dd>\n";
82 * Implementation of hook_perm()
84 function views_ui_perm() {
85 return array('administer views');
89 * Implementation of hook_menu()
91 function views_ui_menu($may_cache) {
95 $items[] = array('path' => 'admin/build/views',
96 'title' => t('Views'),
97 'callback' => 'views_ui_admin_page',
98 'access' => user_access('administer views'),
99 'description' => t('Views are customized lists of content on your system; they are highly configurable and give you control over how lists of content are presented.'),
100 'type' => MENU_NORMAL_ITEM
);
101 $items[] = array('path' => 'admin/build/views/list',
102 'title' => t('List'),
103 'callback' => 'views_ui_admin_page',
104 'access' => user_access('administer views'),
105 'type' => MENU_DEFAULT_LOCAL_TASK
,
107 $items[] = array('path' => 'admin/build/views/add',
109 'callback' => 'views_ui_admin_add_page',
110 'access' => user_access('administer views'),
111 'type' => MENU_LOCAL_TASK
);
112 $items[] = array('path' => 'admin/build/views/clone',
113 'title' => t('Clone'),
114 'callback' => 'views_ui_admin_clone_page',
115 'access' => user_access('administer views'),
116 'type' => MENU_CALLBACK
);
117 $items[] = array('path' => 'admin/build/views/edit',
118 'title' => t('Edit view'),
119 'callback' => 'views_ui_admin_edit_page',
120 'access' => user_access('administer views'),
121 'type' => MENU_CALLBACK
);
122 $items[] = array('path' => 'admin/build/views/import',
123 'title' => t('Import'),
124 'callback' => 'views_ui_admin_import_page',
125 'access' => user_access('administer views'),
126 'type' => MENU_LOCAL_TASK
);
127 $items[] = array('path' => 'admin/build/views/export',
128 'title' => t('Export view'),
129 'callback' => 'drupal_get_form',
130 'callback arguments' => array('views_ui_admin_export_page'),
131 'access' => user_access('administer views'),
132 'type' => MENU_CALLBACK
);
133 $items[] = array('path' => 'admin/build/views/delete',
134 'title' => t('Edit view'),
135 'callback' => 'drupal_get_form',
136 'callback arguments' => array('views_ui_admin_delete_confirm'),
137 'access' => user_access('administer views'),
138 'type' => MENU_CALLBACK
);
139 $items[] = array('path' => 'admin/build/views/enable',
140 'callback' => 'views_ui_admin_enable_page',
141 'access' => user_access('administer views'),
142 'type' => MENU_CALLBACK
);
143 $items[] = array('path' => 'admin/build/views/disable',
144 'callback' => 'views_ui_admin_disable_page',
145 'access' => user_access('administer views'),
146 'type' => MENU_CALLBACK
);
152 // ---------------------------------------------------------------------------
153 // Administrative Pages
156 * This page lists all system views and provides links to edit them.
158 function views_ui_admin_page() {
163 drupal_set_title(t('administer views'));
165 $result = pager_query("SELECT vid, name, description, menu_title, page_title, block_title, url, page, menu, block FROM {view_view} ORDER BY name", $numViews);
167 while ($view = db_fetch_object($result)) {
168 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
171 $provides[] = 'Page';
174 $provides[] = 'Block';
177 $provides[] = 'Menu';
181 views_get_title($view, 'menu'),
183 implode(', ', $provides),
185 theme('links', array(
186 array('title' => t('Edit'), 'href' => "admin/build/views/edit/$view->vid"),
187 array('title' => t('Export'), 'href' => "admin/build/views/export/$view->vid"),
188 array('title' => t('Delete'), 'href' => "admin/build/views/delete/$view->vid"),
189 array('title' => t('Clone'), 'href' => "admin/build/views/clone/$view->vid"),
195 $output = theme('table', array(t('View'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Actions')), $items, array("cellpadding" => "4"), t('Existing Views'));
196 $output .
= theme('pager', NULL
, $numViews);
199 $output .
= t('<p>No views have currently been defined.</p>');
202 $result = db_query("SELECT name FROM {view_view}");
203 while ($view = db_fetch_object($result)) {
204 $used[$view->name
] = true
;
207 $output .
= t('<p>Below are system default views; if you edit one of these, a view will be created that will override any system use of the view.</p>');
209 $default_views = _views_get_default_views();
211 $views_status = variable_get('views_defaults', array());
213 foreach ($default_views as
$view) {
214 $url = ($view->page ?
l($view->url
, $view->url
) : t('No Page View'));
216 if ($used[$view->name
]) {
217 $status = t('Overridden');
219 else if (isset($views_status[$view->name
])) {
220 if ($views_status[$view->name
] == 'enabled') {
221 $status = t('Enabled');
224 $status = t('Disabled');
227 else if ($view->disabled
) {
228 $status = t('Disabled');
231 $status = t('Enabled');
236 $provides[] = t('Page');
239 $provides[] = t('Block');
242 $provides[] = t('Menu');
246 $links[] = array('title' => t('Add'), 'href' => "admin/build/views/add/$view->name");
247 if ($status == t('Enabled')) {
248 $links[] = array('title' => t('Disable'), 'href' => "admin/build/views/disable/$view->name");
250 else if ($status == t('Disabled')) {
251 $links[] = array('title' => t('Enable'), 'href' => "admin/build/views/enable/$view->name");
254 $items[] = array($view->name
, views_get_title($view, 'menu'), $view->description
, implode(', ', $provides), $url, $status, theme('links', $links));
258 $output .
= theme('table', array(t('Default view'), t('Title'), t('Description'), t('Provides'), t('URL'), t('Status'), t('Actions')), $items, array("cellpadding" => "4"), t('Default Views'));
261 $output .
= t('<p>No views have currently been defined.</p>');
268 * Page to enable a disabled default view
270 function views_ui_admin_enable_page($view = '') {
274 $views_status = variable_get('views_defaults', array());
275 $views_status[$view] = 'enabled';
276 variable_set('views_defaults', $views_status);
279 drupal_goto('admin/build/views');
283 * Page to disable an enabled default view
285 function views_ui_admin_disable_page($view = '') {
289 $views_status = variable_get('views_defaults', array());
290 $views_status[$view] = 'disabled';
291 variable_set('views_defaults', $views_status);
294 drupal_goto('admin/build/views');
298 * Provide a textarea to paste a view export into.
300 function views_ui_admin_import_page() {
303 if ($_POST['form_id'] == 'views_edit_view') {
304 return views_ui_admin_add_page();
306 drupal_set_title("Import a View");
308 return drupal_get_form('views_ui_admin_import', $form);
311 function views_ui_admin_import() {
312 $form['view'] = array(
313 '#type' => 'textarea',
314 '#title' => t('Import View Code'),
317 '#description' => t('Cut and paste the results of an Export View here.'),
320 $form['submit'] = array(
322 '#value' => t("Submit"),
325 $form['#redirect'] = NULL
;
330 * Handle the submit button on importing a view.
332 function views_ui_admin_import_submit($formid, $form) {
337 $tables = array_keys(_views_get_tables());
339 if (!is_array($view->requires
) || !array_diff($view->requires
, $tables)) {
340 views_sanitize_view($view);
341 drupal_set_title(t('Add a View'));
342 $output = drupal_get_form('views_edit_view', $view, NULL
);
343 print theme('page', $output);
347 drupal_set_message(t("You don't seem to have the following requirements: ") .
implode(', ', array_diff($view->requires
, $tables)));
351 drupal_set_message(t('Unable to get a view out of that.'));
356 * Export a view for cut & paste.
358 function views_ui_admin_export_page($vid = '') {
361 $code = views_create_view_code($vid);
362 $lines = substr_count($code, "\n");
363 $form['code'] = array(
364 '#type' => 'textarea',
365 '#title' => $view->name
,
366 '#default_value' => $code,
372 * Provide a form to add a view. Allow adding a view from default templates.
374 function views_ui_admin_add_page($template = NULL
) {
378 if ($op == t('Cancel')) {
379 return 'admin/build/views';
382 $view = _views_get_default_view($template);
384 drupal_set_title(t('Add a View'));
386 return drupal_get_form('views_edit_view', $view, $op);
390 * Provide a form to clone a view.
392 function views_ui_admin_clone_page($viewname) {
396 if ($op == t('Cancel')) {
397 return 'admin/build/views';
400 $view = views_load_view($viewname);
402 return drupal_not_found();
406 drupal_set_title(t('Add a View'));
408 return drupal_get_form('views_edit_view', $view, $op);
412 * Provide a form to edit a view.
414 function views_ui_admin_edit_page($vid = '') {
418 if ($op == t('Cancel')) {
419 drupal_goto('admin/build/views');
422 if ($op == t('Delete')) {
423 drupal_goto("admin/build/views/delete/$vid");
426 if (!($view = _views_load_view($vid))) {
427 drupal_goto('admin/build/views');
430 drupal_set_title(t('Edit view %n', array('%n' => $view->name
)));
431 return drupal_get_form('views_edit_view', $view, $op);
435 * Provide a form to confirm deletion of a view.
437 function views_ui_admin_delete_confirm($vid = '') {
438 $view = _views_load_view($vid);
441 return drupal_goto('admin/build/views');
444 $form['vid'] = array('#type' => 'value', '#value' => $view->vid
);
445 // bdragon note: Did you mean to drop the above line on the floor with this?
446 $form = confirm_form($form,
447 t('Are you sure you want to delete %title?', array('%title' => $view->name
)),
448 $_GET['destination'] ?
$_GET['destination'] : 'admin/build/views',
449 t('This action cannot be undone.'),
450 t('Delete'), t('Cancel')
456 * Handle the submit button to delete a view.
458 function views_ui_admin_delete_confirm_submit($form_id, $form) {
459 _views_delete_view((object) $form);
461 drupal_goto('admin/build/views');
465 * Get an empty view with basic defaults.
467 function _views_get_default_view($template = '') {
469 $default_views = _views_get_default_views();
470 if (isset($default_views[$template])) {
471 $view = $default_views[$template];
475 $view = new
stdClass();
476 $view->use_pager
= true
;
477 $view->nodes_per_page
= variable_get('default_nodes_main', 10);
478 $view->page_header_format
= variable_get('filter_default_format', 1);
479 $view->page_footer_format
= variable_get('filter_default_format', 1);
480 $view->page_header_format
= variable_get('filter_default_format', 1);
481 $view->block_header_format
= variable_get('filter_default_format', 1);
482 $view->block_footer_format
= variable_get('filter_default_format', 1);
483 $view->block_header_format
= variable_get('filter_default_format', 1);
487 return _views_check_arrays($view);
490 // ---------------------------------------------------------------------------
491 // Select Box Definitions
493 // These should probably have string array keys that are easier to identify.
496 * Select box entries for argument defaults.
498 function _views_get_arguments_default() {
500 1 => t('Return Page Not Found'),
501 2 => t('Display All Values'),
502 3 => t('Summary, unsorted'),
503 4 => t('Summary, sorted ascending'),
504 5 => t('Summary, sorted descending'),
505 6 => t('Summary, sorted as view'),
506 7 => t('Use Empty Text'),
511 * Select box entries for sort ordering.
513 function _views_sortorders() {
515 'ASC' => t('Ascending'),
516 'DESC' => t('Descending')
521 * Swap two items in an array.
523 function _views_swap(&$arr, $a, $b) {
530 * Move an item up in an array.
532 function _views_move_up(&$arr, $i) {
533 if ($i <= 0 || $i >= count($arr)) {
534 return; // can't do it.
536 _views_swap($arr, $i - 1, $i);
540 * Move an item down in an array.
542 function _views_move_down(&$arr, $i) {
543 if ($i >= count($arr) - 1 || $i < 0) {
544 return; // can't do it.
546 _views_swap($arr, $i + 1, $i);
550 * Move an item to the front of an array.
552 function _views_move_top(&$arr, $i) {
553 if ($i <= 0 || $i >= count($arr)) {
554 return; // can't do it.
557 for ($x = $i; $x > 0; $x--)
558 $arr[$x] = $arr[$x - 1];
563 * Move an item to the end of an array.
565 function _views_move_bottom(&$arr, $i) {
566 $end = count($arr) - 1;
567 if ($i >= $end || $i < 0) {
568 return; // can't do it.
571 for ($x = $i; $x < $end; $x++)
572 $arr[$x] = $arr[$x + 1];
577 * Figure out which of the many, many buttons on a form were clicked and
580 function _views_check_sub_ops(&$form, &$order, $i) {
582 if ($form['delete']['#value']) {
583 unset($form['delete']['#value']);
585 $order = array_values($order); // reindex
586 $form['delete']['#printed'] = true
;
587 $form['up']['#printed'] = true
;
588 $form['down']['#printed'] = true
;
589 $form['top']['#printed'] = true
;
590 $form['bottom']['#printed'] = true
;
593 else foreach (array('up', 'down', 'top', 'bottom') as
$dir) {
594 if ($form[$dir]['#value']) {
595 unset ($form[$dir]['#value']);
596 $func = "_views_move_$dir";
605 * Figure out if one of the add buttons on a form were clicked, and handle it.
607 function _views_check_ops(&$view, $op, $form) {
608 if ($op == t('Add Filter')) {
609 $view->new_filter
['id'] = $form['filter']['add']['id']['#value'];
612 else if ($op == t('Add Criteria')) {
613 $view->new_sort
['id'] = $form['sort']['add']['id']['#value'];
616 else if ($op == t('Add Argument')) {
617 $view->new_argument
['id'] = $form['argument']['add']['id']['#value'];
620 else if ($op == t('Add Field')) {
621 $fieldbits = explode('.', $form['field']['add']['id']['#value']);
622 $view->new_field
['id'] = $form['field']['add']['id']['#value'];
623 $view->new_field
['tablename'] = $fieldbits[0];
624 $view->new_field
['field'] = $fieldbits[1];
625 $view->new_field
['label'] = $fieldnames[$form['field']['add']['id']['#value']];
626 $view->new_field
['queryname'] = "$fieldbits[0]_$fieldbits[1]";
629 else if ($op == t('Expose Filter')) {
630 $view->new_exposed_filter
['id'] = $form['exposed_filter']['add']['id']['#value'];
636 * Custom form element to do our nice images.
638 function views_elements() {
639 $type['views_imagebutton'] = array('#input' => TRUE
, '#button_type' => 'submit',);
643 function theme_views_imagebutton($element) {
644 return '<input type="image" class="form-'.
$element['#button_type'] .
'" name="'.
$element['#name'] .
'" value="'.
check_plain($element['#default_value']) .
'" '.
drupal_attributes($element['#attributes']) .
' src="' .
$element['#image'] .
'" alt="' .
$element['#title'] .
'" title="' .
$element['#title'] .
"\" />\n";
647 function views_imagebutton_value() {
648 // null function guarantees default_value doesn't get moved to #value.
652 * Set up the dynamic #options on a widget
654 function views_ui_setup_widget($widget, $default_value, $argument = NULL
) {
659 if (is_string($widget['#options']) && function_exists($widget['#options'])) {
660 $widget['#options'] = $widget['#options']('option', $argument);
662 if ($widget['#multiple'] && is_array($widget['#options'])) {
663 $widget['#size'] = min(count($widget['#options']), 8);
665 $widget['#default_value'] = $default_value;
670 * Display all the guts of a view in a form for editing.
672 function views_edit_view($view, $op = '') {
673 _views_check_arrays($view); // make sure arrays that might be empty get set
675 // Put in all our add buttons, then process them to see if they've been hit.
677 views_ui_add_add_button($form, 'field', _views_get_fields(true
), t('Add Field'));
678 views_ui_add_add_button($form, 'argument', _views_get_arguments(true
), t('Add Argument'));
679 views_ui_add_add_button($form, 'filter', _views_get_filters(true
), t('Add Filter'));
680 views_ui_add_add_button($form, 'sort', _views_get_sorts(true
), t('Add Criteria'));
682 $allbut = _views_check_ops($view, $op, $form);
683 if ($_POST['edit'] && $op != t('Save')) {
684 drupal_set_message(t('You have modified this view; changes will not be recorded until you Save the form.'));
687 $form['exposed_filter'] = array();
688 foreach (array('field', 'argument', 'filter', 'exposed_filter', 'sort') as
$section) {
689 if (views_ui_add_section($form[$section], $view, $section)) {
694 $form['vid'] = array(
696 '#value' => $view->vid
,
698 $form['allbut'] = array(
702 $form['changed'] = array(
704 '#value' => $view->changed
,
707 $form['basic-info'] = array(
708 '#type' => 'fieldset',
709 '#collapsible' => true
,
710 '#collapsed' => ($allbut != NULL
),
711 '#title' => t('Basic Information'),
714 $form['basic-info']['name'] = array(
715 '#type' => 'textfield',
716 '#title' => t('Name'),
717 '#default_value' => $view->name
,
720 '#description' => t('The unique identifier of the view; it is only important for overridden views and views that modules or themes will need to use. Only alphanumeric and _ allowed here'),
724 $form['basic-info']['access'] = array(
725 '#type' => 'checkboxes',
726 '#title' => t('Access'),
727 '#default_value' => $view->access
,
728 '#options' => views_handler_filter_role(),
729 '#description' => t('Only the checked roles will be able to see this view in any form; if no roles are checked, access will not be restricted.'),
732 $form['basic-info']['description'] = array(
733 '#type' => 'textfield',
734 '#title' => t('Description'),
735 '#default_value' => $view->description
,
738 '#description' => t('A description of the view for the admin list.'),
743 $form['page-info'] = array(
744 '#type' => 'fieldset',
745 '#collapsible' => true
,
746 '#collapsed' => ($allbut != NULL
|| !$view->page
),
747 '#title' => t('Page'),
750 $form['page-info']['page'] = array(
751 '#type' => 'checkbox',
752 '#title' => t('Provide Page View'),
753 '#return_value' => 1,
754 '#default_value' => $view->page
,
755 '#description' => t('If checked this view will be provided as a page. If not checked, the fields in this group will be ignored.'),
758 $form['page-info']['url'] = array(
759 '#type' => 'textfield',
760 '#title' => t('URL'),
761 '#default_value' => $view->url
,
764 '#description' => t('Enter the URL to use for this view in the form of \'dir/dir\'. Do not begin or end the URL with a /. Example: \'view/tracker\'. This is required if providing a page view. You can also add $arg as a placeholder for arguments passed in the URL, for example \'user/$arg/tracker\' or \'view/taxonomy/$arg\'.'),
767 $form['page-info']['page_type'] = array(
769 '#title' => t('View Type'),
770 '#default_value' => $view->page_type
,
771 '#options' => _views_get_style_plugins(true
),
772 '#description' => t('How the nodes should be displayed to the user.'),
775 $form['page-info']['page_title'] = array(
776 '#type' => 'textfield',
777 '#title' => t('Title'),
778 '#default_value' => $view->page_title
,
781 '#description' => t('The title that be shown at the top of the view. May be blank. This title ignores arguments; if you want your title to take arguments into account, use the "title" field in the arguments section.'),
784 $form['page-info']['use_pager'] = array(
785 '#type' => 'checkbox',
786 '#title' => t('Use Pager'),
787 '#return_value' => 1,
788 '#default_value' => $view->use_pager
,
789 '#description' => t('If checked this query may be multiple pages. If not checked this query will be one page.'),
791 $form['page-info']['breadcrumb_no_home'] = array(
792 '#type' => 'checkbox',
793 '#title' => t('Breadcrumb trail should not include "Home"'),
794 '#return_value' => 1,
795 '#default_value' => $view->breadcrumb_no_home
,
796 '#description' => t('If checked the breadcrumb trail for this page will discard "Home". Usually you will not set this, but this is used for the Front Page View, where it IS Home and should not leave a trail to itself.'),
798 $form['page-info']['nodes_per_page'] = array(
799 '#type' => 'textfield',
800 '#title' => t('Nodes per Page'),
801 '#default_value' => intval($view->nodes_per_page
),
804 '#description' => t('The number of nodes to display per page. If 0, all nodes will be displayed. If not using a pager, this will be the maximum number of nodes in the list.'),
805 '#attributes' => NULL
,
808 $form['page-info']['page_header_fieldset'] = array(
809 '#type' => 'fieldset',
810 '#collapsible' => TRUE
,
811 '#collapsed' => TRUE
,
812 '#title' => t('Header'),
814 $form['page-info']['page_header_fieldset']['page_header'] = array(
815 '#type' => 'textarea',
816 '#default_value' => $view->page_header
,
819 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
822 $form['page-info']['page_header_fieldset']['page_header_format'] = filter_form($view->page_header_format
, 1, array('page_header_format'));
824 $form['page-info']['page_footer_fieldset'] = array(
825 '#type' => 'fieldset',
826 '#collapsible' => TRUE
,
827 '#collapsed' => TRUE
,
828 '#title' => t('Footer'),
830 $form['page-info']['page_footer_fieldset']['page_footer'] = array(
831 '#type' => 'textarea',
832 '#default_value' => $view->page_footer
,
835 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
838 $form['page-info']['page_footer_fieldset']['page_footer_format'] = filter_form($view->page_footer_format
, 1, array('page_footer_format'));
840 $form['page-info']['page_empty_fieldset'] = array(
841 '#type' => 'fieldset',
842 '#collapsible' => TRUE
,
843 '#collapsed' => TRUE
,
844 '#title' => t('Empty Text'),
846 $form['page-info']['page_empty_fieldset']['page_empty'] = array(
847 '#type' => 'textarea',
848 '#default_value' => $view->page_empty
,
851 '#description' => t('Text to display if a view returns no nodes. Optional.'),
854 $form['page-info']['page_empty_fieldset']['page_empty_format'] = filter_form($view->page_empty_format
, 1, array('page_empty_format'));
856 $form['page-info']['menu-info'] = array(
857 '#type' => 'fieldset',
858 '#collapsible' => TRUE
,
859 '#collapsed' => TRUE
,
860 '#title' => t('Menu'),
863 $form['page-info']['menu-info']['menu'] = array(
864 '#type' => 'checkbox',
865 '#title' => t('Provide Menu'),
866 '#return_value' => 1,
867 '#default_value' => $view->menu
,
868 '#description' => t('If checked this view be given a menu entry in the Drupal menu system. If not checked the data in this group will be ignored.'),
871 $form['page-info']['menu-info']['menu_tab'] = array(
872 '#type' => 'checkbox',
873 '#title' => t('Provide Menu as Tab'),
874 '#return_value' => 1,
875 '#default_value' => $view->menu_tab
,
876 '#description' => t("If checked this view's menu entry will be provided as a tab rather than in the main menu system."),
879 $form['page-info']['menu-info']['menu_tab_default'] = array(
880 '#type' => 'checkbox',
881 '#title' => t('Make Default Menu Tab'),
882 '#return_value' => 1,
883 '#default_value' => $view->menu_tab_default
,
884 '#description' => t("If checked this view's menu entry will be provided as a tab, and will be the default tab for that URL path. For example, if the URL is 'tracker/all' and it is set as the default menu tab, it will be put into the menu as 'tracker' and 'tracker/all' will be the default tab. For tabs to work properly, one tab in the group must be set as the default."),
887 $form['page-info']['menu-info']['menu_tab_weight'] = array(
888 '#type' => 'textfield',
889 '#title' => t('Tab Weight'),
890 '#default_value' => $view->menu_tab_weight
,
892 '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
895 $form['page-info']['menu-info']['menu_title'] = array(
896 '#type' => 'textfield',
897 '#title' => t('Menu Title'),
898 '#default_value' => $view->menu_title
,
901 '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
906 $form['block-info'] = array(
907 '#type' => 'fieldset',
908 '#collapsible' => true
,
909 '#collapsed' => ($allbut != NULL
|| !$view->block
),
910 '#title' => t('Block'),
913 $form['block-info']['block'] = array(
914 '#type' => 'checkbox',
915 '#title' => t('Provide Block'),
916 '#return_value' => 1,
917 '#default_value' => $view->block
,
918 '#description' => t('If checked this view will be provided as a block. If checked title may not be blank.'),
921 $form['block-info']['block_type'] = array(
923 '#title' => t('View Type'),
924 '#default_value' => $view->block_type
,
925 '#options' => _views_get_style_plugins(true
),
926 '#description' => t('How the nodes should be displayed to the user.'),
929 $form['block-info']['block_title'] = array(
930 '#type' => 'textfield',
931 '#title' => t('Title'),
932 '#default_value' => $view->block_title
,
935 '#description' => t('The title that will be shown at the top of the block. May be blank.'),
938 $form['block-info']['nodes_per_block'] = array(
939 '#type' => 'textfield',
940 '#title' => t('Nodes per Block'),
941 '#default_value' => $view->nodes_per_block
,
944 '#description' => t('If using a block, the maximum number of items to display in the block. Pagers are not used in blocks.'),
945 '#attributes' => NULL
,
948 $form['block-info']['block_more'] = array(
949 '#type' => 'checkbox',
950 '#title' => t('[More] Link?'),
951 '#return_value' => 1,
952 '#default_value' => $view->block_more
,
953 '#description' => t('If using a view as both a page and a block, display a more link in the block that links to the view URL?'),
956 $form['block-info']['block_header_fieldset'] = array(
957 '#type' => 'fieldset',
958 '#collapsible' => TRUE
,
959 '#collapsed' => TRUE
,
960 '#title' => t('Header'),
962 $form['block-info']['block_header_fieldset']['block_use_page_header'] = array(
963 '#type' => 'checkbox',
964 '#title' => t('Use Page Header'),
965 '#return_value' => 1,
966 '#default_value' => $view->block_use_page_header
,
967 '#description' => t('If checked, use the Page Header for block view instead. If so, you should leave the Block Header blank.'),
970 $form['block-info']['block_header_fieldset']['block_header'] = array(
971 '#type' => 'textarea',
972 '#title' => t('Header'),
973 '#default_value' => $view->block_header
,
976 '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
979 $form['block-info']['block_header_fieldset']['block_header_format'] = filter_form($view->block_header_format
, 1, array( 'block_header_format'));
981 $form['block-info']['block_footer_fieldset'] = array(
982 '#type' => 'fieldset',
983 '#collapsible' => TRUE
,
984 '#collapsed' => TRUE
,
985 '#title' => t('Footer'),
987 $form['block-info']['block_footer_fieldset']['block_use_page_footer'] = array(
988 '#type' => 'checkbox',
989 '#title' => t('Use Page Footer'),
990 '#return_value' => 1,
991 '#default_value' => $view->block_use_page_footer
,
992 '#description' => t('If checked, use the page footer for block view instead. If so, you should leave the block footer blank.'),
995 $form['block-info']['block_footer_fieldset']['block_footer'] = array(
996 '#type' => 'textarea',
997 '#title' => t('Footer'),
998 '#default_value' => $view->block_footer
,
1001 '#description' => t('Text to display at the bottom of the view. May contain an explanation or links or whatever you like. Optional.'),
1004 $form['block-info']['block_footer_fieldset']['block_footer_format'] = filter_form($view->block_footer_format
, 1, array( 'block_footer_format'));
1006 $form['block-info']['block_empty_fieldset'] = array(
1007 '#type' => 'fieldset',
1008 '#collapsible' => TRUE
,
1009 '#collapsed' => TRUE
,
1010 '#title' => t('Empty text'),
1012 $form['block-info']['block_empty_fieldset']['block_use_page_empty'] = array(
1013 '#type' => 'checkbox',
1014 '#title' => t('Use Page empty'),
1015 '#return_value' => 1,
1016 '#default_value' => $view->block_use_page_empty
,
1017 '#description' => t('If checked, use the Page Empty Text for block view instead. If so, you should leave the block empty text blank.'),
1020 $form['block-info']['block_empty_fieldset']['block_empty'] = array(
1021 '#type' => 'textarea',
1022 '#title' => t('Empty text'),
1023 '#default_value' => $view->block_empty
,
1026 '#description' => t('Text to display if a view results in no nodes. Optional.'),
1029 $form['block-info']['block_empty_fieldset']['block_empty_format'] = filter_form($view->block_empty_format
, 1, array( 'block_empty_format'));
1031 $access = user_access('use PHP for block visibility');
1033 $form['view_args_php_fieldset'] = array(
1034 '#type' => 'fieldset',
1035 '#collapsible' => TRUE
,
1036 '#collapsed' => empty($view->view_args_php
) ? TRUE
: FALSE
,
1037 '#title' => t('Argument Handling Code'),
1040 $form['view_args_php_fieldset']['view_args_php'] = array(
1041 '#type' => 'textarea',
1042 '#title' => t('Argument Code'),
1043 '#default_value' => $view->view_args_php
,
1046 '#description' => '<p>'.
t('Advanced Usage Only: PHP code that returns a custom array of arguments for the view. Should not include <?php ?> delimiters.') .
'</p>' .
1047 '<p>'.
t('For more information, please see the <a href="!arg">Argument Handling Code documentation</a> in the Drupal handbook.', array('%arg' => 'http://drupal.org/node/70145')) .
'</p>',
1050 $form['view_args_php_fieldset']['view_args_php'] = array(
1052 '#value' => $view->view_args_php
,
1056 $form['save'] = array(
1057 '#type' => 'submit',
1058 '#value' => t('Save'),
1061 $form['delete'] = array(
1062 '#type' => 'submit',
1063 '#value' => t('Delete'),
1066 $form['cancel'] = array(
1067 '#type' => 'submit',
1068 '#value' => t('Cancel'),
1075 * separately build a piece of the form
1077 function views_ui_build_form(&$form) {
1078 // NOT REALLY SURE WHY THIS SORT OF WORKS. IT'S MAGIC THAT CHX POINTED ME TO
1079 // IN THE POLL MODULE.
1080 $form['#post'] = $_POST;
1081 $form = form_builder('views_edit_view', $form);
1085 * Add the 'add' button for a section into the form
1087 function views_ui_add_add_button(&$form, $section, $options, $label) {
1088 $form[$section]['add'] = array('#tree' => true
, '#parents' => array($section, 'add'));
1090 $form[$section]['add']['id'] = array(
1091 '#type' => 'select',
1092 '#options' => $options,
1094 $form[$section]['add']['button'] = array(
1095 '#type' => 'button',
1098 views_ui_build_form($form[$section]['add']);
1102 * Add one of the list sections to the form.
1104 function views_ui_add_section(&$form, &$view, $section) {
1105 // add fields to the form.
1107 $form += array('#tree' => true
, '#parents' => array($section));
1108 $view_section = &$view->$section;
1110 $num_items = count($view_section);
1111 // Populate the form with fields we need to check to track
1112 // changes through the form's life.
1113 $form['count'] = array('#type' => 'hidden', '#default_value' => $num_items);
1115 $form['order'] = array(
1116 '#type' => 'hidden',
1117 '#default_value' => $num_items ?
implode(',', range(0, $num_items - 1)) : '',
1120 // Run the builder to get the value on the form.
1121 views_ui_build_form($form);
1122 $order = $form['order']['#value'] != ''
1123 ?
explode(',', $form['order']['#value'])
1126 // Shortcut because operator precedence gets sticky if using $foo->$bar[]
1127 for ($i = $num_items; $i < $form['count']['#value']; $i++) {
1128 $view_section[] = array();
1131 $new_section = "new_$section";
1132 // instantiate the new field if one was added.
1133 if ($view->$new_section) {
1134 $view_section[] = $view->$new_section;
1135 $order[] = $form['count']['#value'];
1136 $form['count']['#value']++;
1139 $func = "views_ui_add_$section";
1141 foreach ($order as
$key => $i) {
1142 $form[$i] = array('#tree' => true
, '#parents' => array($section, $i));
1144 $form[$i]['id'] = array(
1145 '#type' => 'hidden',
1146 '#default_value' => $view_section[$i]['id'],
1149 views_ui_add_buttons($form[$i]);
1150 views_ui_build_form($form[$i]);
1152 $retval = _views_check_sub_ops($form[$i], $order, $key);
1153 if ($retval !== 'delete') {
1154 $retval2 = $func($form[$i], $view_section[$i], $order, $key, $i, $view);
1157 if ($retval || $retval2) {
1161 $form['order']['#value'] = implode(',', $order);
1167 * Add all the info for a single field into the form.
1169 function views_ui_add_field(&$form, $field, &$order, $key, $i) {
1170 $fields = _views_get_fields();
1171 $fieldname = $form['id']['#value']; // combined table & fieldname
1172 $fieldinfo = $fields[$fieldname]; // laziness + readability
1174 $form['fullname'] = array(
1175 '#type' => 'hidden',
1176 '#default_value' => $field['id'],
1179 $form['name'] = array(
1180 '#type' => 'markup',
1181 '#value' => $fieldinfo['name'], // combined table & fieldname
1183 $form['queryname'] = array(
1184 '#type' => 'hidden',
1185 '#default_value' => $field['queryname'],
1187 $form['tablename'] = array(
1188 '#type' => 'hidden',
1189 '#default_value' => $field['tablename'],
1191 $form['field'] = array(
1192 '#type' => 'hidden',
1193 '#default_value' => $field['field'],
1196 $form['label'] = array(
1197 '#type' => 'textfield',
1198 '#default_value' => $field['label'],
1200 '#maxlength' => 255,
1203 if (is_array($fieldinfo['handler'])) {
1204 $form['handler'] = array(
1205 '#type' => 'select',
1206 '#default_value' => $field['handler'],
1207 '#options' => $fieldinfo['handler'],
1211 if (isset($fieldinfo['option'])) {
1212 $form['options'] = views_ui_setup_widget($fieldinfo['option'], $field['options']);
1216 if ($fieldinfo['sortable']) {
1217 $form['sortable'] = array(
1218 '#type' => 'select',
1219 '#default_value' => $field['sortable'],
1220 '#options' => array('0' => t('No'), '1' => t('Yes')),
1222 $form['defaultsort'] = array(
1223 '#type' => 'select',
1224 '#default_value' => $field['defaultsort'],
1225 '#options' => array('0' => t('None'), 'ASC' => t('Ascending'), 'DESC' => t('Descending')),
1228 if (isset($fieldinfo['help'])) {
1229 $form['help'] = array(
1230 '#type' => 'markup',
1231 '#value' => '<div class="description">' .
$fieldinfo['help'] .
'</div>',
1237 * Add all the info for a single argument into the form.
1239 function views_ui_add_argument(&$form, $argument, &$order, $key, $i) {
1240 $arguments = _views_get_arguments();
1241 $arg_type = $form['id']['#value'];
1242 $arginfo = $arguments[$arg_type];
1244 $form['type'] = array(
1245 '#type' => 'hidden',
1246 '#default_value' => $argument['id'],
1249 $form['name'] = array(
1250 '#value' => $arginfo['name'],
1253 $form['argdefault'] = array(
1254 '#type' => 'select',
1255 '#default_value' => max(1, intval($argument['argdefault'])),
1256 '#options' => _views_get_arguments_default(),
1258 $form['title'] = array(
1259 '#type' => 'textfield',
1260 '#default_value' => $argument['title'],
1262 '#maxlength' => 255,
1264 if (isset($arginfo['option'])) {
1265 $form['options'] = views_ui_setup_widget($arginfo['option'], $argument['options']);
1267 $form['wildcard'] = array(
1268 '#type' => 'textfield',
1269 '#default_value' => $argument['wildcard'],
1273 $form['wildcard_substitution'] = array(
1274 '#type' => 'textfield',
1275 '#default_value' => $argument['wildcard_substitution'],
1279 if (isset($arginfo['help'])) {
1280 $form['help'] = array(
1281 '#type' => 'markup',
1282 '#value' => $arginfo['help'],
1288 * Add all the info for a single filter into the form.
1290 function views_ui_add_filter(&$form, $filter, &$order, $key, $i, &$view) {
1291 $filters = _views_get_filters();
1292 $filtername = $form['id']['#value']; // combined table & filtername
1293 $filterinfo = $filters[$filtername];
1295 $form['field'] = array(
1296 '#type' => 'hidden',
1297 '#default_value' => $filter['id'],
1300 $form['name'] = array(
1301 '#type' => 'markup',
1302 '#value' => $filterinfo['name'],
1305 // 'operator' is either an array or a handler
1306 $operator = $filterinfo['operator'];
1307 if (!is_array($operator) && function_exists($filterinfo['operator'])) {
1308 $operator = $filterinfo['operator']('operator', $filterinfo);
1311 $form['operator'] = array(
1312 '#type' => 'select',
1313 '#default_value' => $filter['operator'],
1314 '#options' => $operator,
1316 if (isset($filterinfo['option'])) {
1317 $form['options'] = views_ui_setup_widget($filterinfo['option'], $filter['options']);
1320 $form['value'] = views_ui_setup_widget($filterinfo['value'], $filter['value'], $filterinfo);
1322 if (isset($filterinfo['help'])) {
1323 $form['help'] = array(
1324 '#type' => 'markup',
1325 '#value' => $filterinfo['help'],
1329 // This is kind of a pain in the butt -- The only way to know all of
1330 // the filters that have been exposed is to look at the form, but that
1331 // part of the form hasn't been built yet, so I can't use the form_builder
1332 // method. I can't put this after that, either, because then I'd have to
1333 // go back to add one if we see one here. So we cheat heavily with _POST.
1335 if ($_POST['edit']) {
1336 $exposed_order = $_POST['edit']['exposed_filter']['order'];
1337 if ($exposed_order !== '') {
1338 $exposed_order = explode(',', $exposed_order);
1339 foreach ($exposed_order as
$x) {
1340 if ($_POST['edit']['exposed_filter'][$x]['id'] == $filtername) {
1347 else { // do this if there's never been a submit.
1348 $exposed_order = array_keys($view->exposed_filter
);
1349 foreach ($exposed_order as
$x) {
1350 if ($view->exposed_filter
[$x]['id'] == $filtername) {
1357 // I can't find a way to see if this button was pressed other than
1358 // directly checking, because #form_submitted doesn't work on arrays
1360 if ($_POST['edit']['filter'][$i]['expose']) {
1361 $view->new_exposed_filter
['id'] = $filtername;
1365 $form['expose'] = array(
1366 '#type' => 'button',
1367 '#default_value' => t('Expose'),
1368 '#name' => "edit[filter][$i][expose]",
1375 * Add all the info for a single exposed filter into the form.
1377 function views_ui_add_exposed_filter(&$form, $filter, &$order, $key, $i) {
1378 $filters = _views_get_filters();
1379 $filtername = $form['id']['#value']; // combined table & filtername
1380 $filterinfo = $filters[$filtername];
1382 $form['field'] = array(
1383 '#type' => 'hidden',
1384 '#default_value' => $filter['id'],
1387 $form['name'] = array(
1388 '#type' => 'markup',
1389 '#value' => $filterinfo['name'],
1392 $form['label'] = array(
1393 '#type' => 'textfield',
1394 '#default_value' => $filter['label'],
1396 '#maxlength' => 255,
1399 $form['optional'] = array(
1400 '#type' => 'checkbox',
1401 '#default_value' => $filter['optional'],
1404 $form['is_default'] = array(
1405 '#type' => 'checkbox',
1406 '#default_value' => $filter['is_default'],
1409 $form['single'] = array(
1410 '#type' => 'checkbox',
1411 '#default_value' => $filter['single'],
1414 $form['operator'] = array(
1415 '#type' => 'checkbox',
1416 '#default_value' => $filter['operator'],
1421 * Add all the info for a single sort into the form.
1423 function views_ui_add_sort(&$form, $sort, &$order, $key, $i) {
1424 $sorts = _views_get_sorts();
1425 $sortname = $form['id']['#value']; // combined table & sortname
1426 $sortinfo = $sorts[$sortname];
1428 $form['field'] = array(
1429 '#type' => 'hidden',
1430 '#default_value' => $sort['id'],
1433 $form['name'] = array(
1434 '#value' => $sortinfo['name'],
1437 $form['sortorder'] = array(
1438 '#type' => 'select',
1440 '#default_value' => $sort['sortorder'],
1441 '#options' => _views_sortorders(),
1443 // option can be either 'string', 'integer', an array or a handler
1444 // that must return an array.
1445 if (isset($sortinfo['option'])) {
1446 $form['options'] = views_ui_setup_widget($sortinfo['option'], $sort['options']);
1448 if (isset($sortinfo['help'])) {
1449 $form['help'] = array(
1450 '#type' => 'markup',
1451 '#value' => $sortinfo['help'],
1457 * Add the up, down, top, bottom, delete buttons to a form.
1459 function views_ui_add_buttons(&$form_item) {
1460 $form_item['delete'] = views_ui_add_button('user-trash.png', t('Delete'), t('Delete this item.'));
1461 $form_item['up'] = views_ui_add_button('go-up.png', t('Up'), t('Move this item up.'));
1462 $form_item['down'] = views_ui_add_button('go-down.png', t('Down'), t('Move this item down.'));
1463 $form_item['top'] = views_ui_add_button('go-top.png', t('Top'), t('Move this item to the top.'));
1464 $form_item['bottom'] = views_ui_add_button('go-bottom.png', t('Bottom'), t('Move this item to the bottom.'));
1468 * Add a single button to a form.
1470 function views_ui_add_button($image, $name, $text) {
1471 $module_path = base_path() .
drupal_get_path('module', 'views');
1474 '#type' => 'views_imagebutton',
1475 '#image' => $module_path .
'/' .
$image,
1477 '#default_value' => $name,
1482 * Display the form built by _views_view_form()
1484 function theme_views_edit_view($form) {
1486 $output .
= drupal_render($form['basic-info'], false
);
1487 $output .
= drupal_render($form['page-info'], false
);
1488 $output .
= drupal_render($form['block-info'], false
);
1490 $allbut = $form['allbut']['#value'];
1492 // drupal_render wants a reference so use full array notation.
1495 $collapsed = $allbut && $allbut != 'field';
1496 $group = views_ui_render_section($form['field'], array('name', 'label', 'handler', 'options', 'sortable', 'defaultsort'), array(t('Name'), t('Label'), t('Handler'), t('Option'), t('Sortable'), t('Default Sort')), 'fields', $collapsed);
1497 $group .
= views_ui_render_section_add($form['field']['add'], array('id', 'button'), t('Add Field'));
1499 $group .
= '<p>'.
t('Fields are only meaningful with List view and Table View; they allow you to choose which fields are presented and in what order.') .
'</p>';
1500 $output .
= theme('fieldset', array('#title' => t('Fields'), '#children' => $group,
1501 '#collapsible' => true
, '#collapsed' => $collapsed,));
1505 $collapsed = $allbut && $allbut != 'argument';
1506 $group = views_ui_render_section($form['argument'], array('name', 'argdefault', 'title', 'options', 'wildcard', 'wildcard_substitution'), array(t('Argument Type'), t('Default'), t('Title'), t('Option'), t('Wildcard'), t('Wildcard Sub')), 'arguments', $collapsed);
1508 $group .
= views_ui_render_section_add($form['argument']['add'], array('id', 'button'), t('Add Argument'));
1510 $group .
= drupal_render($form['view_args_php_fieldset']);
1512 $group .
= '<p>'.
t('Arguments are parsed directly from the URL. They are not necessary to any given view, but allow flexibility.') .
'</p>';
1513 $output .
= theme('fieldset', array('#title' => t('Arguments'), '#children' => $group,
1514 '#collapsible' => true
, '#collapsed' => $collapsed,));
1517 $collapsed = $allbut && $allbut != 'filter';
1518 $group = views_ui_render_section($form['filter'], array('name', 'operator', 'value', 'options', 'expose'), array(t('Field'), t('Operator'), t('Value'), t('Option'), ''), 'filters', $collapsed);
1520 $group .
= views_ui_render_section_add($form['filter']['add'], array('id', 'button'), t('Add Filter'));
1522 $group .
= '<p>'.
t('Filters allow you to select a subset of all the nodes to display. All Filters are ANDed together.') .
'</p>';
1523 $output .
= theme('fieldset', array('#title' => t('Filters'), '#children' => $group,
1524 '#collapsible' => true
, '#collapsed' => $collapsed,));
1527 // exposed exposed_filter
1528 $collapsed = $allbut && $allbut != 'exposed_filter';
1529 $group = views_ui_render_section($form['exposed_filter'], array('name', 'label', 'optional', 'is_default', 'single', 'operator'), array(t('Field'), t('Label'), t('Optional'), t('Filter settings Default'), t('Force Single'), t('Lock Operator')), 'exposed_filters', $collapsed);
1532 // $group .= views_ui_render_section_add($form['exposed_filter']['add'], array('id', 'button'), t('Expose Filter'));
1534 $group .
= '<p>'.
t('Exposed filters will be presented to the viewer. If not set required, then filters will include a "<None>" Value if possible. If set default, filters will default as set here, otherwise filter settings will be ignored. If Lock Operator is set, no operator will be made available to the user.') .
'</p>';
1535 $output .
= theme('fieldset', array('#title' => t('Exposed Filters'), '#children' => $group,
1536 '#collapsible' => true
, '#collapsed' => $collapsed,));
1540 $collapsed = $allbut && $allbut != 'sort';
1541 $group = views_ui_render_section($form['sort'], array('name', 'sortorder', 'options'), array(t('Field'), t('Order'), t('Option')), 'sort criteria', $collapsed);
1543 $group .
= views_ui_render_section_add($form['sort']['add'], array('id', 'button'), t('Add criteria'));
1544 $output .
= theme('fieldset', array('#title' => t('Sort Criteria'), '#children' => $group,
1545 '#collapsible' => true
, '#collapsed' => $collapsed,));
1547 $output .
= drupal_render($form, false
);
1552 * Render one of the dynamic sections on our form.
1554 function views_ui_render_section(&$form, $items, $header, $section, &$collapsed) {
1555 // always do the buttons
1556 $items = array_merge($items, array('delete', 'top', 'up', 'down', 'bottom'));
1557 $num_items = count($items);
1559 $order = ($form['order']['#value'] != ''
1560 ?
explode(',', $form['order']['#value'])
1563 $count = count($order) - 1;
1564 foreach ($order as
$key => $i) {
1566 foreach ($items as
$item) {
1567 if (($key == 0 && ($item == 'up' || $item == 'top')) ||
1568 ($key == $count && ($item == 'down' || $item == 'bottom'))) {
1569 $form[$i][$item]['#printed'] = true
;
1570 $row[] = ' '; // array('data' => ' ', 'width' => 16);
1573 $row[] = drupal_render($form[$i][$item], false
);
1577 if (isset($form[$i]['help'])) {
1578 $rows[] = array(array('data' => drupal_render($form[$i]['help']), 'colspan' => $num_items));
1582 $rows[] = array(array('data' => t('This view currently has no %s defined.', array('%s' => $section)), 'colspan' => $num_items));
1586 $header[] = array('data' => t('Ops'), 'colspan' => 5);
1587 return theme('table', $header, $rows) .
"<br />";
1591 * Render the add buttons for one of the dynamic sections on our form.
1593 function views_ui_render_section_add(&$form, $items, $label) {
1594 foreach ($items as
$item) {
1595 $row[] = drupal_render($form[$item], false
);
1599 $header = array(array('data' => $label, 'colspan' => count($items)));
1600 return theme('table', $header, $rows) .
"<br />";
1604 * Validate that a view sent via form is OK.
1606 function views_edit_view_validate($form_id, $view, $form) {
1608 if ($op != t('Save')) {
1609 return; // only validate on saving!
1613 $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
1614 if ($changed && $view['changed'] != $changed) {
1615 form_set_error('', t('Another user has modified this view, unable to save. You can get this error by using the Back button to re-edit a view after saving one; if you do this, be sure to Reload before making any changes!'));
1620 if (!$view['name']) {
1621 form_error($form['basic-info']['name'], t('View name is required.'));
1624 // view name must be alphanumeric or underscores, no other punctuation.
1625 if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
1626 form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
1629 // test uniqueness of name
1630 $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
1631 if ($vid && $vid != $view['vid']) {
1632 form_error($form['basic-info']['name'], t('View name already in use.'));
1635 if ($view['page']) {
1636 if ($view['use_pager'] && !$view['nodes_per_page']) {
1637 form_error($form['page-info']['nodes_per_page'], t('Nodes per page cannot be 0 if using the pager.'));
1641 if ($view['block'] && $view['nodes_per_block'] < 1) {
1642 form_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
1645 // validation based on type:
1647 $plugins = _views_get_style_plugins();
1648 if ($view['page']) {
1649 $validator = $plugins[$view['page_type']]['validate'];
1650 if (function_exists($validator)) {
1651 $validator('page', $view, $form);
1655 if ($view['block']) {
1656 $validator = $plugins[$view['block_type']]['validate'];
1657 if (function_exists($validator)) {
1658 $validator('block', $view, $form);
1662 foreach (array('field', 'argument', 'sort', 'filter') as
$type) {
1663 $function = "_views_get_$type" .
's';
1664 $info = $function();
1665 if (is_array($view->$type)) {
1666 foreach ($view->$type as
$key => $data) {
1667 if (!is_numeric($key)) {
1668 continue; // some non-data data is in here.
1670 $validator = $info[$data['id']]['validate'];
1671 if (function_exists($validator)) {
1672 $validator($data, $view, $form);
1679 function views_ui_reorder(&$view) {
1680 // re-order things as they were ordered on the form.
1681 $order = $view['order'] != '' ?
explode(',', $view['order']) : array();
1682 foreach ($order as
$position => $key) {
1683 $placeholder[] = $view[$key];
1685 $view = $placeholder;
1689 * Handle submit buttons on a view form.
1691 function views_edit_view_submit($form_id, $form) {
1692 $view = (object) $form;
1694 // Transform any data that requires it here.
1695 $view->access
= array_keys(array_filter($view->access
));
1697 // ensure for MYSQL 5 irritation reasons.
1698 $view->nodes_per_page
= intval($view->nodes_per_page
);
1699 $view->nodes_per_block
= intval($view->nodes_per_block
);
1700 $view->menu_tab_weight
= intval($view->menu_tab_weight
);
1702 // re-order things as they were ordered on the form.
1703 foreach (array('field', 'argument', 'filter', 'sort', 'exposed_filter') as
$section) {
1704 views_ui_reorder($view->$section);
1707 _views_save_view($view);
1710 drupal_set_message(t('View successfully saved.'));
1713 drupal_set_message(t('View successfully added.'));
1716 return 'admin/build/views';
1720 * Validate a view with a type: list.
1722 function views_ui_plugin_validate_list($type, $view, $form) {
1723 // list (and table) modes require there to be at least 1 field active.
1724 if (is_array($view['field'])) {
1725 $fields = array_filter(array_keys($view['field']), 'is_numeric');
1728 form_error($form["$type-info"][$type .
'_type'], t('List and Table types require at least one field.'));
1731 if (isset($view['field']['count'])) {
1732 $defaultsort = false
;
1733 for ($i = 0; $i < $view['field']['count']; $i++) {
1734 if ($view['field'][$i]['defaultsort']) {
1736 form_error($form['field'][$i]['defaultsort'], t('You can only set on Default Sort on one field.'));
1739 $defaultsort = true
;
1746 * Validate a view with type: table.
1748 function views_ui_plugin_validate_table($type, $view, $form) {
1749 // pass thru to list cause for now they're the same.
1750 return views_ui_plugin_validate_list($type, $view, $form);
1752 // ---------------------------------------------------------------------------
1753 // View creator tool
1758 function views_tf($val) {
1759 return ($val ?
'TRUE' : 'FALSE');
1763 * This function creates view code for a view.
1765 function views_create_view_code($vid) {
1766 $view = _views_load_view($vid);
1768 return t("View '%vid' not found.", array('%vid' => $vid));
1771 $requires = array();
1773 $output = " \$view = new stdClass();\n";
1774 $output .
= " \$view->name = " .
var_export($view->name
, true
) .
";\n";
1775 $output .
= " \$view->description = " .
var_export($view->description
, true
) .
";\n";
1776 $output .
= " \$view->access = " .
var_export($view->access
, true
) .
";\n";
1777 if (user_access('use PHP for block visibility')) {
1778 $output .
= " \$view->view_args_php = " .
var_export($view->view_args_php
, true
) .
";\n";
1782 $output .
= " \$view->page = " .
views_tf($view->page
) .
";\n";
1783 $output .
= " \$view->page_title = " .
var_export($view->page_title
, true
) .
";\n";
1784 $output .
= " \$view->page_header = " .
var_export($view->page_header
, true
) .
";\n";
1785 $output .
= " \$view->page_header_format = " .
var_export($view->page_header_format
, true
) .
";\n";
1786 $output .
= " \$view->page_footer = " .
var_export($view->page_footer
, true
) .
";\n";
1787 $output .
= " \$view->page_footer_format = " .
var_export($view->page_footer_format
, true
) .
";\n";
1788 $output .
= " \$view->page_empty = " .
var_export($view->page_empty
, true
) .
";\n";
1789 $output .
= " \$view->page_empty_format = " .
var_export($view->page_empty_format
, true
) .
";\n";
1790 $output .
= " \$view->page_type = " .
var_export($view->page_type
, true
) .
";\n";
1791 $output .
= " \$view->url = " .
var_export($view->url
, true
) .
";\n";
1792 $output .
= " \$view->use_pager = " .
views_tf($view->use_pager
) .
";\n";
1793 $output .
= " \$view->nodes_per_page = " .
var_export($view->nodes_per_page
, true
) .
";\n";
1795 $output .
= " \$view->menu = " .
views_tf($view->menu
) .
";\n";
1796 $output .
= " \$view->menu_title = " .
var_export($view->menu_title
, true
) .
";\n";
1797 $output .
= " \$view->menu_tab = " .
views_tf($view->menu_tab
) .
";\n";
1798 $output .
= " \$view->menu_tab_default = " .
views_tf($view->menu_tab_default
) .
";\n";
1799 $output .
= " \$view->menu_tab_weight = " .
var_export($view->menu_tab_weight
, true
) .
";\n";
1803 $output .
= " \$view->block = " .
views_tf($view->block
) .
";\n";
1804 $output .
= " \$view->block_title = " .
var_export($view->block_title
, true
) .
";\n";
1805 $output .
= " \$view->block_header = " .
var_export($view->block_header
, true
) .
";\n";
1806 $output .
= " \$view->block_header_format = " .
var_export($view->block_header_format
, true
) .
";\n";
1807 $output .
= " \$view->block_footer = " .
var_export($view->block_footer
, true
) .
";\n";
1808 $output .
= " \$view->block_footer_format = " .
var_export($view->block_footer_format
, true
) .
";\n";
1809 $output .
= " \$view->block_empty = " .
var_export($view->block_empty
, true
) .
";\n";
1810 $output .
= " \$view->block_empty_format = " .
var_export($view->block_empty_format
, true
) .
";\n";
1811 $output .
= " \$view->block_type = " .
var_export($view->block_type
, true
) .
";\n";
1812 $output .
= " \$view->nodes_per_block = " .
var_export($view->nodes_per_block
, true
) .
";\n";
1813 $output .
= " \$view->block_more = " .
views_tf($view->block_more
) .
";\n";
1814 $output .
= " \$view->block_use_page_header = " .
views_tf($view->block_use_page_header
) .
";\n";
1815 $output .
= " \$view->block_use_page_footer = " .
views_tf($view->block_use_page_footer
) .
";\n";
1816 $output .
= " \$view->block_use_page_empty = " .
views_tf($view->block_use_page_empty
) .
";\n";
1819 $output .
= " \$view->sort = array (\n";
1820 foreach ($view->sort as
$sort) {
1821 $output .
= " array (\n";
1822 $fieldbits = explode('.', $sort['field']);
1823 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1824 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1825 $output .
= " 'sortorder' => " .
var_export($sort['sortorder'], true
) .
",\n";
1826 $output .
= " 'options' => " .
var_export($sort['options'], true
) .
",\n";
1828 $requires[$fieldbits[0]] = 1;
1832 $output .
= " \$view->argument = array (\n";
1833 foreach ($view->argument as
$argument) {
1834 $output .
= " array (\n";
1835 $output .
= " 'type' => " .
var_export($argument['type'], true
) .
",\n";
1836 $output .
= " 'argdefault' => " .
var_export($argument['argdefault'], true
) .
",\n";
1837 $output .
= " 'title' => " .
var_export($argument['title'], true
) .
",\n";
1838 $output .
= " 'options' => " .
var_export($argument['options'], true
) .
",\n";
1839 $output .
= " 'wildcard' => " .
var_export($argument['wildcard'], true
) .
",\n";
1840 $output .
= " 'wildcard_substitution' => " .
var_export($argument['wildcard_substitution'], true
) .
",\n";
1845 $output .
= " \$view->field = array (\n";
1846 foreach ($view->field as
$field) {
1847 $output .
= " array (\n";
1848 $output .
= " 'tablename' => " .
var_export($field['tablename'], true
) .
",\n";
1849 $output .
= " 'field' => " .
var_export($field['field'], true
) .
",\n";
1850 $output .
= " 'label' => " .
var_export($field['label'], true
) .
",\n";
1851 if ($field['handler']) {
1852 $output .
= " 'handler' => " .
var_export($field['handler'], true
) .
",\n";
1854 if ($field['sortable']) {
1855 $output .
= " 'sortable' => " .
var_export($field['sortable'], true
) .
",\n";
1857 if ($field['defaultsort']) {
1858 $output .
= " 'defaultsort' => " .
var_export($field['defaultsort'], true
) .
",\n";
1860 if ($field['options']) {
1861 $output .
= " 'options' => " .
var_export($field['options'], true
) .
",\n";
1864 $requires[$field['tablename']] = 1;
1868 $output .
= " \$view->filter = array (\n";
1869 foreach ($view->filter as
$filter) {
1870 $output .
= " array (\n";
1871 $fieldbits = explode('.', $filter['field']);
1872 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1873 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1874 $output .
= " 'operator' => " .
var_export($filter['operator'], true
) .
",\n";
1875 $output .
= " 'options' => " .
var_export($filter['options'], true
) .
",\n";
1876 $output .
= " 'value' => " .
var_export($filter['value'], true
) .
",\n";
1878 $requires[$fieldbits[0]] = 1;
1882 $output .
= " \$view->exposed_filter = array (\n";
1883 foreach ($view->exposed_filter as
$filter) {
1884 $output .
= " array (\n";
1885 $fieldbits = explode('.', $filter['field']);
1886 $output .
= " 'tablename' => " .
var_export($fieldbits[0], true
) .
",\n";
1887 $output .
= " 'field' => " .
var_export($fieldbits[1], true
) .
",\n";
1888 $output .
= " 'label' => " .
var_export($filter['label'], true
) .
",\n";
1889 $output .
= " 'optional' => " .
var_export($filter['optional'], true
) .
",\n";
1890 $output .
= " 'is_default' => " .
var_export($filter['is_default'], true
) .
",\n";
1891 $output .
= " 'operator' => " .
var_export($filter['operator'], true
) .
",\n";
1892 $output .
= " 'single' => " .
var_export($filter['single'], true
) .
",\n";
1894 $requires[$fieldbits[0]] = 1;
1897 $output .
= " \$view->requires = array(" .
implode(', ', array_keys($requires)) .
");\n";
1898 $output .
= " \$views[\$view->name] = \$view;\n";