2007-07-16 06:40:25 +00:00
< ? php
// $Id$
/**
* @ file
* Admin page callbacks for the block module .
*/
2007-10-05 09:35:09 +00:00
/**
2009-07-20 18:51:36 +00:00
* Menu callback for admin / structure / block .
2007-10-05 09:35:09 +00:00
*/
function block_admin_display ( $theme = NULL ) {
global $custom_theme ;
// If non-default theme configuration has been selected, set the custom theme.
$custom_theme = isset ( $theme ) ? $theme : variable_get ( 'theme_default' , 'garland' );
2008-05-15 21:30:02 +00:00
// Fetch and sort blocks.
2007-10-05 09:35:09 +00:00
$blocks = _block_rehash ();
usort ( $blocks , '_block_compare' );
return drupal_get_form ( 'block_admin_display_form' , $blocks , $theme );
}
2007-07-16 06:40:25 +00:00
/**
2007-12-19 19:13:29 +00:00
* Generate main blocks administration form .
2007-07-16 06:40:25 +00:00
*/
2007-10-05 09:35:09 +00:00
function block_admin_display_form ( & $form_state , $blocks , $theme = NULL ) {
2007-07-16 06:40:25 +00:00
global $theme_key , $custom_theme ;
2008-10-26 18:06:39 +00:00
drupal_add_css ( drupal_get_path ( 'module' , 'block' ) . '/block.css' , array ( 'preprocess' => FALSE ));
2007-07-16 06:40:25 +00:00
// If non-default theme configuration has been selected, set the custom theme.
2007-10-05 09:35:09 +00:00
$custom_theme = isset ( $theme ) ? $theme : variable_get ( 'theme_default' , 'garland' );
2009-07-14 10:22:17 +00:00
drupal_theme_initialize ();
2007-07-16 06:40:25 +00:00
2009-07-18 02:36:01 +00:00
$block_regions = system_region_list ( $theme_key , REGIONS_VISIBLE ) + array ( BLOCK_REGION_NONE => '<' . t ( 'none' ) . '>' );
2007-07-16 06:40:25 +00:00
2008-11-22 10:53:20 +00:00
// 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 );
2008-05-15 21:30:02 +00:00
// Build the form tree.
2007-10-05 09:35:09 +00:00
$form = array (
2009-07-20 18:51:36 +00:00
'#action' => arg ( 4 ) ? url ( 'admin/structure/block/list/' . $theme_key ) : url ( 'admin/structure/block' ),
2007-10-05 09:35:09 +00:00
'#tree' => TRUE ,
);
2007-11-14 09:50:00 +00:00
2007-07-16 06:40:25 +00:00
foreach ( $blocks as $i => $block ) {
2008-04-14 17:48:46 +00:00
$key = $block [ 'module' ] . '_' . $block [ 'delta' ];
2007-10-05 09:35:09 +00:00
$form [ $key ][ 'module' ] = array (
'#type' => 'value' ,
'#value' => $block [ 'module' ],
);
$form [ $key ][ 'delta' ] = array (
'#type' => 'value' ,
'#value' => $block [ 'delta' ],
);
$form [ $key ][ 'info' ] = array (
2008-07-16 21:59:29 +00:00
'#markup' => check_plain ( $block [ 'info' ]),
2007-10-05 09:35:09 +00:00
);
$form [ $key ][ 'theme' ] = array (
'#type' => 'hidden' ,
2008-05-15 21:30:02 +00:00
'#value' => $theme_key ,
2007-10-05 09:35:09 +00:00
);
$form [ $key ][ 'weight' ] = array (
'#type' => 'weight' ,
'#default_value' => $block [ 'weight' ],
2008-11-22 10:53:20 +00:00
'#delta' => $weight_delta ,
2007-10-05 09:35:09 +00:00
);
$form [ $key ][ 'region' ] = array (
'#type' => 'select' ,
2007-11-14 09:50:00 +00:00
'#default_value' => $block [ 'region' ],
2007-07-16 06:40:25 +00:00
'#options' => $block_regions ,
);
2008-05-15 21:30:02 +00:00
$form [ $key ][ 'configure' ] = array (
2008-07-16 21:59:29 +00:00
'#markup' => l ( t ( 'configure' ),
2009-07-20 18:51:36 +00:00
'admin/structure/block/configure/' . $block [ 'module' ] . '/' . $block [ 'delta' ]),
2008-05-15 21:30:02 +00:00
);
2007-07-16 06:40:25 +00:00
if ( $block [ 'module' ] == 'block' ) {
2008-05-15 21:30:02 +00:00
$form [ $key ][ 'delete' ] = array (
2009-02-11 03:38:46 +00:00
'#markup' => l ( t ( 'delete' ),
2009-07-20 18:51:36 +00:00
'admin/structure/block/delete/' . $block [ 'delta' ]),
2008-05-15 21:30:02 +00:00
);
2007-07-16 06:40:25 +00:00
}
}
2009-05-21 21:12:25 +00:00
// Do not allow disabling the main system content block.
unset ( $form [ 'system_main' ][ 'region' ][ '#options' ][ BLOCK_REGION_NONE ]);
2007-10-05 09:35:09 +00:00
$form [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Save blocks' ),
);
2007-07-16 06:40:25 +00:00
return $form ;
}
/**
2008-05-15 21:30:02 +00:00
* Process main blocks administration form submissions .
2007-07-16 06:40:25 +00:00
*/
2007-10-05 09:35:09 +00:00
function block_admin_display_form_submit ( $form , & $form_state ) {
2007-07-16 06:40:25 +00:00
foreach ( $form_state [ 'values' ] as $block ) {
2009-05-28 11:31:20 +00:00
$block [ 'status' ] = ( int ) ( $block [ 'region' ] != BLOCK_REGION_NONE );
2007-07-16 06:40:25 +00:00
$block [ 'region' ] = $block [ 'status' ] ? $block [ 'region' ] : '' ;
2009-05-28 11:31:20 +00:00
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 ();
2007-07-16 06:40:25 +00:00
}
drupal_set_message ( t ( 'The block settings have been updated.' ));
cache_clear_all ();
}
/**
2009-07-20 18:51:36 +00:00
* Helper function for sorting blocks on admin / structure / block .
2007-07-16 06:40:25 +00:00
*
* Active blocks are sorted by region , then by weight .
* Disabled blocks are sorted by name .
*/
function _block_compare ( $a , $b ) {
2007-11-14 09:50:00 +00:00
global $theme_key ;
2009-06-07 02:29:07 +00:00
$regions = & drupal_static ( __FUNCTION__ );
2007-11-14 09:50:00 +00:00
// We need the region list to correctly order by region.
if ( ! isset ( $regions )) {
$regions = array_flip ( array_keys ( system_region_list ( $theme_key )));
$regions [ BLOCK_REGION_NONE ] = count ( $regions );
}
2007-07-16 06:40:25 +00:00
// Separate enabled from disabled.
2007-11-14 09:50:00 +00:00
$status = $b [ 'status' ] - $a [ 'status' ];
2007-07-16 06:40:25 +00:00
if ( $status ) {
return $status ;
}
2007-11-14 09:50:00 +00:00
// Sort by region (in the order defined by theme .info file).
2008-06-25 09:52:41 +00:00
if (( ! empty ( $a [ 'region' ]) && ! empty ( $b [ 'region' ])) && ( $place = ( $regions [ $a [ 'region' ]] - $regions [ $b [ 'region' ]]))) {
2007-10-05 09:35:09 +00:00
return $place ;
2007-07-16 06:40:25 +00:00
}
2007-10-05 09:35:09 +00:00
// Sort by weight.
$weight = $a [ 'weight' ] - $b [ 'weight' ];
if ( $weight ) {
return $weight ;
2007-07-16 06:40:25 +00:00
}
2007-10-05 09:35:09 +00:00
// Sort by title.
return strcmp ( $a [ 'info' ], $b [ 'info' ]);
2007-07-16 06:40:25 +00:00
}
/**
* Menu callback ; displays the block configuration form .
*/
function block_admin_configure ( & $form_state , $module = NULL , $delta = 0 ) {
2008-05-15 21:30:02 +00:00
$form [ 'module' ] = array (
'#type' => 'value' ,
'#value' => $module ,
);
$form [ 'delta' ] = array (
'#type' => 'value' ,
'#value' => $delta ,
);
2007-07-16 06:40:25 +00:00
2009-05-28 11:31:20 +00:00
$edit = db_query ( " SELECT pages, visibility, custom, title FROM { block} WHERE module = :module AND delta = :delta " , array (
':module' => $module ,
':delta' => $delta ,
)) -> fetchAssoc ();
2007-07-16 06:40:25 +00:00
$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 ,
2007-12-22 23:24:26 +00:00
'#description' => $module == 'block' ? t ( 'The title of the block as shown to the user.' ) : t ( 'Override the default title for the block. Use <em><none></em> to display no title, or leave blank to use the default block title.' ),
2007-07-16 06:40:25 +00:00
'#default_value' => $edit [ 'title' ],
'#weight' => - 18 ,
);
2009-08-26 10:10:01 +00:00
// Allow the user to define this block's region directly
$form [ 'regions' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Region settings' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
'#description' => t ( 'Specify in which region this block is displayed.' ),
'#tree' => TRUE ,
);
$theme_default = variable_get ( 'theme_default' , 'garland' );
// Create a select list for each theme
2009-08-27 19:40:36 +00:00
foreach ( list_themes () as $theme_key => $theme ) {
2009-08-26 10:10:01 +00:00
// Only display enabled themes
if ( $theme -> status ) {
$region = db_query ( " SELECT region FROM { block} WHERE module = :module AND delta = :delta AND theme = :theme " , array (
':module' => $module ,
':delta' => $delta ,
':theme' => $theme_key ,
)) -> fetchField ();
$form [ 'regions' ][ $theme_key ] = array (
'#type' => 'select' ,
'#title' => t ( '!theme region' , array ( '!theme' => $theme -> info [ 'name' ])),
'#default_value' => ( ! empty ( $region ) ? $region : BLOCK_REGION_NONE ),
'#options' => array ( BLOCK_REGION_NONE => t ( 'Disabled' )) + $theme -> info [ 'regions' ],
'#expandable' => ( $theme_key !== $theme_default ),
'#weight' => ( $theme_key == $theme_default ? 9 : 10 ),
);
}
}
2007-07-16 06:40:25 +00:00
// Module-specific block configurations.
2008-12-16 23:57:33 +00:00
if ( $settings = module_invoke ( $module , 'block_configure' , $delta )) {
2007-07-16 06:40:25 +00:00
foreach ( $settings as $k => $v ) {
$form [ 'block_settings' ][ $k ] = $v ;
}
}
// Get the block subject for the page title.
2009-08-29 05:46:04 +00:00
$info = module_invoke ( $module , 'block_info' );
2007-07-16 06:40:25 +00:00
if ( isset ( $info [ $delta ])) {
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( " '%name' block " , array ( '%name' => $info [ $delta ][ 'info' ])), PASS_THROUGH );
2007-07-16 06:40:25 +00:00
}
$form [ 'page_vis_settings' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Page specific visibility settings' ),
'#collapsible' => TRUE ,
2008-11-22 10:27:25 +00:00
'#collapsed' => TRUE ,
2007-07-16 06:40:25 +00:00
);
2009-05-07 15:29:08 +00:00
$access = user_access ( 'use PHP for settings' );
2007-07-16 06:40:25 +00:00
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 {
2009-06-27 11:48:58 +00:00
$options = array ( t ( 'Every page except those specified below.' ), t ( 'Only the pages specified below.' ));
2007-07-16 06:40:25 +00:00
$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>' ));
2009-05-07 15:29:08 +00:00
if ( module_exists ( 'php' ) && $access ) {
2007-07-16 06:40:25 +00:00
$options [] = t ( 'Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).' );
2008-04-14 17:48:46 +00:00
$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 ?>' ));
2007-07-16 06:40:25 +00:00
}
$form [ 'page_vis_settings' ][ 'visibility' ] = array (
'#type' => 'radios' ,
'#title' => t ( 'Show block on specific pages' ),
'#options' => $options ,
2009-05-29 21:28:58 +00:00
'#default_value' => $edit [ 'visibility' ],
2007-07-16 06:40:25 +00:00
);
$form [ 'page_vis_settings' ][ 'pages' ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Pages' ),
'#default_value' => $edit [ 'pages' ],
'#description' => $description ,
);
}
2008-11-22 10:27:25 +00:00
// Role-based visibility settings.
2009-05-28 11:31:20 +00:00
$default_role_options = db_query ( " SELECT rid FROM { block_role} WHERE module = :module AND delta = :delta " , array (
':module' => $module ,
':delta' => $delta ,
)) -> fetchCol ();
$role_options = db_query ( 'SELECT rid, name FROM {role} ORDER BY name' ) -> fetchAllKeyed ();
2008-11-22 10:27:25 +00:00
$form [ 'role_vis_settings' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Role specific visibility settings' ),
'#collapsible' => TRUE ,
'#collapsed' => 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.' ),
);
2009-07-01 08:04:19 +00:00
// Content type specific configuration.
$default_type_options = db_query ( " SELECT type FROM { block_node_type} WHERE module = :module AND delta = :delta " , array (
':module' => $module ,
':delta' => $delta ,
)) -> fetchCol ();
$form [ 'content_type_vis_settings' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Content type specific visibility settings' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
);
$form [ 'content_type_vis_settings' ][ 'types' ] = array (
'#type' => 'checkboxes' ,
'#title' => t ( 'Show block for specific content types' ),
'#default_value' => $default_type_options ,
'#options' => node_type_get_names (),
'#description' => t ( 'Show this block only when on a page displaying a post of the given type(s). If you select no types, there will be no type specific limitation.' ),
);
2008-11-22 10:27:25 +00:00
// Standard block configurations.
$form [ 'user_vis_settings' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'User specific visibility settings' ),
'#collapsible' => TRUE ,
'#collapsed' => 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.' ),
2009-05-29 21:28:58 +00:00
'#default_value' => $edit [ 'custom' ],
2008-11-22 10:27:25 +00:00
);
2007-07-16 06:40:25 +00:00
$form [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Save block' ),
);
return $form ;
}
function block_admin_configure_validate ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'module' ] == 'block' ) {
2009-08-28 19:44:05 +00:00
$custom_block_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {block_custom} WHERE bid <> :bid AND info = :info' , array (
2009-05-28 11:31:20 +00:00
':bid' => $form_state [ 'values' ][ 'delta' ],
':info' => $form_state [ 'values' ][ 'info' ],
), 0 , 1 ) -> fetchField ();
2009-08-28 19:44:05 +00:00
if ( empty ( $form_state [ 'values' ][ 'info' ]) || $custom_block_exists ) {
2007-07-16 06:40:25 +00:00
form_set_error ( 'info' , t ( 'Please ensure that each block description is unique.' ));
}
}
}
function block_admin_configure_submit ( $form , & $form_state ) {
if ( ! form_get_errors ()) {
2009-05-28 11:31:20 +00:00
db_update ( 'block' )
-> fields ( array (
2009-05-29 21:28:58 +00:00
'visibility' => ( int ) $form_state [ 'values' ][ 'visibility' ],
2009-05-28 11:31:20 +00:00
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
2009-05-29 21:28:58 +00:00
'custom' => ( int ) $form_state [ 'values' ][ 'custom' ],
2009-05-28 11:31:20 +00:00
'title' => $form_state [ 'values' ][ 'title' ],
))
-> condition ( 'module' , $form_state [ 'values' ][ 'module' ])
-> condition ( 'delta' , $form_state [ 'values' ][ 'delta' ])
-> execute ();
2009-07-01 08:04:19 +00:00
2009-05-28 11:31:20 +00:00
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' ));
2007-07-16 06:40:25 +00:00
foreach ( array_filter ( $form_state [ 'values' ][ 'roles' ]) as $rid ) {
2009-05-28 11:31:20 +00:00
$query -> values ( array (
'rid' => $rid ,
'module' => $form_state [ 'values' ][ 'module' ],
'delta' => $form_state [ 'values' ][ 'delta' ],
));
2007-07-16 06:40:25 +00:00
}
2009-05-28 11:31:20 +00:00
$query -> execute ();
2009-07-01 08:04:19 +00:00
db_delete ( 'block_node_type' )
-> condition ( 'module' , $form_state [ 'values' ][ 'module' ])
-> condition ( 'delta' , $form_state [ 'values' ][ 'delta' ])
-> execute ();
$query = db_insert ( 'block_node_type' ) -> fields ( array ( 'type' , 'module' , 'delta' ));
foreach ( array_filter ( $form_state [ 'values' ][ 'types' ]) as $type ) {
$query -> values ( array (
'type' => $type ,
'module' => $form_state [ 'values' ][ 'module' ],
'delta' => $form_state [ 'values' ][ 'delta' ],
));
}
$query -> execute ();
2009-08-26 10:10:01 +00:00
// 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 (
'region' => $region ,
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
'status' => ( int ) ( $region != BLOCK_REGION_NONE ),
))
-> execute ();
}
2008-12-16 23:57:33 +00:00
module_invoke ( $form_state [ 'values' ][ 'module' ], 'block_save' , $form_state [ 'values' ][ 'delta' ], $form_state [ 'values' ]);
2007-07-16 06:40:25 +00:00
drupal_set_message ( t ( 'The block configuration has been saved.' ));
cache_clear_all ();
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/block' ;
2007-07-16 06:40:25 +00:00
}
}
/**
* Menu callback : display the custom block addition form .
*/
function block_add_block_form ( & $form_state ) {
return block_admin_configure ( $form_state , 'block' , NULL );
}
function block_add_block_form_validate ( $form , & $form_state ) {
2009-08-28 19:44:05 +00:00
$custom_block_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {block_custom} WHERE info = :info' , array ( ':info' => $form_state [ 'values' ][ 'info' ]), 0 , 1 ) -> fetchField ();
2009-05-16 15:23:16 +00:00
2009-08-28 19:44:05 +00:00
if ( empty ( $form_state [ 'values' ][ 'info' ]) || $custom_block_exists ) {
2007-07-16 06:40:25 +00:00
form_set_error ( 'info' , t ( 'Please ensure that each block description is unique.' ));
}
}
/**
* Save the new custom block .
*/
function block_add_block_form_submit ( $form , & $form_state ) {
2009-08-28 19:44:05 +00:00
$delta = db_insert ( 'block_custom' )
2009-05-28 11:31:20 +00:00
-> fields ( array (
'body' => $form_state [ 'values' ][ 'body' ],
'info' => $form_state [ 'values' ][ 'info' ],
'format' => $form_state [ 'values' ][ 'body_format' ],
))
-> execute ();
$query = db_insert ( 'block' ) -> fields ( array ( 'visibility' , 'pages' , 'custom' , 'title' , 'module' , 'theme' , 'status' , 'weight' , 'delta' , 'cache' ));
2007-07-16 06:40:25 +00:00
foreach ( list_themes () as $key => $theme ) {
if ( $theme -> status ) {
2009-05-28 11:31:20 +00:00
$query -> values ( array (
2009-05-29 21:28:58 +00:00
'visibility' => ( int ) $form_state [ 'values' ][ 'visibility' ],
2009-05-28 11:31:20 +00:00
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
2009-05-29 21:28:58 +00:00
'custom' => ( int ) $form_state [ 'values' ][ 'custom' ],
2009-05-28 11:31:20 +00:00
'title' => $form_state [ 'values' ][ 'title' ],
'module' => $form_state [ 'values' ][ 'module' ],
'theme' => $theme -> name ,
'status' => 0 ,
'weight' => 0 ,
'delta' => $delta ,
2009-08-31 17:06:10 +00:00
'cache' => DRUPAL_NO_CACHE ,
2009-05-28 11:31:20 +00:00
));
2007-07-16 06:40:25 +00:00
}
}
2009-05-28 11:31:20 +00:00
$query -> execute ();
2007-07-16 06:40:25 +00:00
2009-05-28 11:31:20 +00:00
$query = db_insert ( 'block_role' ) -> fields ( array ( 'rid' , 'module' , 'delta' ));
2007-07-16 06:40:25 +00:00
foreach ( array_filter ( $form_state [ 'values' ][ 'roles' ]) as $rid ) {
2009-05-28 11:31:20 +00:00
$query -> values ( array (
'rid' => $rid ,
'module' => $form_state [ 'values' ][ 'module' ],
'delta' => $delta ,
));
2007-07-16 06:40:25 +00:00
}
2009-05-28 11:31:20 +00:00
$query -> execute ();
2007-07-16 06:40:25 +00:00
2009-07-01 08:04:19 +00:00
$query = db_insert ( 'block_node_type' ) -> fields ( array ( 'type' , 'module' , 'delta' ));
foreach ( array_filter ( $form_state [ 'values' ][ 'types' ]) as $type ) {
$query -> values ( array (
'type' => $type ,
'module' => $form_state [ 'values' ][ 'module' ],
'delta' => $delta ,
));
}
$query -> execute ();
2009-08-27 19:40:36 +00:00
// 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 ,
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
'status' => ( int ) ( $region != BLOCK_REGION_NONE ),
))
-> execute ();
}
2009-07-01 08:04:19 +00:00
2007-07-16 06:40:25 +00:00
drupal_set_message ( t ( 'The block has been created.' ));
cache_clear_all ();
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/block' ;
2007-07-16 06:40:25 +00:00
}
/**
* Menu callback ; confirm deletion of custom blocks .
*/
2009-08-28 19:44:05 +00:00
function block_custom_block_delete ( & $form_state , $bid = 0 ) {
$custom_block = block_custom_block_get ( $bid );
$form [ 'info' ] = array ( '#type' => 'hidden' , '#value' => $custom_block [ 'info' ] ? $custom_block [ 'info' ] : $custom_block [ 'title' ]);
2007-07-16 06:40:25 +00:00
$form [ 'bid' ] = array ( '#type' => 'hidden' , '#value' => $bid );
2009-08-28 19:44:05 +00:00
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' ));
2007-07-16 06:40:25 +00:00
}
/**
* Deletion of custom blocks .
*/
2009-08-28 19:44:05 +00:00
function block_custom_block_delete_submit ( $form , & $form_state ) {
db_delete ( 'block_custom' )
2009-05-28 11:31:20 +00:00
-> condition ( 'bid' , $form_state [ 'values' ][ 'bid' ])
-> execute ();
db_delete ( 'block' )
-> condition ( 'module' , 'block' )
-> condition ( 'delta' , $form_state [ 'values' ][ 'bid' ])
-> execute ();
2007-07-16 06:40:25 +00:00
drupal_set_message ( t ( 'The block %name has been removed.' , array ( '%name' => $form_state [ 'values' ][ 'info' ])));
cache_clear_all ();
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/block' ;
2007-07-16 06:40:25 +00:00
return ;
}
2007-09-01 05:31:09 +00:00
/**
* Process variables for block - admin - display . tpl . php .
*
* The $variables array contains the following arguments :
* - $form
*
* @ see block - admin - display . tpl . php
* @ see theme_block_admin_display ()
*/
2007-10-05 09:35:09 +00:00
function template_preprocess_block_admin_display_form ( & $variables ) {
2007-09-01 05:31:09 +00:00
global $theme_key ;
2009-07-18 02:36:01 +00:00
$block_regions = system_region_list ( $theme_key , REGIONS_VISIBLE );
2007-11-14 09:50:00 +00:00
$variables [ 'block_regions' ] = $block_regions + array ( BLOCK_REGION_NONE => t ( 'Disabled' ));
2007-09-01 05:31:09 +00:00
foreach ( $block_regions as $key => $value ) {
2007-11-14 09:50:00 +00:00
// Initialize an empty array for the region.
$variables [ 'block_listing' ][ $key ] = array ();
2007-09-01 05:31:09 +00:00
}
2007-11-14 09:50:00 +00:00
// Initialize disabled blocks array.
$variables [ 'block_listing' ][ BLOCK_REGION_NONE ] = array ();
2007-11-26 16:36:44 +00:00
// Set up to track previous region in loop.
2007-09-01 05:31:09 +00:00
$last_region = '' ;
foreach ( element_children ( $variables [ 'form' ]) as $i ) {
$block = & $variables [ 'form' ][ $i ];
// Only take form elements that are blocks.
if ( isset ( $block [ 'info' ])) {
// Fetch region for current block.
$region = $block [ 'region' ][ '#default_value' ];
2007-11-14 09:50:00 +00:00
// Set special classes needed for table drag and drop.
2009-08-22 14:34:23 +00:00
$variables [ 'form' ][ $i ][ 'region' ][ '#attributes' ][ 'class' ] = array ( 'block-region-select' , 'block-region-' . $region );
$variables [ 'form' ][ $i ][ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'block-weight' , 'block-weight-' . $region );
2007-11-14 09:50:00 +00:00
2008-11-22 11:14:48 +00:00
$variables [ 'block_listing' ][ $region ][ $i ] = new stdClass ();
2009-08-22 14:34:23 +00:00
$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' ]);
2007-11-14 09:50:00 +00:00
$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 ;
2007-09-01 05:31:09 +00:00
$last_region = $region ;
}
}
2009-02-03 18:55:32 +00:00
$variables [ 'form_submit' ] = drupal_render_children ( $variables [ 'form' ]);
2007-09-01 05:31:09 +00:00
}