function block_create in Patterns 7.2
Wraps several calls to several drupal_form_submit to create a block. Depending on the type of block (custom or not), a different amount of records will be inserted.
Parameters
string $form_id String containing the form ID. In the case of custom functions the value is empty.:
array $form_state Set of values after parsing the action.:
2 string references to 'block_create'
- block_patterns in patterns_components/
components/ block.inc - block_patterns_cleanup in patterns_components/
components/ block.inc - Implements hook_patterns_cleanup().
File
- patterns_components/
components/ block.inc, line 441
Code
function block_create($form_id, &$form_state) {
//In case the block is a custom one (created by block) we will need
//to perform and extra insertion in the block_custom table (all fields are mandatory)
//and to add an entry for each theme as in function block_add_block_form_submit()
if ($form_state['values']['module'] == 'block') {
//Insert an unique specific record for block_custom
$delta = db_insert('block_custom')
->fields(array(
'body' => $form_state['values']['body'],
'info' => $form_state['values']['info'],
'format' => $form_state['values']['format'],
))
->execute();
// Store block delta to allow other modules to work with new block.
$form_state['values']['delta'] = $delta;
//Insert specific record for the theme given in the pattern
$query = db_insert('block')
->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'status',
'weight',
'delta',
'cache',
'region',
));
$query
->values(array(
'visibility' => isset($form_state['values']['visibility']) ? (int) $form_state['values']['visibility'] : 1,
'pages' => isset($form_state['values']['pages']) ? trim($form_state['values']['pages']) : '',
'custom' => isset($form_state['values']['custom']) ? (int) $form_state['values']['custom'] : 0,
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => isset($form_state['values']['theme']) ? $form_state['values']['theme'] : variable_get('theme_default', 'bartik'),
'status' => isset($form_state['values']['status']) ? $form_state['values']['status'] : 0,
'weight' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
'delta' => $form_state['values']['delta'],
'cache' => isset($form_state['values']['cache']) ? $form_state['values']['cache'] : -1,
'region' => isset($form_state['values']['region']) ? $form_state['values']['region'] : -1,
));
$query
->execute();
//Insert rest of values, for all themes but the one already given in the pattern
$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 && $theme->name != $form_state['values']['theme']) {
$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' => $form_state['values']['delta'],
'cache' => DRUPAL_NO_CACHE,
));
}
}
$query
->execute();
}
else {
//If it is not a custom block, we will just insert the new record with the values provided in the pattern
$query = db_insert('block')
->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'status',
'weight',
'delta',
'cache',
'region',
));
$query
->values(array(
'visibility' => isset($form_state['values']['visibility']) ? (int) $form_state['values']['visibility'] : 1,
'pages' => isset($form_state['values']['pages']) ? trim($form_state['values']['pages']) : '',
'custom' => isset($form_state['values']['custom']) ? (int) $form_state['values']['custom'] : 0,
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => isset($form_state['values']['theme']) ? $form_state['values']['theme'] : variable_get('theme_default', 'bartik'),
'status' => isset($form_state['values']['status']) ? $form_state['values']['status'] : 0,
'weight' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
'delta' => $form_state['values']['delta'],
'cache' => isset($form_state['values']['cache']) ? $form_state['values']['cache'] : -1,
'region' => isset($form_state['values']['region']) ? $form_state['values']['region'] : -1,
));
$query
->execute();
}
return patterns_results(PATTERNS_SUCCESS, t('New block successfully added'));
}