Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.945 diff -u -p -r1.945 common.inc --- includes/common.inc 29 Jul 2009 06:39:33 -0000 1.945 +++ includes/common.inc 4 Aug 2009 09:42:48 -0000 @@ -3813,6 +3813,18 @@ function drupal_render(&$elements) { $elements['#theme'] = 'markup'; } + // If there is are action links associated with this element but the access + // property has not been set, use the menu callback to determine whether or + // not it is editable by the current user. + if (isset($elements['#actions'])) { + foreach ($elements['#actions'] as $key => $data) { + if (!isset($data['access']) && drupal_function_exists('menu_get_item')) { + $item = menu_get_item($data['href']); + $elements['#actions'][$key]['access'] = !empty($item['access']); + } + } + } + // Make any final changes to the element before it is rendered. This means // that the $element or the children can be altered or corrected before the // element is rendered into the final text. @@ -4118,6 +4130,9 @@ function drupal_common_theme() { 'links' => array( 'arguments' => array('links' => NULL, 'attributes' => array('class' => 'links')), ), + 'action_links' => array( + 'arguments' => array('link' => NULL, 'element' => NULL), + ), 'image' => array( 'arguments' => array('path' => NULL, 'alt' => '', 'title' => '', 'attributes' => array(), 'getsize' => TRUE), ), Index: includes/menu.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/menu.inc,v retrieving revision 1.330 diff -u -p -r1.330 menu.inc --- includes/menu.inc 20 Jul 2009 01:28:16 -0000 1.330 +++ includes/menu.inc 29 Jul 2009 10:51:19 -0000 @@ -766,7 +766,7 @@ function menu_get_object($type = 'node', } /** - * Render a menu tree based on the current path. + * Returns a menu tree based on the current path. * * The tree is expanded based on the current path and dynamic paths are also * changed according to the defined to_arg functions (for example the 'My account' @@ -775,28 +775,30 @@ function menu_get_object($type = 'node', * @param $menu_name * The name of the menu. * @return - * The rendered HTML of that menu on the current page. + * An array representing the menu on the current page, suitable for + * rendering with drupal_render(). */ function menu_tree($menu_name) { - $menu_output = &drupal_static(__FUNCTION__, array()); + $menu_tree = &drupal_static(__FUNCTION__, array()); - if (!isset($menu_output[$menu_name])) { + if (!isset($menu_tree[$menu_name])) { $tree = menu_tree_page_data($menu_name); - $menu_output[$menu_name] = menu_tree_output($tree); + $menu_tree[$menu_name] = menu_tree_unrendered($tree); } - return $menu_output[$menu_name]; + return $menu_tree[$menu_name]; } /** - * Returns a rendered menu tree. + * Returns an array representing a menu, suitable for rendering. * * @param $tree * A data structure representing the tree as returned from menu_tree_data. * @return - * The rendered HTML of that data structure. + * A structured array representing the menu, suitable for rendering with + * drupal_render(). */ -function menu_tree_output($tree) { - $output = ''; +function menu_tree_unrendered($tree) { + $content = ''; $items = array(); // Pull out just the menu items we are going to render so that we @@ -819,13 +821,28 @@ function menu_tree_output($tree) { $extra_class = implode(' ', $extra_class); $link = theme('menu_item_link', $data['link']); if ($data['below']) { - $output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class); + $content .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class); } else { - $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class); + $content .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class); } } - return $output ? theme('menu_tree', $output) : ''; + return array( + '#theme' => 'menu_tree', + '#menu' => $content, + ); +} + +/** + * Returns a rendered menu tree. + * + * @param $tree + * A data structure representing the tree as returned from menu_tree_data. + * @return + * The rendered HTML of that data structure. + */ +function menu_tree_output($tree) { + return drupal_render(menu_tree_unrendered($tree)); } /** @@ -1260,7 +1277,7 @@ function theme_menu_item_link($link) { * @ingroup themeable */ function theme_menu_tree($tree) { - return '
'; + return !empty($tree['#menu']) ? ' ' : ''; } /** Index: includes/theme.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/theme.inc,v retrieving revision 1.500 diff -u -p -r1.500 theme.inc --- includes/theme.inc 27 Jul 2009 18:38:35 -0000 1.500 +++ includes/theme.inc 4 Aug 2009 12:26:27 -0000 @@ -740,16 +740,22 @@ function theme() { } else { // The theme call is a template. - $variables = array( - 'template_files' => array() - ); + $variables = array(); if (!empty($info['arguments'])) { + // Populate the variables with arguments passed to the theme function. + // Note that the first argument may be treated specially by template + // preprocess functions, so it must go into the variables array before + // anything else does. $count = 0; foreach ($info['arguments'] as $name => $default) { $variables[$name] = isset($args[$count]) ? $args[$count] : $default; $count++; } } + // Add an empty array of template files as a default; preprocess functions + // will be able to modify this. We include it last, as per the comment + // above. + $variables += array('template_files' => array()); // default render function and extension. $render_function = 'theme_render_template'; @@ -778,7 +784,12 @@ function theme() { foreach (array('preprocess functions', 'process functions') as $template_phase) { foreach ($info[$template_phase] as $template_function) { if (drupal_function_exists($template_function)) { - call_user_func_array($template_function, $args); + $result = call_user_func_array($template_function, $args); + // Allow process functions to abort rendering the item by returning + // FALSE. + if ($result === FALSE) { + return ''; + } } } } @@ -1320,6 +1331,32 @@ function theme_links($links, $attributes } /** + * Return themed action links for an element on a page. + * + * @param $links + * An array representing the action links to be themed, in the format + * expected by theme_links(); e.g., the array can have keys including + * 'title', 'href', 'html', etc. + * @param $element + * An array representing the editable page element. + * @return + * A themed HTML string representing the edit link. + * + * @see theme_links() + */ +function theme_action_links($links, $element) { + // Construct a CSS class which associates this edit link with the region on + // the page that it edits. + $links_array = array(); + foreach ($links as $key => $link) { + $class = "actions-link actions-$key-link actions-at-" . str_replace('/', '-', $link['href']); + $links_array[$class] = $link; + } + drupal_add_js(!empty($_REQUEST['edit_mode']) ? 'misc/actions.mode.js' : 'misc/action-links.js'); + return theme('links', $links_array, array('class' => 'links action-links')); +} + +/** * Return a themed image. * * @param $path @@ -1837,6 +1874,72 @@ function template_preprocess(&$variables // Initialize html class attribute for the current hook. $variables['classes_array'] = array($hook); + // If the first item passed to the theme function is an editable page + // element which the current user has access to, populate useful variables + // for constructing edit links and other editable markup. + $element = reset($variables); + $variables['has_actions'] = FALSE; + if (is_array($element) && isset($element['#actions'])) { + foreach ($element['#actions'] as $key => $data) { + $variables['has_actions'] = $variables['has_actions'] || !empty($data['access']); + } + } + // Do not show edit links when the user is already on the page that is being + // linked to. + $show_action_links = FALSE; + if ($variables['has_actions'] && isset($_GET['q'])) { + foreach ($element['#actions'] as $key => $data) { + $show_action_links = $show_action_links || ($data['href'] != $_GET['q']); + } + } + // Add appropriate CSS classes that define this element as editable and + // associate it with its edit link. + if ($variables['has_actions']) { + $variables['classes_array'][] = 'actions-enabled-region'; + foreach ($element['#actions'] as $key => $data) { + $variables['classes_array'][] = 'actions-enabled-at-' . str_replace('/', '-', $data['href']); + } + } + if ($show_action_links) { + $available_icons = module_invoke_all('action_links_icons'); + $text_items = array(); + $info_items = array(); + foreach ($element['#actions'] as $key => $data) { + if (isset($available_icons[$key])) { + $title = isset($data['title']) ? $data['title'] : ''; + $text_items[$key] = theme('image', $available_icons[$key]['path'], $available_icons[$key]['alt'], $title, array('class' => 'action-link-icon action-link-icon-' . $key)); + $info_items[$key] = array( + 'title' => $text_items[$key], + 'href' => $data['href'], + // HTML should always be on for icons, which are images. + 'html' => TRUE, + // Refer users back to the current page after they have completed + // their edits. + 'query' => drupal_get_destination(), + ); + } + else { + $check_plain = isset($data['attributes']['html']) ? !$data['attributes']['html'] : TRUE; + $text_items[$key] = $check_plain ? check_plain($data['title']) : $data['title']; + $info_items[$key] = array( + 'title' => $text_items[$key], + 'href' => $data['href'], + 'html' => isset($data['html']) ? $data['html'] : FALSE, + // Refer users back to the current page after they have completed + // their edits. + 'query' => drupal_get_destination(), + ); + } + } + $variables['action_links_text'] = $text_items; + $variables['action_links_info'] = $info_items; + } + else { + $variables['action_links_text'] = array(); + $variables['action_links_info'] = array(); + } + $variables['action_links'] = $show_action_links ? theme('action_links', $variables['action_links_info'], $element) : ''; + // Set default variables that depend on the database. $variables['is_admin'] = FALSE; $variables['is_front'] = FALSE; @@ -1927,7 +2030,10 @@ function template_preprocess_page(&$vari $variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array(); $variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array(); $variables['search_box'] = (theme_get_setting('toggle_search') ? drupal_render(drupal_get_form('search_theme_form')) : ''); - $variables['site_name'] = (theme_get_setting('toggle_name') ? filter_xss_admin(variable_get('site_name', 'Drupal')) : ''); + // TODO: Remove. This is just a quick hack to give a rough feel for the + // "edit mode" in the D7UX proposal. + $edit_mode_link = !empty($_REQUEST['edit_mode']) ? l(t('global edit mode: On'), $_GET['q']) : l(t('global edit mode: Off'), $_GET['q'], array('query' => 'edit_mode=1')); + $variables['site_name'] = (theme_get_setting('toggle_name') ? t('!site_name (!edit_mode)', array('!site_name' => filter_xss_admin(variable_get('site_name', 'Drupal')), '!edit_mode' => $edit_mode_link)) : ''); $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? filter_xss_admin(variable_get('site_slogan', '')) : ''); $variables['tabs'] = theme('menu_local_tasks'); $variables['title'] = drupal_get_title(); Index: misc/action-links.js =================================================================== RCS file: misc/action-links.js diff -N misc/action-links.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/action-links.js 4 Aug 2009 12:04:42 -0000 @@ -0,0 +1,27 @@ +// $Id$ +(function ($) { + +/** + * Highlights the actions region when hovering over an edit link. + */ +Drupal.behaviors.highlightEditableRegion = { + attach: function (context) { + var addHighlight = function () { + var matches = $(this).attr('class').match(/[^ ]*-at-(\S+)/); + if (matches) { + var class = '.actions-enabled-at-' + matches[1]; + $(class).addClass('active-actions-region'); + $(this).addClass('active-actions-link'); + } + }; + + var removeHighlight = function () { + $('.active-actions-region').removeClass('active-actions-region'); + $('.active-actions-link').removeClass('active-actions-link'); + }; + + $('.action-links li').hover(addHighlight, removeHighlight); + } +}; + +})(jQuery); Index: misc/actions.mode.js =================================================================== RCS file: misc/actions.mode.js diff -N misc/actions.mode.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/actions.mode.js 4 Aug 2009 12:19:41 -0000 @@ -0,0 +1,42 @@ +// $Id$ +(function ($) { + +/** + * Allows any actions region on the page to be clicked on and edited. + */ +Drupal.behaviors.enableEditMode = { + attach: function (context) { + /** + * Show the action link within an actions region, and highlight the region. + */ + var showEditable = function () { + hideEditable(); // Doesn't quite work right. + $(this).addClass('active-actions-region'); + var matches = $(this).attr('class').match(/actions-enabled-at-(\S+)/g); + if (matches) { + for (i in matches) { + var actionClass = '.actions-at-' + matches[i].replace(/actions-enabled-at-/, ''); + $(actionClass).show(); + $(actionClass).addClass('active-actions-link'); + } + } + }; + + /** + * Hide the action link within an actions region, and remove highlighting. + */ + var hideEditable = function () { + $('.active-actions-region').removeClass('active-actions-region'); + $('.active-actions-link').hide(); + $('.active-actions-link').removeClass('active-actions-link'); + }; + + // Hide all action links. + $('.actions-link').hide(); + + // Highlight the actions region. + $('.actions-enabled-region').hover(showEditable, hideEditable); + } +}; + +})(jQuery); Index: modules/block/block.module =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.module,v retrieving revision 1.351 diff -u -p -r1.351 block.module --- modules/block/block.module 20 Jul 2009 18:51:32 -0000 1.351 +++ modules/block/block.module 4 Aug 2009 11:50:04 -0000 @@ -277,8 +277,20 @@ function block_get_blocks_by_region($reg $build[$key] += array( '#theme_wrapper' => 'block', '#block' => $block, + '#actions' => array( + 'configure' => array( + 'href' => 'admin/structure/block/configure/' . $block->module . '/' . $block->delta, + 'title' => t('Configure this block'), + ), + ), '#weight' => ++$weight, ); + // Special exception for the main system content block: it's weird and + // confusing to show action links for it - what exactly is the user + // configuring? + if ($block->module == 'system' && $block->delta == 'main') { + unset($build[$key]['#actions']['configure']); + } } $build['#sorted'] = TRUE; } @@ -862,6 +874,22 @@ function block_flush_caches() { function template_preprocess_block(&$variables) { $block_counter = &drupal_static(__FUNCTION__, array()); $variables['block'] = $variables['elements']['#block']; + + // Create the $content variable that templates expect. If the block is + // empty, do not show it. + $variables['content'] = $variables['elements']['#children']; + if (empty($variables['content'])) { + // TODO: This is necessary to match the behavior in _block_render_blocks() + // when $block->content contains HTML output rather than a structured + // array. The inconsistency is ugly, though. It also means that the array + // of blocks per region inserted into the page via block_page_alter() can + // contain a larger number of blocks than actually will display on the + // page. (This part actually might not be bad, though; it means that other + // modules have a chance to insert content into these "empty" blocks and + // therefore make them appear after all, if they want to.) + return FALSE; + } + // All blocks get an independent counter for each region. if (!isset($block_counter[$variables['block']->region])) { $block_counter[$variables['block']->region] = 1; @@ -870,9 +898,6 @@ function template_preprocess_block(&$var $variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even'; $variables['block_id'] = $block_counter[$variables['block']->region]++; - // Create the $content variable that templates expect. - $variables['content'] = $variables['elements']['#children']; - $variables['classes_array'][] = 'block-' . $variables['block']->module; $variables['template_files'][] = 'block-' . $variables['block']->region; Index: modules/block/block.tpl.php =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.tpl.php,v retrieving revision 1.3 diff -u -p -r1.3 block.tpl.php --- modules/block/block.tpl.php 12 Jun 2009 09:02:55 -0000 1.3 +++ modules/block/block.tpl.php 4 Aug 2009 11:19:00 -0000 @@ -30,12 +30,26 @@ * - $logged_in: Flags true when the current user is a logged-in member. * - $is_admin: Flags true when the current user is an administrator. * + * Action variables: + * - $has_actions: TRUE when the block is editable by the current user. + * - $action_links: Already-themed link(s) for actions that may be taken on the + * block; may be empty. + * - $action_links_text: An array of captions for the links to take action on + * the block; may be empty. + * - $action_links_info: An array of information describing the links to take + * action on the block; may be empty. + * * @see template_preprocess() * @see template_preprocess_block() * @see template_process() */ ?>