function block_add_block_form_submit

Form submission handler for block_add_block_form().

Saves the new custom block.

See also

block_add_block_form()

block_add_block_form_validate()

File

drupal/core/modules/block/block.admin.inc, line 648
Admin page callbacks for the block module.

Code

function block_add_block_form_submit($form, &$form_state) {
  $delta = db_insert('block_custom')
    ->fields(array(
    'body' => $form_state['values']['body']['value'],
    'info' => $form_state['values']['info'],
    'format' => $form_state['values']['body']['format'],
  ))
    ->execute();

  // Store block delta to allow other modules to work with new block.
  $form_state['values']['delta'] = $delta;
  $query = db_insert('block')
    ->fields(array(
    'visibility',
    'pages',
    'custom',
    'title',
    'module',
    'theme',
    'status',
    'weight',
    'delta',
    'cache',
  ));
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      $query
        ->values(array(
        'visibility' => (int) $form_state['values']['visibility'],
        'pages' => trim($form_state['values']['pages']),
        'custom' => (int) $form_state['values']['custom'],
        'title' => $form_state['values']['title'],
        'module' => $form_state['values']['module'],
        'theme' => $theme->name,
        'status' => 0,
        'weight' => 0,
        'delta' => $delta,
        'cache' => DRUPAL_NO_CACHE,
      ));
    }
  }
  $query
    ->execute();
  $query = db_insert('block_role')
    ->fields(array(
    'rid',
    'module',
    'delta',
  ));
  foreach (array_filter($form_state['values']['roles']) as $rid) {
    $query
      ->values(array(
      'rid' => $rid,
      'module' => $form_state['values']['module'],
      'delta' => $delta,
    ));
  }
  $query
    ->execute();

  // 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(
      'region' => $region == BLOCK_REGION_NONE ? '' : $region,
      'pages' => trim($form_state['values']['pages']),
      'status' => (int) ($region != BLOCK_REGION_NONE),
    ))
      ->execute();
  }

  // Update the block visibility settings if we have settings to store
  // for the existing languages.
  if (module_exists('language') && isset($form_state['values']['langcodes'])) {
    $query = db_insert('block_language')
      ->fields(array(
      'type',
      'langcode',
      'module',
      'delta',
    ));
    foreach (array_filter($form_state['values']['langcodes']) as $langcode) {
      $query
        ->values(array(
        'type' => $form_state['values']['language_type'],
        'langcode' => $langcode,
        'module' => $form_state['values']['module'],
        'delta' => $form_state['values']['delta'],
      ));
    }
    $query
      ->execute();
  }
  drupal_set_message(t('The block has been created.'));
  cache_invalidate_tags(array(
    'content' => TRUE,
  ));
  $form_state['redirect'] = 'admin/structure/block';
}