Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.961 diff -u -r1.961 common.inc --- includes/common.inc 15 Aug 2009 06:20:20 -0000 1.961 +++ includes/common.inc 15 Aug 2009 11:16:16 -0000 @@ -3940,6 +3940,18 @@ $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. @@ -4247,6 +4259,9 @@ '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.333 diff -u -r1.333 menu.inc --- includes/menu.inc 11 Aug 2009 17:26:33 -0000 1.333 +++ includes/menu.inc 15 Aug 2009 11:12:58 -0000 @@ -766,7 +766,7 @@ } /** - * 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 @@ * @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 @@ $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)); } /** @@ -1258,7 +1275,7 @@ * @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.504 diff -u -r1.504 theme.inc --- includes/theme.inc 13 Aug 2009 03:05:54 -0000 1.504 +++ includes/theme.inc 15 Aug 2009 11:12:55 -0000 @@ -740,16 +740,22 @@ } 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 @@ 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 ''; + } } } } @@ -1322,6 +1333,36 @@ } /** + * 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) { + if (empty($_SESSION['edit_mode'])) { + return; + } + // 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('misc/actions.js'); + drupal_add_css('misc/actions.css'); + return theme('links', $links_array, array('class' => 'action-links')); +} + +/** * Return a themed image. * * @param $path @@ -1839,6 +1880,72 @@ // 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; Index: modules/block/block.module =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.module,v retrieving revision 1.359 diff -u -r1.359 block.module --- modules/block/block.module 15 Aug 2009 06:50:29 -0000 1.359 +++ modules/block/block.module 15 Aug 2009 11:27:46 -0000 @@ -278,8 +278,20 @@ unset($block->content); $build[$key] += array( '#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[$key]['#theme_wrappers'][] ='block'; } $build['#sorted'] = TRUE; @@ -833,6 +845,22 @@ 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; @@ -841,9 +869,6 @@ $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 -r1.3 block.tpl.php --- modules/block/block.tpl.php 12 Jun 2009 09:02:55 -0000 1.3 +++ modules/block/block.tpl.php 15 Aug 2009 11:12:51 -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() */ ?>