You are here

function user_dashboard_page_build in UserDashboard 7

Implements hook_page_build().

Display dashboard blocks in the main content region.

File

./user_dashboard.module, line 250
The User Dashboard module forks Drupal 7's awesome Dashboard module to provide an individual dashboard for each user on the site.

Code

function user_dashboard_page_build(&$page) {
  if (_user_dashboard_visible()) {
    $settings = array(
      'user_dashboard' => array(
        'set_default_blocks_access' => (int) user_access('set default user_dashboard blocks'),
        'default_blocks_callback' => url('admin/dashboard/user_dashboard/set_default'),
      ),
    );
    drupal_add_js($settings, array(
      'type' => 'setting',
    ));

    // Set weight = 1 to ensure that this gets added after the core dashboard's
    // JS.
    drupal_add_js(drupal_get_path('module', 'user_dashboard') . '/js/user_dashboard.js', array(
      'weight' => 1,
    ));
    global $theme_key, $user;
    $available_blocks = variable_get('user_dashboard_available_blocks', array());

    // Load user dashboard blocks.
    $query = db_select('user_dashboard_block', 'b');
    $result = $query
      ->fields('b')
      ->condition('b.theme', $theme_key)
      ->condition('b.status', 1)
      ->condition('b.uid', (int) arg(1))
      ->orderBy('b.region')
      ->orderBy('b.weight')
      ->orderBy('b.module')
      ->addTag('block_load')
      ->addTag('translatable')
      ->execute();
    $block_info = $result
      ->fetchAllAssoc('bid');

    // Allow modules to modify the block list.
    drupal_alter('block_list', $block_info);
    $user_blocks = array();
    foreach ($block_info as $b) {

      // Ensure that this is still a valid block, otherwise remove
      // it and alert the user.
      $data = db_select('block', 'b')
        ->fields('b')
        ->condition('theme', $b->theme)
        ->condition('module', $b->module)
        ->condition('delta', $b->delta)
        ->execute()
        ->fetchAssoc();
      if (!$data || !in_array($b->module . '_' . $b->delta, $available_blocks)) {
        drupal_set_message(t('Block !delta provided by module !module is no longer available and has been removed from your dashboard.', array(
          '!delta' => '<em>' . $b->delta . '</em>',
          '!module' => '<em>' . $b->module . '</em>',
        )), 'warning');
        db_delete('user_dashboard_block')
          ->condition('theme', $b->theme)
          ->condition('module', $b->module)
          ->condition('delta', $b->delta)
          ->condition('uid', $user->uid)
          ->execute();
      }
      else {
        $user_blocks[$b->region][$b->module . '_' . $b->delta] = $b;
      }
    }
    foreach ($user_blocks as $key => $r) {
      if ($list = _block_render_blocks($r)) {
        if (!isset($page[$key])) {
          $page[$key] = array();
        }
        $page[$key] += _block_get_renderable_array($list);
      }
    }
    $block_info = array();

    // Create a wrapper for the dashboard itself, then insert each dashboard
    // region into it.
    $page['content']['user_dashboard'] = array(
      '#theme_wrappers' => array(
        'user_dashboard',
      ),
    );
    foreach (_user_dashboard_regions() as $region) {

      // Insert regions even when they are empty, so that they will be
      // displayed when the dashboard is being configured.
      $page['content']['user_dashboard'][$region] = !empty($page[$region]) ? $page[$region] : array();
      $page['content']['user_dashboard'][$region]['#dashboard_region'] = $region;

      // Allow each dashboard region to be themed differently, or fall back on
      // the generic theme wrapper function for dashboard regions.
      $page['content']['user_dashboard'][$region]['#theme_wrappers'][] = array(
        $region,
        'user_dashboard_region',
      );

      // Allow modules to alter the region.
      drupal_alter('user_dashboard_region', $page['content']['user_dashboard'][$region]);
      unset($page[$region]);
      $blocks_found = array();
      foreach ($page['content']['user_dashboard'][$region] as $item) {
        if (isset($item['#theme_wrappers']) && is_array($item['#theme_wrappers']) && in_array('block', $item['#theme_wrappers'])) {

          // If this item is a block, ensure it has a subject.
          if (empty($item['#block']->subject)) {

            // Locally cache info data for the object for all blocks, in case
            // we find a block similarly missing title from the same module.
            if (!isset($block_info[$item['#block']->module])) {
              $block_info[$item['#block']->module] = module_invoke($item['#block']->module, 'block_info');
            }
            $item['#block']->subject = $block_info[$item['#block']->module][$item['#block']->delta]['info'];
          }
          $blocks_found[$item['#block']->module . '_' . $item['#block']->delta] = TRUE;
        }
      }

      // Find blocks which were not yet displayed on the page (were empty), and
      // add placeholder items in their place for rendering.
      $block_list = db_select('user_dashboard_block')
        ->condition('theme', $theme_key)
        ->condition('status', 1)
        ->condition('region', $region)
        ->condition('uid', (int) arg(1))
        ->fields('user_dashboard_block')
        ->orderBy('weight')
        ->execute();
      foreach ($block_list as $block) {
        if (!isset($blocks_found[$block->module . '_' . $block->delta])) {
          $block->enabled = $block->page_match = TRUE;
          $block->content = array(
            '#markup' => '<div class="dashboard-block-empty">(empty)</div>',
          );
          if (!isset($block_info[$block->module])) {
            $block_info[$block->module] = module_invoke($block->module, 'block_info');
          }
          $block->subject = t('@title', array(
            '@title' => $block_info[$block->module][$block->delta]['info'],
          ));
          $block_render = array(
            $block->module . '_' . $block->delta => $block,
          );
          $build = _block_get_renderable_array($block_render);
          $page['content']['user_dashboard'][$block->region][] = $build;
        }
      }
    }
  }
}