Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

How can I restore the original position (order and initial regions) of blocks on Drupal 7?

I need to make a button that you can restore the block position on Drupal 7. What function or table can be used?

share|improve this question

migrated from stackoverflow.com May 30 '12 at 15:21

1 Answer

The information about the blocks are saved in the "block" table. The block information is saved in the admin form handled by block_admin_display_form() using the following code.

  foreach ($blocks as $i => $block) {
    $key = $block['module'] . '_' . $block['delta'];
    $form['blocks'][$key]['module'] = array(
      '#type' => 'value', 
      '#value' => $block['module'],
    );
    $form['blocks'][$key]['delta'] = array(
      '#type' => 'value', 
      '#value' => $block['delta'],
    );
    $form['blocks'][$key]['info'] = array(
      '#markup' => check_plain($block['info']),
    );
    $form['blocks'][$key]['theme'] = array(
      '#type' => 'hidden', 
      '#value' => $theme,
    );
    $form['blocks'][$key]['weight'] = array(
      '#type' => 'weight', 
      '#default_value' => $block['weight'], 
      '#delta' => $weight_delta, 
      '#title_display' => 'invisible', 
      '#title' => t('Weight for @block block', array('@block' => $block['info'])),
    );
    $form['blocks'][$key]['region'] = array(
      '#type' => 'select', 
      '#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL, 
      '#empty_value' => BLOCK_REGION_NONE, 
      '#title_display' => 'invisible', 
      '#title' => t('Region for @block block', array('@block' => $block['info'])), 
      '#options' => $block_regions,
    );
    $form['blocks'][$key]['configure'] = array(
      '#type' => 'link', 
      '#title' => t('configure'), 
      '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure',
    );
    if ($block['module'] == 'block') {
      $form['blocks'][$key]['delete'] = array(
        '#type' => 'link', 
        '#title' => t('delete'), 
        '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/delete',
      );
    }
  }

Using $form_state['values']['blocks'][$key]['module'], and $form_state['values'][$key]['delta'], you can find the block information from that table, and set the form fields to the values found in the database. In the submission handler for that button, you should also set $form_state['#rebuild'] to TRUE to rebuild the form with the restored values.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.