You are here

block.module in Drupal 5

Same filename and directory in other branches
  1. 6 modules/block/block.module
  2. 7 modules/block/block.module

Controls the boxes that are displayed around the main content.

File

modules/block/block.module
View source
<?php

/**
 * @file
 * Controls the boxes that are displayed around the main content.
 */

/**
 * Denotes that a block is not enabled in any region and should not
 * be shown.
 */
define('BLOCK_REGION_NONE', -1);

/**
 * Implementation of hook_help().
 */
function block_help($section) {
  switch ($section) {
    case 'admin/help#block':
      $output = '<p>' . t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') . '</p>';
      $output .= '<p>' . t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') . '</p>';
      $output .= '<p>' . t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region; heavier ones "sink".') . '</p>';
      $output .= t("<p>A block's visibility depends on:</p>\n<ul>\n<li>Its region placement. Blocks with no region assigned to them are never shown.</li>\n<li>Its throttle checkbox when throttle module is enabled. Throttled blocks are hidden during high server loads.</li>\n<li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>\n<li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>\n<li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>\n<li>Its user-role visibility settings. Administrators can choose to let blocks be visible only for certain user roles.</li>\n<li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>\n</ul>\n");
      $output .= '<h3>' . t('Module blocks') . '</h3>';
      $output .= '<p>' . t('Some modules generate blocks that become available when the modules are enabled. These blocks can be administered via the <a href="@admin-block">blocks administration page</a>.</p>', array(
        '@admin-block' => url('admin/build/block'),
      )) . '</p>';
      $output .= '<h3>' . t('Administrator defined blocks') . '</h3>';
      $output .= '<p>' . t('Administrators can also define custom blocks. These blocks consist of a title, a description, and a body which can be as long as you wish. Block content can be in any of the input formats supported for other content.') . '</p>';
      $output .= '<p>' . t('For more information please read the configuration and customization handbook <a href="@block">Block page</a>.', array(
        '@block' => 'http://drupal.org/handbook/modules/block/',
      )) . '</p>';
      return $output;
    case 'admin/build/block':
      return t('<p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
<p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar).  Highlighted labels on this page show the regions into which blocks can be rendered. You can specify where within a region a block will appear by adjusting its weight.</p>
<p>If you want certain blocks to disable themselves temporarily during high server loads, check the "Throttle" box. You can configure the auto-throttle on the <a href="@throttle">throttle configuration page</a> after having enabled the throttle module.</p>
<p>You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the "configure" link for each block.</p>', array(
        '@throttle' => url('admin/settings/throttle'),
      ));
    case 'admin/build/block/add':
      return '<p>' . t('Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="@overview">blocks</a>. The description is used in the "block" column on the <a href="@overview">blocks</a> page.', array(
        '@overview' => url('admin/build/block'),
      )) . '</p>';
  }
}

/**
 * Implementation of hook_perm().
 */
function block_perm() {
  return array(
    'administer blocks',
    'use PHP for block visibility',
  );
}

/**
 * Implementation of hook_menu().
 */
function block_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/build/block',
      'title' => t('Blocks'),
      'access' => user_access('administer blocks'),
      'description' => t('Configure what block content appears in your site\'s sidebars and other regions.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'block_admin_display',
      ),
    );
    $items[] = array(
      'path' => 'admin/build/block/list',
      'title' => t('List'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'admin/build/block/configure',
      'title' => t('Configure block'),
      'access' => user_access('administer blocks'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'block_admin_configure',
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/block/delete',
      'title' => t('Delete block'),
      'access' => user_access('administer blocks'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'block_box_delete',
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/block/add',
      'title' => t('Add block'),
      'access' => user_access('administer blocks'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'block_box_form',
      ),
      'type' => MENU_LOCAL_TASK,
    );
    $default = variable_get('theme_default', 'garland');
    foreach (list_themes() as $key => $theme) {
      $items[] = array(
        'path' => 'admin/build/block/list/' . $key,
        'title' => t('!key settings', array(
          '!key' => $key,
        )),
        'callback' => 'drupal_get_form',
        'callback arguments' => array(
          'block_admin_display',
          $key,
        ),
        'access' => user_access('administer blocks'),
        'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
        'weight' => $key == $default ? -10 : 0,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_block().
 *
 * Generates the administrator-defined blocks for display.
 */
function block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks = array();
      $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info');
      while ($block = db_fetch_object($result)) {
        $blocks[$block->bid]['info'] = $block->info;
      }
      return $blocks;
    case 'configure':
      $box = block_box_get($delta);
      if (filter_access($box['format'])) {
        return block_box_form($box);
      }
      break;
    case 'save':
      block_box_save($edit, $delta);
      break;
    case 'view':
      $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta));
      $data['content'] = check_markup($block->body, $block->format, FALSE);
      return $data;
  }
}

/**
 * Update the 'blocks' DB table with the blocks currently exported by modules.
 *
 * @return
 *   Blocks currently exported by modules.
 */
function _block_rehash() {
  global $theme_key;
  init_theme();
  $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }
  $blocks = array();
  foreach (module_list() as $module) {
    $module_blocks = module_invoke($module, 'block', 'list');
    if ($module_blocks) {
      foreach ($module_blocks as $delta => $block) {
        $block['module'] = $module;
        $block['delta'] = $delta;

        // If previously written to database, load values.
        if ($old_blocks[$module][$delta]) {
          $block['status'] = $old_blocks[$module][$delta]->status;
          $block['weight'] = $old_blocks[$module][$delta]->weight;
          $block['region'] = $old_blocks[$module][$delta]->region;
          $block['visibility'] = $old_blocks[$module][$delta]->visibility;
          $block['pages'] = $old_blocks[$module][$delta]->pages;
          $block['custom'] = $old_blocks[$module][$delta]->custom;
          $block['throttle'] = $old_blocks[$module][$delta]->throttle;
          $block['title'] = $old_blocks[$module][$delta]->title;
        }
        else {
          $properties = array(
            'status' => 0,
            'weight' => 0,
            'region' => 'left',
            'pages' => '',
            'custom' => 0,
            'title' => '',
          );
          foreach ($properties as $property => $default) {
            if (!isset($block[$property])) {
              $block[$property] = $default;
            }
          }
        }
        $blocks[] = $block;
      }
    }
  }
  db_lock_table('blocks');

  // Remove all blocks from table.
  db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);

  // Reinsert new set of blocks into table.
  foreach ($blocks as $block) {
    db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title']);
  }
  db_unlock_tables();
  return $blocks;
}

/**
 * Generate main block administration form.
 */
function block_admin_display($theme = NULL) {
  global $theme_key, $custom_theme;

  // Add CSS
  drupal_add_css(drupal_get_path('module', 'block') . '/block.css', 'module', 'all', FALSE);

  // If non-default theme configuration has been selected, set the custom theme.
  if ($theme) {
    $custom_theme = $theme;
  }
  else {
    $custom_theme = variable_get('theme_default', 'garland');
  }
  init_theme();

  // Fetch and sort blocks
  $blocks = _block_rehash();
  usort($blocks, '_block_compare');
  $throttle = module_exists('throttle');
  $block_regions = array(
    BLOCK_REGION_NONE => '<' . t('none') . '>',
  ) + system_region_list($theme_key);

  // Build form tree
  $form['#action'] = arg(3) ? url('admin/build/block/list/' . $theme_key) : url('admin/build/block');
  $form['#tree'] = TRUE;
  foreach ($blocks as $i => $block) {
    $form[$i]['module'] = array(
      '#type' => 'value',
      '#value' => $block['module'],
    );
    $form[$i]['delta'] = array(
      '#type' => 'value',
      '#value' => $block['delta'],
    );
    $form[$i]['info'] = array(
      '#value' => check_plain($block['info']),
    );
    $form[$i]['theme'] = array(
      '#type' => 'hidden',
      '#value' => $theme_key,
    );
    $form[$i]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $block['weight'],
    );
    $form[$i]['region'] = array(
      '#type' => 'select',
      '#default_value' => $block['status'] ? isset($block['region']) ? $block['region'] : system_default_region($theme_key) : BLOCK_REGION_NONE,
      '#options' => $block_regions,
    );
    if ($throttle) {
      $form[$i]['throttle'] = array(
        '#type' => 'checkbox',
        '#default_value' => $block['throttle'],
      );
    }
    $form[$i]['configure'] = array(
      '#value' => l(t('configure'), 'admin/build/block/configure/' . $block['module'] . '/' . $block['delta']),
    );
    if ($block['module'] == 'block') {
      $form[$i]['delete'] = array(
        '#value' => l(t('delete'), 'admin/build/block/delete/' . $block['delta']),
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save blocks'),
  );
  return $form;
}

/**
 * Helper function for sorting blocks on admin/build/block.
 *
 * Active blocks are sorted by region, then by weight.
 * Disabled blocks are sorted by name.
 */
function _block_compare($a, $b) {
  $status = $b['status'] - $a['status'];

  // Separate enabled from disabled.
  if ($status) {
    return $status;
  }

  // Enabled blocks
  if ($a['status']) {
    $place = strcmp($a['region'], $b['region']);
    return $place ? $place : $a['weight'] - $b['weight'];
  }
  else {
    return strcmp($a['info'], $b['info']);
  }
}

/**
 * Process main block administration form submission.
 */
function block_admin_display_submit($form_id, $form_values) {
  foreach ($form_values as $block) {
    $block['status'] = $block['region'] != BLOCK_REGION_NONE;
    $block['region'] = $block['status'] ? $block['region'] : '';
    db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], isset($block['throttle']) ? $block['throttle'] : 0, $block['module'], $block['delta'], $block['theme']);
  }
  drupal_set_message(t('The block settings have been updated.'));
  cache_clear_all();
}

/**
 * Theme main block administration form submission.
 *
 * Note: the blocks are already sorted in the right order,
 * grouped by status, region and weight.
 */
function theme_block_admin_display($form) {
  global $theme_key;
  $throttle = module_exists('throttle');
  $block_regions = system_region_list($theme_key);

  // Highlight regions on page to provide visual reference.
  foreach ($block_regions as $key => $value) {
    drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
  }

  // Build rows
  $rows = array();
  $last_region = '';
  $last_status = 1;
  foreach (element_children($form) as $i) {
    $block =& $form[$i];

    // Only take form elements that are blocks.
    if (is_array($block['info'])) {

      // Fetch values
      $region = $block['region']['#default_value'];
      $status = $region != BLOCK_REGION_NONE;

      // Output region header
      if ($status && $region != $last_region) {
        $region_title = t('@region', array(
          '@region' => drupal_ucfirst($block_regions[$region]),
        ));
        $rows[] = array(
          array(
            'data' => $region_title,
            'class' => 'region',
            'colspan' => $throttle ? 6 : 5,
          ),
        );
        $last_region = $region;
      }
      elseif ($status != $last_status) {
        $rows[] = array(
          array(
            'data' => t('Disabled'),
            'class' => 'region',
            'colspan' => $throttle ? 6 : 5,
          ),
        );
        $last_status = $status;
      }

      // Generate block row
      $row = array(
        array(
          'data' => drupal_render($block['info']),
          'class' => 'block',
        ),
        drupal_render($block['region']) . drupal_render($block['theme']),
        drupal_render($block['weight']),
      );
      if ($throttle) {
        $row[] = drupal_render($block['throttle']);
      }
      $row[] = drupal_render($block['configure']);
      $row[] = $block['delete'] ? drupal_render($block['delete']) : '';
      $rows[] = $row;
    }
  }

  // Finish table
  $header = array(
    t('Block'),
    t('Region'),
    t('Weight'),
  );
  if ($throttle) {
    $header[] = t('Throttle');
  }
  $header[] = array(
    'data' => t('Operations'),
    'colspan' => 2,
  );
  $output = theme('table', $header, $rows, array(
    'id' => 'blocks',
  ));
  $output .= drupal_render($form);
  return $output;
}
function block_box_get($bid) {
  return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
}

/**
 * Menu callback; displays the block configuration form.
 */
function block_admin_configure($module = NULL, $delta = 0) {
  $form['module'] = array(
    '#type' => 'value',
    '#value' => $module,
  );
  $form['delta'] = array(
    '#type' => 'value',
    '#value' => $delta,
  );
  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
  $form['block_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Block specific settings'),
    '#collapsible' => TRUE,
  );
  $form['block_settings']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Block title'),
    '#maxlength' => 64,
    '#description' => $module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>&lt;none&gt;</em> to display no title, or leave blank to use the default block title.'),
    '#default_value' => $edit['title'],
    '#weight' => -18,
  );

  // Module-specific block configurations.
  if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
    foreach ($settings as $k => $v) {
      $form['block_settings'][$k] = $v;
    }
  }

  // Get the block subject for the page title.
  $info = module_invoke($module, 'block', 'list');
  drupal_set_title(t("'%name' block", array(
    '%name' => $info[$delta]['info'],
  )));

  // Standard block configurations.
  $form['user_vis_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('User specific visibility settings'),
    '#collapsible' => TRUE,
  );
  $form['user_vis_settings']['custom'] = array(
    '#type' => 'radios',
    '#title' => t('Custom visibility settings'),
    '#options' => array(
      t('Users cannot control whether or not they see this block.'),
      t('Show this block by default, but let individual users hide it.'),
      t('Hide this block by default but let individual users show it.'),
    ),
    '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
    '#default_value' => $edit['custom'],
  );

  // Role-based visibility settings
  $default_role_options = array();
  $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
  while ($role = db_fetch_object($result)) {
    $default_role_options[] = $role->rid;
  }
  $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
  $role_options = array();
  while ($role = db_fetch_object($result)) {
    $role_options[$role->rid] = $role->name;
  }
  $form['role_vis_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Role specific visibility settings'),
    '#collapsible' => TRUE,
  );
  $form['role_vis_settings']['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Show block for specific roles'),
    '#default_value' => $default_role_options,
    '#options' => $role_options,
    '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
  );
  $form['page_vis_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page specific visibility settings'),
    '#collapsible' => TRUE,
  );
  $access = user_access('use PHP for block visibility');
  if ($edit['visibility'] == 2 && !$access) {
    $form['page_vis_settings'] = array();
    $form['page_vis_settings']['visibility'] = array(
      '#type' => 'value',
      '#value' => 2,
    );
    $form['page_vis_settings']['pages'] = array(
      '#type' => 'value',
      '#value' => $edit['pages'],
    );
  }
  else {
    $options = array(
      t('Show on every page except the listed pages.'),
      t('Show on only the listed pages.'),
    );
    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    ));
    if ($access) {
      $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
      $description .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array(
        '%php' => '<?php ?>',
      ));
    }
    $form['page_vis_settings']['visibility'] = array(
      '#type' => 'radios',
      '#title' => t('Show block on specific pages'),
      '#options' => $options,
      '#default_value' => $edit['visibility'],
    );
    $form['page_vis_settings']['pages'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages'),
      '#default_value' => $edit['pages'],
      '#description' => $description,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save block'),
  );
  return $form;
}
function block_admin_configure_validate($form_id, $form_values) {
  if ($form_values['module'] == 'block') {
    if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
      form_set_error('info', t('Please ensure that each block description is unique.'));
    }
  }
}
function block_admin_configure_submit($form_id, $form_values) {
  if (!form_get_errors()) {
    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], trim($form_values['pages']), $form_values['custom'], $form_values['title'], $form_values['module'], $form_values['delta']);
    db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
    foreach (array_filter($form_values['roles']) as $rid) {
      db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $form_values['delta']);
    }
    module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
    drupal_set_message(t('The block configuration has been saved.'));
    cache_clear_all();
    return 'admin/build/block';
  }
}
function block_box_form_validate($form_id, $form_values) {
  if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
    form_set_error('info', t('Please ensure that each block description is unique.'));
  }
}
function block_box_form_submit($form_id, $form_values) {
  if (!form_get_errors()) {
    if (block_box_save($form_values)) {
      drupal_set_message(t('The block has been created.'));
      return 'admin/build/block';
    }
  }
}

/**
 * Menu callback; confirm deletion of custom blocks.
 */
function block_box_delete($bid = 0) {
  $box = block_box_get($bid);
  $form['info'] = array(
    '#type' => 'hidden',
    '#value' => $box['info'],
  );
  $form['bid'] = array(
    '#type' => 'hidden',
    '#value' => $bid,
  );
  return confirm_form($form, t('Are you sure you want to delete the block %name?', array(
    '%name' => $box['info'],
  )), 'admin/build/block', '', t('Delete'), t('Cancel'));
}

/**
 * Deletion of custom blocks.
 */
function block_box_delete_submit($form_id, $form_values) {
  db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
  db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = '%s'", $form_values['bid']);
  drupal_set_message(t('The block %name has been removed.', array(
    '%name' => $form_values['info'],
  )));
  cache_clear_all();
  return 'admin/build/block';
}
function block_box_form($edit = array()) {
  $form['info'] = array(
    '#type' => 'textfield',
    '#title' => t('Block description'),
    '#default_value' => $edit['info'],
    '#maxlength' => 64,
    '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array(
      '@overview' => url('admin/build/block'),
    )),
    '#required' => TRUE,
    '#weight' => -19,
  );
  $form['body_filter']['#weight'] = -17;
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Block body'),
    '#default_value' => $edit['body'],
    '#rows' => 15,
    '#description' => t('The content of the block as shown to the user.'),
    '#weight' => -17,
  );
  if (!isset($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
  }
  $form['body_filter']['format'] = filter_form($edit['format'], -16);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save block'),
  );
  return $form;
}
function block_box_save($edit, $delta = NULL) {
  if (!filter_access($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
  }
  if (isset($delta)) {
    db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
  }
  else {
    db_query("INSERT INTO {boxes} (body, info, format) VALUES  ('%s', '%s', %d)", $edit['body'], $edit['info'], $edit['format']);
  }
  return TRUE;
}

/**
 * Implementation of hook_user().
 *
 * Allow users to decide which custom blocks to display when they visit
 * the site.
 */
function block_user($type, $edit, &$account, $category = NULL) {
  switch ($type) {
    case 'form':
      if ($category == 'account') {
        $rids = array_keys($account->roles);
        $placeholders = implode(',', array_fill(0, count($rids), '%d'));
        $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN ({$placeholders}) OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids);
        $form['block'] = array(
          '#type' => 'fieldset',
          '#title' => t('Block configuration'),
          '#weight' => 3,
          '#collapsible' => TRUE,
          '#tree' => TRUE,
        );
        while ($block = db_fetch_object($result)) {
          $data = module_invoke($block->module, 'block', 'list');
          if ($data[$block->delta]['info']) {
            $return = TRUE;
            $form['block'][$block->module][$block->delta] = array(
              '#type' => 'checkbox',
              '#title' => check_plain($data[$block->delta]['info']),
              '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : $block->custom == 1,
            );
          }
        }
        if ($return) {
          return $form;
        }
      }
      break;
    case 'validate':
      if (!$edit['block']) {
        $edit['block'] = array();
      }
      return $edit;
  }
}

/**
 * Return all blocks in the specified region for the current user.
 *
 * @param $region
 *   The name of a region.
 *
 * @return
 *   An array of block objects, indexed with <i>module</i>_<i>delta</i>.
 *   If you are displaying your blocks in one or two sidebars, you may check
 *   whether this array is empty to see how many columns are going to be
 *   displayed.
 *
 * @todo
 *   Add a proper primary key (bid) to the blocks table so we don't have
 *   to mess around with this <i>module</i>_<i>delta</i> construct.
 *   Currently, the blocks table has no primary key defined!
 */
function block_list($region) {
  global $user, $theme_key;
  static $blocks = array();
  if (!count($blocks)) {
    $rids = array_keys($user->roles);
    $placeholders = implode(',', array_fill(0, count($rids), '%d'));
    $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN ({$placeholders}) OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", array_merge(array(
      $theme_key,
    ), $rids));
    while ($block = db_fetch_object($result)) {
      if (!isset($blocks[$block->region])) {
        $blocks[$block->region] = array();
      }

      // Use the user's block visibility setting, if necessary
      if ($block->custom != 0) {
        if ($user->uid && isset($user->block[$block->module][$block->delta])) {
          $enabled = $user->block[$block->module][$block->delta];
        }
        else {
          $enabled = $block->custom == 1;
        }
      }
      else {
        $enabled = TRUE;
      }

      // Match path if necessary
      if ($block->pages) {
        if ($block->visibility < 2) {
          $path = drupal_get_path_alias($_GET['q']);
          $regexp = '/^(' . preg_replace(array(
            '/(\\r\\n?|\\n)/',
            '/\\\\\\*/',
            '/(^|\\|)\\\\<front\\\\>($|\\|)/',
          ), array(
            '|',
            '.*',
            '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
          ), preg_quote($block->pages, '/')) . ')$/';

          // Compare with the internal and path alias (if any).
          $page_match = preg_match($regexp, $path);
          if ($path != $_GET['q']) {
            $page_match = $page_match || preg_match($regexp, $_GET['q']);
          }

          // When $block->visibility has a value of 0, the block is displayed on
          // all pages except those listed in $block->pages. When set to 1, it
          // is displayed only on those pages listed in $block->pages.
          $page_match = !($block->visibility xor $page_match);
        }
        else {
          $page_match = drupal_eval($block->pages);
        }
      }
      else {
        $page_match = TRUE;
      }
      $block->enabled = $enabled;
      $block->page_match = $page_match;
      $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
    }
  }

  // Create an empty array if there were no entries
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
  }
  foreach ($blocks[$region] as $key => $block) {

    // Render the block content if it has not been created already.
    if (!isset($block->content)) {

      // Erase the block from the static array - we'll put it back if it has content.
      unset($blocks[$region][$key]);
      if ($block->enabled && $block->page_match) {

        // Check the current throttle status and see if block should be displayed
        // based on server load.
        if (!($block->throttle && module_invoke('throttle', 'status') > 0)) {
          $array = module_invoke($block->module, 'block', 'view', $block->delta);
          if (isset($array) && is_array($array)) {
            foreach ($array as $k => $v) {
              $block->{$k} = $v;
            }
          }
        }
        if (isset($block->content) && $block->content) {

          // Override default block title if a custom display title is present.
          if ($block->title) {

            // Check plain here to allow module generated titles to keep any markup.
            $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
          }
          $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
        }
      }
    }
  }
  return $blocks[$region];
}

Functions

Namesort descending Description
block_admin_configure Menu callback; displays the block configuration form.
block_admin_configure_submit
block_admin_configure_validate
block_admin_display Generate main block administration form.
block_admin_display_submit Process main block administration form submission.
block_block Implementation of hook_block().
block_box_delete Menu callback; confirm deletion of custom blocks.
block_box_delete_submit Deletion of custom blocks.
block_box_form
block_box_form_submit
block_box_form_validate
block_box_get
block_box_save
block_help Implementation of hook_help().
block_list Return all blocks in the specified region for the current user.
block_menu Implementation of hook_menu().
block_perm Implementation of hook_perm().
block_user Implementation of hook_user().
theme_block_admin_display Theme main block administration form submission.
_block_compare Helper function for sorting blocks on admin/build/block.
_block_rehash Update the 'blocks' DB table with the blocks currently exported by modules.

Constants

Namesort descending Description
BLOCK_REGION_NONE Denotes that a block is not enabled in any region and should not be shown.