block.admin.inc 23.7 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
// $Id$

/**
 * @file
 * Admin page callbacks for the block module.
 */

9 10 11 12
/**
 * Menu callback for admin/structure/block/demo.
 */
function block_admin_demo($theme = NULL) {
13
  drupal_add_css(drupal_get_path('module', 'block') . '/block.css');
14 15 16
  return '';
}

17
/**
18
 * Menu callback for admin/structure/block.
19 20 21 22
 *
 * @param $theme
 *   The theme to display the administration page for. If not provided, defaults
 *   to the currently used theme.
23 24
 */
function block_admin_display($theme = NULL) {
25
  global $theme_key;
26

27 28 29 30 31 32 33
  drupal_theme_initialize();

  if (!isset($theme)) {
    // If theme is not specifically set, rehash for the current theme.
    $theme = $theme_key;
  }

34
  // Fetch and sort blocks.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  $blocks = block_admin_display_prepare_blocks($theme);

  return drupal_get_form('block_admin_display_form', $blocks, $theme);
}

/**
 * Prepares a list of blocks for display on the blocks administration page.
 *
 * @param $theme
 *   The machine-readable name of the theme whose blocks should be returned.
 *
 * @return
 *   An array of blocks, as returned by _block_rehash(), sorted by region in
 *   preparation for display on the blocks administration page.
 *
 * @see block_admin_display_form()
 */
function block_admin_display_prepare_blocks($theme) {
53 54 55
  $blocks = _block_rehash($theme);
  $compare_theme = &drupal_static('_block_compare:theme');
  $compare_theme = $theme;
56
  usort($blocks, '_block_compare');
57
  return $blocks;
58 59
}

60
/**
61 62 63
 * Form builder for the main blocks administration form.
 *
 * @param $blocks
64
 *   An array of blocks, as returned by block_admin_display_prepare_blocks().
65 66
 * @param $theme
 *   A string representing the name of the theme to edit blocks for.
67 68 69 70 71 72 73 74
 * @param $block_regions
 *   (optional) An array of regions in which the blocks will be allowed to be
 *   placed. Defaults to all visible regions for the theme whose blocks are
 *   being configured. In all cases, a dummy region for disabled blocks will
 *   also be displayed.
 *
 * @return
 *   An array representing the form definition.
75 76
 *
 * @ingroup forms
77
 * @see block_admin_display_form_submit()
78
 */
79
function block_admin_display_form($form, &$form_state, $blocks, $theme, $block_regions = NULL) {
80

81
  drupal_add_css(drupal_get_path('module', 'block') . '/block.css');
82

83 84 85 86 87
  // Get a list of block regions if one was not provided.
  if (!isset($block_regions)) {
    $block_regions = system_region_list($theme, REGIONS_VISIBLE);
  }

88 89 90 91 92
  // Weights range from -delta to +delta, so delta should be at least half
  // of the amount of blocks present. This makes sure all blocks in the same
  // region get an unique weight.
  $weight_delta = round(count($blocks) / 2);

93
  // Build the form tree.
94 95 96 97 98 99
  $form['edited_theme'] = array(
    '#type' => 'value',
    '#value' => $theme,
  );
  $form['block_regions'] = array(
    '#type' => 'value',
100 101
    // Add a last region for disabled blocks.
    '#value' => $block_regions + array(BLOCK_REGION_NONE => BLOCK_REGION_NONE),
102 103
  );
  $form['blocks'] = array();
104
  $form['#tree'] = TRUE;
105

106
  foreach ($blocks as $i => $block) {
107
    $key = $block['module'] . '_' . $block['delta'];
108
    $form['blocks'][$key]['module'] = array(
109 110 111
      '#type' => 'value',
      '#value' => $block['module'],
    );
112
    $form['blocks'][$key]['delta'] = array(
113 114 115
      '#type' => 'value',
      '#value' => $block['delta'],
    );
116
    $form['blocks'][$key]['info'] = array(
117
      '#markup' => check_plain($block['info']),
118
    );
119
    $form['blocks'][$key]['theme'] = array(
120
      '#type' => 'hidden',
121
      '#value' => $theme,
122
    );
123
    $form['blocks'][$key]['weight'] = array(
124 125
      '#type' => 'weight',
      '#default_value' => $block['weight'],
126
      '#delta' => $weight_delta,
127
      '#title_display' => 'invisible',
128
      '#title' => t('Weight for @block block', array('@block' => $block['info'])),
129
    );
130
    $form['blocks'][$key]['region'] = array(
131
      '#type' => 'select',
132 133
      '#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL,
      '#empty_value' => BLOCK_REGION_NONE,
134
      '#title_display' => 'invisible',
135
      '#title' => t('Region for @block block', array('@block' => $block['info'])),
136 137
      '#options' => $block_regions,
    );
138
    $form['blocks'][$key]['configure'] = array(
139 140 141
      '#type' => 'link',
      '#title' => t('configure'),
      '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure',
142
    );
143
    if ($block['module'] == 'block') {
144
      $form['blocks'][$key]['delete'] = array(
145 146 147 148
        '#type' => 'link',
        '#title' => t('delete'),
        '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/delete',
     );
149 150
    }
  }
151 152 153 154
  // Do not allow disabling the main system content block when it is present.
  if (isset($form['blocks']['system_main']['region'])) {
    $form['blocks']['system_main']['region']['#required'] = TRUE;
  }
155

156 157
  $form['actions'] = array(
    '#tree' => FALSE,
158
    '#type' => 'actions',
159 160
  );
  $form['actions']['submit'] = array(
161 162 163
    '#type' => 'submit',
    '#value' => t('Save blocks'),
  );
164 165 166 167 168

  return $form;
}

/**
169 170 171
 * Form submission handler for the main blocks administration form.
 *
 * @see block_admin_display_form()
172
 */
173
function block_admin_display_form_submit($form, &$form_state) {
174 175
  $txn = db_transaction();

176
  foreach ($form_state['values']['blocks'] as $block) {
177
    $block['status'] = (int) ($block['region'] != BLOCK_REGION_NONE);
178
    $block['region'] = $block['status'] ? $block['region'] : '';
179 180 181 182 183 184 185 186 187 188
    db_update('block')
      ->fields(array(
        'status' => $block['status'],
        'weight' => $block['weight'],
        'region' => $block['region'],
      ))
      ->condition('module', $block['module'])
      ->condition('delta', $block['delta'])
      ->condition('theme', $block['theme'])
      ->execute();
189 190 191 192 193 194
  }
  drupal_set_message(t('The block settings have been updated.'));
  cache_clear_all();
}

/**
195
 * Helper function for sorting blocks on admin/structure/block.
196 197 198 199 200
 *
 * Active blocks are sorted by region, then by weight.
 * Disabled blocks are sorted by name.
 */
function _block_compare($a, $b) {
201
  global $theme_key;
202

203 204 205 206 207 208
  // Theme should be set before calling this function, or the current theme
  // is being used.
  $theme = &drupal_static(__FUNCTION__ . ':theme');
  if (!isset($theme)) {
    $theme = $theme_key;
  }
209

210
  $regions = &drupal_static(__FUNCTION__ . ':regions');
211 212
  // We need the region list to correctly order by region.
  if (!isset($regions)) {
213
    $regions = array_flip(array_keys(system_region_list($theme)));
214 215 216
    $regions[BLOCK_REGION_NONE] = count($regions);
  }

217
  // Separate enabled from disabled.
218
  $status = $b['status'] - $a['status'];
219 220 221
  if ($status) {
    return $status;
  }
222
  // Sort by region (in the order defined by theme .info file).
223
  if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
224
    return $place;
225
  }
226 227 228 229 230 231
  // Sort by weight, unless disabled.
  if ($a['region'] != BLOCK_REGION_NONE) {
    $weight = $a['weight'] - $b['weight'];
    if ($weight) {
      return $weight;
    }
232
  }
233 234
  // Sort by title.
  return strcmp($a['info'], $b['info']);
235 236 237
}

/**
238 239 240 241 242 243 244 245 246 247 248 249
 * Form builder for the block configuration form.
 *
 * Also used by block_add_block_form() for adding a new custom block.
 *
 * @param $module
 *   Name of the module that implements the block to be configured.
 * @param $delta
 *   Unique ID of the block within the context of $module.
 *
 * @see block_admin_configure_validate()
 * @see block_admin_configure_submit()
 * @ingroup forms
250
 */
251 252
function block_admin_configure($form, &$form_state, $module, $delta) {
  $block = block_load($module, $delta);
253 254
  $form['module'] = array(
    '#type' => 'value',
255
    '#value' => $block->module,
256 257 258
  );
  $form['delta'] = array(
    '#type' => 'value',
259
    '#value' => $block->delta,
260
  );
261

262 263 264 265 266 267 268
  // Get the block subject for the page title.
  $info = module_invoke($block->module, 'block_info');
  if (isset($info[$block->delta])) {
    drupal_set_title(t("'%name' block", array('%name' => $info[$block->delta]['info'])), PASS_THROUGH);
  }

  $form['settings']['title'] = array(
269 270 271
    '#type' => 'textfield',
    '#title' => t('Block title'),
    '#maxlength' => 64,
272
    '#description' => $block->module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>!placeholder</em> to display no title, or leave blank to use the default block title.', array('!placeholder' => '&lt;none&gt;')),
273
    '#default_value' => isset($block->title) ? $block->title : '',
274 275 276
    '#weight' => -18,
  );

277 278 279 280 281 282 283 284
  // Module-specific block configuration.
  if ($settings = module_invoke($block->module, 'block_configure', $block->delta)) {
    foreach ($settings as $k => $v) {
      $form['settings'][$k] = $v;
    }
  }

  // Region settings.
285 286 287
  $form['regions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Region settings'),
288 289
    '#collapsible' => FALSE,
    '#description' => t('Specify in which themes and regions this block is displayed.'),
290 291 292
    '#tree' => TRUE,
  );

293
  $theme_default = variable_get('theme_default', 'bartik');
294
  $admin_theme = variable_get('admin_theme');
295
  foreach (list_themes() as $key => $theme) {
296 297 298
    // Only display enabled themes
    if ($theme->status) {
      $region = db_query("SELECT region FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(
299 300
        ':module' => $block->module,
        ':delta' => $block->delta,
301
        ':theme' => $key,
302
      ))->fetchField();
303

304 305 306 307 308 309 310 311 312
      // Use a meaningful title for the main site theme and administrative
      // theme.
      $theme_title = $theme->info['name'];
      if ($key == $theme_default) {
        $theme_title = t('!theme (default theme)', array('!theme' => $theme_title));
      }
      elseif ($admin_theme && $key == $admin_theme) {
        $theme_title = t('!theme (administration theme)', array('!theme' => $theme_title));
      }
313
      $form['regions'][$key] = array(
314
        '#type' => 'select',
315
        '#title' => $theme_title,
316 317 318
        '#default_value' => !empty($region) && $region != -1 ? $region : NULL,
        '#empty_value' => BLOCK_REGION_NONE,
        '#options' => system_region_list($key, REGIONS_VISIBLE),
319
        '#weight' => ($key == $theme_default ? 9 : 10),
320 321 322 323
      );
    }
  }

324 325 326 327 328 329 330 331 332 333 334
  // Visibility settings.
  $form['visibility_title'] = array(
    '#type' => 'item',
    '#title' => t('Visibility settings'),
  );
  $form['visibility'] = array(
    '#type' => 'vertical_tabs',
    '#attached' => array(
      'js' => array(drupal_get_path('module', 'block') . '/block.js'),
    ),
  );
335

336 337
  // Per-path visibility.
  $form['visibility']['path'] = array(
338
    '#type' => 'fieldset',
339
    '#title' => t('Pages'),
340
    '#collapsible' => TRUE,
341
    '#collapsed' => TRUE,
342
    '#group' => 'visibility',
343
    '#weight' => 0,
344 345
  );

346
  $access = user_access('use PHP for settings');
347
  if (isset($block->visibility) && $block->visibility == BLOCK_VISIBILITY_PHP && !$access) {
348 349
    $form['visibility']['path']['visibility'] = array(
      '#type' => 'value',
350
      '#value' => BLOCK_VISIBILITY_PHP,
351 352
    );
    $form['visibility']['path']['pages'] = array(
353 354 355
      '#type' => 'value',
      '#value' => isset($block->pages) ? $block->pages : '',
    );
356 357
  }
  else {
358
    $options = array(
359 360
      BLOCK_VISIBILITY_NOTLISTED => t('All pages except those listed'),
      BLOCK_VISIBILITY_LISTED => t('Only the listed pages'),
361 362
    );
    $description = t("Specify pages by using their paths. Enter one path per line. 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>'));
363

364
    if (module_exists('php') && $access) {
365
      $options += array(BLOCK_VISIBILITY_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
366 367 368 369 370
      $title = t('Pages or PHP code');
      $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
    }
    else {
      $title = t('Pages');
371
    }
372
    $form['visibility']['path']['visibility'] = array(
373 374 375
      '#type' => 'radios',
      '#title' => t('Show block on specific pages'),
      '#options' => $options,
376
      '#default_value' => isset($block->visibility) ? $block->visibility : BLOCK_VISIBILITY_NOTLISTED,
377
    );
378
    $form['visibility']['path']['pages'] = array(
379
      '#type' => 'textarea',
380
      '#title' => '<span class="element-invisible">' . $title . '</span>',
381
      '#default_value' => isset($block->pages) ? $block->pages : '',
382 383 384 385
      '#description' => $description,
    );
  }

386 387
  // Per-role visibility.
  $default_role_options = db_query("SELECT rid FROM {block_role} WHERE module = :module AND delta = :delta", array(
388 389
    ':module' => $block->module,
    ':delta' => $block->delta,
390
  ))->fetchCol();
391
  $role_options = array_map('check_plain', user_roles());
392
  $form['visibility']['role'] = array(
393
    '#type' => 'fieldset',
394
    '#title' => t('Roles'),
395 396
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
397
    '#group' => 'visibility',
398
    '#weight' => 10,
399
  );
400
  $form['visibility']['role']['roles'] = array(
401
    '#type' => 'checkboxes',
402 403 404 405
    '#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.'),
406 407
  );

408 409
  // Per-user visibility.
  $form['visibility']['user'] = array(
410
    '#type' => 'fieldset',
411
    '#title' => t('Users'),
412 413
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
414
    '#group' => 'visibility',
415
    '#weight' => 20,
416
  );
417
  $form['visibility']['user']['custom'] = array(
418
    '#type' => 'radios',
419
    '#title' => t('Customizable per user'),
420
    '#options' => array(
421 422 423
      BLOCK_CUSTOM_FIXED => t('Not customizable'),
      BLOCK_CUSTOM_ENABLED => t('Customizable, visible by default'),
      BLOCK_CUSTOM_DISABLED => t('Customizable, hidden by default'),
424 425
    ),
    '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
426
    '#default_value' => isset($block->custom) ? $block->custom : BLOCK_CUSTOM_FIXED,
427 428
  );

429
  $form['actions'] = array('#type' => 'actions');
430
  $form['actions']['submit'] = array(
431 432 433 434 435 436 437
    '#type' => 'submit',
    '#value' => t('Save block'),
  );

  return $form;
}

438 439 440 441 442 443
/**
 * Form validation handler for the block configuration form.
 *
 * @see block_admin_configure()
 * @see block_admin_configure_submit()
 */
444 445
function block_admin_configure_validate($form, &$form_state) {
  if ($form_state['values']['module'] == 'block') {
446
    $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE bid <> :bid AND info = :info', 0, 1, array(
447 448
      ':bid' => $form_state['values']['delta'],
      ':info' => $form_state['values']['info'],
449
    ))->fetchField();
450
    if (empty($form_state['values']['info']) || $custom_block_exists) {
451
      form_set_error('info', t('Ensure that each block description is unique.'));
452 453 454 455
    }
  }
}

456 457 458 459 460 461
/**
 * Form submission handler for the block configuration form.
 *
 * @see block_admin_configure()
 * @see block_admin_configure_validate()
 */
462 463
function block_admin_configure_submit($form, &$form_state) {
  if (!form_get_errors()) {
464 465
    $txn = db_transaction();

466 467
    db_update('block')
      ->fields(array(
Dries's avatar
Dries committed
468
        'visibility' => (int) $form_state['values']['visibility'],
469
        'pages' => trim($form_state['values']['pages']),
Dries's avatar
Dries committed
470
        'custom' => (int) $form_state['values']['custom'],
471 472 473 474 475
        'title' => $form_state['values']['title'],
      ))
      ->condition('module', $form_state['values']['module'])
      ->condition('delta', $form_state['values']['delta'])
      ->execute();
476

477 478 479 480 481
    db_delete('block_role')
      ->condition('module', $form_state['values']['module'])
      ->condition('delta', $form_state['values']['delta'])
      ->execute();
    $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
482
    foreach (array_filter($form_state['values']['roles']) as $rid) {
483 484 485 486 487
      $query->values(array(
        'rid' => $rid,
        'module' => $form_state['values']['module'],
        'delta' => $form_state['values']['delta'],
      ));
488
    }
489
    $query->execute();
490

491 492 493 494 495
    // Store regions per theme for this block
    foreach ($form_state['values']['regions'] as $theme => $region) {
      db_merge('block')
        ->key(array('theme' => $theme, 'delta' => $form_state['values']['delta'], 'module' => $form_state['values']['module']))
        ->fields(array(
496
          'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
497 498 499 500 501 502
          'pages' => trim($form_state['values']['pages']),
          'status' => (int) ($region != BLOCK_REGION_NONE),
        ))
        ->execute();
    }

503
    module_invoke($form_state['values']['module'], 'block_save', $form_state['values']['delta'], $form_state['values']);
504 505
    drupal_set_message(t('The block configuration has been saved.'));
    cache_clear_all();
506
    $form_state['redirect'] = 'admin/structure/block';
507 508 509 510
  }
}

/**
511 512 513 514 515
 * Form builder for the add block form.
 *
 * @see block_add_block_form_validate()
 * @see block_add_block_form_submit()
 * @ingroup forms
516
 */
517
function block_add_block_form($form, &$form_state) {
518
  return block_admin_configure($form, $form_state, 'block', NULL);
519 520
}

521 522 523 524 525 526
/**
 * Form validation handler for the add block form.
 *
 * @see block_add_block_form()
 * @see block_add_block_form_submit()
 */
527
function block_add_block_form_validate($form, &$form_state) {
528
  $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE info = :info', 0, 1, array(':info' => $form_state['values']['info']))->fetchField();
529

530
  if (empty($form_state['values']['info']) || $custom_block_exists) {
531
    form_set_error('info', t('Ensure that each block description is unique.'));
532 533 534 535
  }
}

/**
536 537 538 539 540 541
 * Form submission handler for the add block form.
 *
 * Saves the new custom block.
 *
 * @see block_add_block_form()
 * @see block_add_block_form_validate()
542 543
 */
function block_add_block_form_submit($form, &$form_state) {
544
  $delta = db_insert('block_custom')
545
    ->fields(array(
546
      'body' => $form_state['values']['body']['value'],
547
      'info' => $form_state['values']['info'],
548
      'format' => $form_state['values']['body']['format'],
549 550
    ))
    ->execute();
551 552
  // Store block delta to allow other modules to work with new block.
  $form_state['values']['delta'] = $delta;
553 554

  $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
555 556
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
557
      $query->values(array(
Dries's avatar
Dries committed
558
        'visibility' => (int) $form_state['values']['visibility'],
559
        'pages' => trim($form_state['values']['pages']),
Dries's avatar
Dries committed
560
        'custom' => (int) $form_state['values']['custom'],
561
        'title' => $form_state['values']['title'],
562
        'module' => $form_state['values']['module'],
563
        'theme' => $theme->name,
564 565
        'status' => 0,
        'weight' => 0,
566
        'delta' => $delta,
567
        'cache' => DRUPAL_NO_CACHE,
568
      ));
569 570
    }
  }
571
  $query->execute();
572

573
  $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
574
  foreach (array_filter($form_state['values']['roles']) as $rid) {
575 576 577 578 579
    $query->values(array(
      'rid' => $rid,
      'module' => $form_state['values']['module'],
      'delta' => $delta,
    ));
580
  }
581
  $query->execute();
582

583 584 585 586 587
  // Store regions per theme for this block
  foreach ($form_state['values']['regions'] as $theme => $region) {
    db_merge('block')
      ->key(array('theme' => $theme, 'delta' => $delta, 'module' => $form_state['values']['module']))
      ->fields(array(
588
        'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
589 590 591 592 593
        'pages' => trim($form_state['values']['pages']),
        'status' => (int) ($region != BLOCK_REGION_NONE),
      ))
      ->execute();
  }
594

595 596
  drupal_set_message(t('The block has been created.'));
  cache_clear_all();
597
  $form_state['redirect'] = 'admin/structure/block';
598 599 600
}

/**
601 602 603 604 605 606 607 608 609
 * Form builder for the custom block deletion form.
 *
 * @param $module
 *   The name of the module that implements the block to be deleted. This should
 *   always equal 'block' since it only allows custom blocks to be deleted.
 * @param $delta
 *   The unique ID of the block within the context of $module.
 *
 * @see block_custom_block_delete_submit()
610
 */
611 612
function block_custom_block_delete($form, &$form_state, $module, $delta) {
  $block = block_load($module, $delta);
613
  $custom_block = block_custom_block_get($block->delta);
614
  $form['info'] = array('#type' => 'hidden', '#value' => $custom_block['info'] ? $custom_block['info'] : $custom_block['title']);
615
  $form['bid'] = array('#type' => 'hidden', '#value' => $block->delta);
616

617
  return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $custom_block['info'])), 'admin/structure/block', '', t('Delete'), t('Cancel'));
618 619 620
}

/**
621 622 623
 * Form submission handler for the custom block deletion form.
 *
 * @see block_custom_block_delete()
624
 */
625 626
function block_custom_block_delete_submit($form, &$form_state) {
  db_delete('block_custom')
627 628 629 630 631 632
    ->condition('bid', $form_state['values']['bid'])
    ->execute();
  db_delete('block')
    ->condition('module', 'block')
    ->condition('delta', $form_state['values']['bid'])
    ->execute();
633 634 635 636
  db_delete('block_role')
    ->condition('module', 'block')
    ->condition('delta', $form_state['values']['bid'])
    ->execute();
637 638
  drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
  cache_clear_all();
639
  $form_state['redirect'] = 'admin/structure/block';
640 641
  return;
}
642 643

/**
644
 * Processes variables for block-admin-display-form.tpl.php.
645 646 647 648 649 650 651
 *
 * The $variables array contains the following arguments:
 * - $form
 *
 * @see block-admin-display.tpl.php
 * @see theme_block_admin_display()
 */
652
function template_preprocess_block_admin_display_form(&$variables) {
653 654 655 656
  $variables['block_regions'] = $variables['form']['block_regions']['#value'];
  if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
    $variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled');
  }
657

658
  foreach ($variables['block_regions'] as $key => $value) {
659 660
    // Initialize an empty array for the region.
    $variables['block_listing'][$key] = array();
661 662
  }

663 664 665
  // Initialize disabled blocks array.
  $variables['block_listing'][BLOCK_REGION_NONE] = array();

666 667 668 669 670
  // Add each block in the form to the appropriate place in the block listing.
  foreach (element_children($variables['form']['blocks']) as $i) {
    $block = &$variables['form']['blocks'][$i];

    // Fetch the region for the current block.
671
    $region = (isset($block['region']['#default_value']) ? $block['region']['#default_value'] : BLOCK_REGION_NONE);
672 673 674 675 676 677 678 679 680 681 682 683 684 685

    // Set special classes needed for table drag and drop.
    $block['region']['#attributes']['class'] = array('block-region-select', 'block-region-' . $region);
    $block['weight']['#attributes']['class'] = array('block-weight', 'block-weight-' . $region);

    $variables['block_listing'][$region][$i] = new stdClass();
    $variables['block_listing'][$region][$i]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
    $variables['block_listing'][$region][$i]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
    $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
    $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
    $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
    $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
    $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
    $variables['block_listing'][$region][$i]->printed = FALSE;
686 687
  }

688
  $variables['form_submit'] = drupal_render_children($variables['form']);
689
}
690