2007-07-16 06:40:25 +00:00
< ? php
/**
* @ file
* Admin page callbacks for the block module .
*/
2009-10-14 02:13:15 +00:00
/**
2011-11-03 10:53:06 +00:00
* Page callback : Attaches CSS for the block region demo .
*
* @ see block_menu ()
2009-10-14 02:13:15 +00:00
*/
function block_admin_demo ( $theme = NULL ) {
2011-08-12 14:27:04 +00:00
drupal_add_css ( drupal_get_path ( 'module' , 'block' ) . '/block.admin.css' );
2009-10-14 02:13:15 +00:00
return '' ;
}
2007-10-05 09:35:09 +00:00
/**
2011-11-03 10:53:06 +00:00
* Page callback : Shows the block administration page .
*
2011-05-24 01:07:02 +00:00
* @ param $theme
2009-10-14 02:13:15 +00:00
* The theme to display the administration page for . If not provided , defaults
* to the currently used theme .
2011-11-03 10:53:06 +00:00
*
* @ see block_menu ()
2007-10-05 09:35:09 +00:00
*/
function block_admin_display ( $theme = NULL ) {
2009-10-14 02:13:15 +00:00
global $theme_key ;
2009-12-07 21:16:31 +00:00
2009-10-14 02:13:15 +00:00
drupal_theme_initialize ();
if ( ! isset ( $theme )) {
// If theme is not specifically set, rehash for the current theme.
$theme = $theme_key ;
}
2008-05-15 21:30:02 +00:00
// Fetch and sort blocks.
2010-08-30 00:22:03 +00:00
$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 ) {
2009-10-14 02:13:15 +00:00
$blocks = _block_rehash ( $theme );
$compare_theme = & drupal_static ( '_block_compare:theme' );
$compare_theme = $theme ;
2007-10-05 09:35:09 +00:00
usort ( $blocks , '_block_compare' );
2010-08-30 00:22:03 +00:00
return $blocks ;
2007-10-05 09:35:09 +00:00
}
2007-07-16 06:40:25 +00:00
/**
2011-11-03 10:53:06 +00:00
* Form constructor for the main block administration form .
2010-07-24 17:53:55 +00:00
*
* @ param $blocks
2010-08-30 00:22:03 +00:00
* An array of blocks , as returned by block_admin_display_prepare_blocks () .
2010-07-24 17:53:55 +00:00
* @ param $theme
* A string representing the name of the theme to edit blocks for .
2010-08-30 00:22:03 +00:00
* @ 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 .
2010-07-24 17:53:55 +00:00
*
* @ ingroup forms
2010-08-30 00:22:03 +00:00
* @ see block_admin_display_form_submit ()
2007-07-16 06:40:25 +00:00
*/
2010-08-30 00:22:03 +00:00
function block_admin_display_form ( $form , & $form_state , $blocks , $theme , $block_regions = NULL ) {
2012-02-08 14:49:23 +00:00
$path = drupal_get_path ( 'module' , 'block' );
$form [ '#attached' ][ 'css' ][] = $path . '/block.admin.css' ;
$form [ '#attached' ][ 'js' ][] = 'core/misc/tableheader.js' ;
$form [ '#attached' ][ 'js' ][] = $path . '/block.js' ;
2007-07-16 06:40:25 +00:00
2010-08-30 00:22:03 +00:00
// Get a list of block regions if one was not provided.
if ( ! isset ( $block_regions )) {
$block_regions = system_region_list ( $theme , REGIONS_VISIBLE );
}
2012-02-08 14:49:23 +00:00
// Add a last region for disabled blocks.
$block_regions_with_disabled = $block_regions + array ( BLOCK_REGION_NONE => BLOCK_REGION_NONE );
foreach ( $block_regions_with_disabled as $region => $title ) {
$form [ '#attached' ][ 'drupal_add_tabledrag' ][] = array ( 'blocks' , 'match' , 'sibling' , 'block-region-select' , 'block-region-' . $region , NULL , FALSE );
$form [ '#attached' ][ 'drupal_add_tabledrag' ][] = array ( 'blocks' , 'order' , 'sibling' , 'block-weight' , 'block-weight-' . $region );
}
2010-08-30 00:22:03 +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.
2010-08-30 00:22:03 +00:00
$form [ 'edited_theme' ] = array (
'#type' => 'value' ,
'#value' => $theme ,
);
$form [ 'block_regions' ] = array (
'#type' => 'value' ,
2012-02-08 14:49:23 +00:00
'#value' => $block_regions_with_disabled ,
2010-08-30 00:22:03 +00:00
);
$form [ 'blocks' ] = array ();
2009-09-18 00:12:48 +00:00
$form [ '#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' ];
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'module' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'value' ,
'#value' => $block [ 'module' ],
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'delta' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'value' ,
'#value' => $block [ 'delta' ],
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'info' ] = array (
2008-07-16 21:59:29 +00:00
'#markup' => check_plain ( $block [ 'info' ]),
2007-10-05 09:35:09 +00:00
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'theme' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'hidden' ,
2009-10-14 02:13:15 +00:00
'#value' => $theme ,
2007-10-05 09:35:09 +00:00
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'weight' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'weight' ,
'#default_value' => $block [ 'weight' ],
2008-11-22 10:53:20 +00:00
'#delta' => $weight_delta ,
2010-06-20 17:34:51 +00:00
'#title_display' => 'invisible' ,
2010-09-24 21:36:22 +00:00
'#title' => t ( 'Weight for @block block' , array ( '@block' => $block [ 'info' ])),
2007-10-05 09:35:09 +00:00
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'region' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'select' ,
2010-09-24 21:36:22 +00:00
'#default_value' => $block [ 'region' ] != BLOCK_REGION_NONE ? $block [ 'region' ] : NULL ,
'#empty_value' => BLOCK_REGION_NONE ,
2010-09-17 13:32:56 +00:00
'#title_display' => 'invisible' ,
2010-09-24 21:36:22 +00:00
'#title' => t ( 'Region for @block block' , array ( '@block' => $block [ 'info' ])),
2007-07-16 06:40:25 +00:00
'#options' => $block_regions ,
);
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'configure' ] = array (
2009-11-03 05:27:18 +00:00
'#type' => 'link' ,
'#title' => t ( 'configure' ),
'#href' => 'admin/structure/block/manage/' . $block [ 'module' ] . '/' . $block [ 'delta' ] . '/configure' ,
2008-05-15 21:30:02 +00:00
);
2007-07-16 06:40:25 +00:00
if ( $block [ 'module' ] == 'block' ) {
2010-08-30 00:22:03 +00:00
$form [ 'blocks' ][ $key ][ 'delete' ] = array (
2009-11-03 05:27:18 +00:00
'#type' => 'link' ,
'#title' => t ( 'delete' ),
'#href' => 'admin/structure/block/manage/' . $block [ 'module' ] . '/' . $block [ 'delta' ] . '/delete' ,
);
2007-07-16 06:40:25 +00:00
}
}
2010-10-10 20:11:21 +00:00
// 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 ;
}
2007-10-05 09:35:09 +00:00
2010-01-03 21:01:04 +00:00
$form [ 'actions' ] = array (
'#tree' => FALSE ,
2010-04-24 14:49:14 +00:00
'#type' => 'actions' ,
2010-01-03 21:01:04 +00:00
);
$form [ 'actions' ][ 'submit' ] = array (
2007-10-05 09:35:09 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Save blocks' ),
);
2007-07-16 06:40:25 +00:00
return $form ;
}
/**
2011-11-03 10:53:06 +00:00
* Form submission handler for block_admin_display_form () .
2010-07-24 17:53:55 +00:00
*
* @ see block_admin_display_form ()
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 ) {
2011-06-09 05:34:15 +00:00
$transaction = db_transaction ();
try {
foreach ( $form_state [ 'values' ][ 'blocks' ] as $block ) {
$block [ 'status' ] = ( int ) ( $block [ 'region' ] != BLOCK_REGION_NONE );
$block [ 'region' ] = $block [ 'status' ] ? $block [ 'region' ] : '' ;
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 ();
}
}
catch ( Exception $e ) {
$transaction -> rollback ();
watchdog_exception ( 'block' , $e );
throw $e ;
2007-07-16 06:40:25 +00:00
}
drupal_set_message ( t ( 'The block settings have been updated.' ));
2012-06-13 01:37:07 +00:00
cache_invalidate ( array ( 'content' => TRUE ));
2007-07-16 06:40:25 +00:00
}
/**
2011-11-03 10:53:06 +00:00
* Sorts active blocks by region , then by weight ; sorts inactive blocks by name .
2007-07-16 06:40:25 +00:00
*
2011-11-03 10:53:06 +00:00
* Callback for usort () in block_admin_display_prepare_blocks () .
2007-07-16 06:40:25 +00:00
*/
function _block_compare ( $a , $b ) {
2007-11-14 09:50:00 +00:00
global $theme_key ;
2009-12-07 21:16:31 +00:00
2009-10-14 02:13:15 +00:00
// 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 ;
}
2009-12-07 21:16:31 +00:00
2009-10-14 02:13:15 +00:00
$regions = & drupal_static ( __FUNCTION__ . ':regions' );
2007-11-14 09:50:00 +00:00
// We need the region list to correctly order by region.
if ( ! isset ( $regions )) {
2009-10-14 02:13:15 +00:00
$regions = array_flip ( array_keys ( system_region_list ( $theme )));
2007-11-14 09:50:00 +00:00
$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
}
2010-05-04 15:29:56 +00:00
// Sort by weight, unless disabled.
if ( $a [ 'region' ] != BLOCK_REGION_NONE ) {
$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
}
/**
2011-11-30 03:40:49 +00:00
* Form constructor for the block configuration form .
2011-11-03 10:53:06 +00:00
*
2010-07-24 17:53:55 +00:00
* 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 .
*
2011-11-03 10:53:06 +00:00
* @ see block_menu ()
2010-07-24 17:53:55 +00:00
* @ see block_admin_configure_validate ()
* @ see block_admin_configure_submit ()
* @ ingroup forms
2007-07-16 06:40:25 +00:00
*/
2009-11-11 08:28:50 +00:00
function block_admin_configure ( $form , & $form_state , $module , $delta ) {
$block = block_load ( $module , $delta );
2008-05-15 21:30:02 +00:00
$form [ 'module' ] = array (
'#type' => 'value' ,
2009-10-16 23:48:38 +00:00
'#value' => $block -> module ,
2008-05-15 21:30:02 +00:00
);
$form [ 'delta' ] = array (
'#type' => 'value' ,
2009-10-16 23:48:38 +00:00
'#value' => $block -> delta ,
2008-05-15 21:30:02 +00:00
);
2007-07-16 06:40:25 +00:00
2009-12-07 21:16:31 +00:00
// 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 (
2007-07-16 06:40:25 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Block title' ),
'#maxlength' => 64 ,
2010-01-10 16:49:37 +00:00
'#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' => '<none>' )),
2009-10-16 23:48:38 +00:00
'#default_value' => isset ( $block -> title ) ? $block -> title : '' ,
2011-09-11 21:06:35 +00:00
'#weight' => - 19 ,
2007-07-16 06:40:25 +00:00
);
2009-12-07 21:16:31 +00:00
// 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.
2009-08-26 10:10:01 +00:00
$form [ 'regions' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Region settings' ),
2009-12-07 21:16:31 +00:00
'#collapsible' => FALSE ,
'#description' => t ( 'Specify in which themes and regions this block is displayed.' ),
2009-08-26 10:10:01 +00:00
'#tree' => TRUE ,
);
2011-12-13 03:29:45 +00:00
$theme_default = variable_get ( 'theme_default' , 'stark' );
2010-11-06 23:24:33 +00:00
$admin_theme = variable_get ( 'admin_theme' );
2009-10-14 02:13:15 +00:00
foreach ( list_themes () as $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 (
2009-10-16 23:48:38 +00:00
':module' => $block -> module ,
':delta' => $block -> delta ,
2009-10-14 02:13:15 +00:00
':theme' => $key ,
2009-08-26 10:10:01 +00:00
)) -> fetchField ();
2009-12-07 21:16:31 +00:00
2010-11-06 23:24:33 +00:00
// 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 ));
}
2009-10-14 02:13:15 +00:00
$form [ 'regions' ][ $key ] = array (
2009-08-26 10:10:01 +00:00
'#type' => 'select' ,
2010-11-06 23:24:33 +00:00
'#title' => $theme_title ,
2010-09-24 21:36:22 +00:00
'#default_value' => ! empty ( $region ) && $region != - 1 ? $region : NULL ,
'#empty_value' => BLOCK_REGION_NONE ,
'#options' => system_region_list ( $key , REGIONS_VISIBLE ),
2009-10-14 02:13:15 +00:00
'#weight' => ( $key == $theme_default ? 9 : 10 ),
2009-08-26 10:10:01 +00:00
);
}
}
2009-12-07 21:16:31 +00:00
// 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' ),
),
);
2007-07-16 06:40:25 +00:00
2009-12-07 21:16:31 +00:00
// Per-path visibility.
$form [ 'visibility' ][ 'path' ] = array (
2007-07-16 06:40:25 +00:00
'#type' => 'fieldset' ,
2009-12-07 21:16:31 +00:00
'#title' => t ( 'Pages' ),
2007-07-16 06:40:25 +00:00
'#collapsible' => TRUE ,
2008-11-22 10:27:25 +00:00
'#collapsed' => TRUE ,
2009-12-14 13:32:53 +00:00
'#group' => 'visibility' ,
2010-01-15 10:59:21 +00:00
'#weight' => 0 ,
2007-07-16 06:40:25 +00:00
);
2009-05-07 15:29:08 +00:00
$access = user_access ( 'use PHP for settings' );
2010-06-25 20:25:54 +00:00
if ( isset ( $block -> visibility ) && $block -> visibility == BLOCK_VISIBILITY_PHP && ! $access ) {
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'path' ][ 'visibility' ] = array (
'#type' => 'value' ,
2010-06-25 20:25:54 +00:00
'#value' => BLOCK_VISIBILITY_PHP ,
2009-12-07 21:16:31 +00:00
);
$form [ 'visibility' ][ 'path' ][ 'pages' ] = array (
2009-10-16 23:48:38 +00:00
'#type' => 'value' ,
'#value' => isset ( $block -> pages ) ? $block -> pages : '' ,
);
2007-07-16 06:40:25 +00:00
}
else {
2009-12-07 21:16:31 +00:00
$options = array (
2010-06-25 20:25:54 +00:00
BLOCK_VISIBILITY_NOTLISTED => t ( 'All pages except those listed' ),
BLOCK_VISIBILITY_LISTED => t ( 'Only the listed pages' ),
2009-12-07 21:16:31 +00:00
);
2011-08-29 13:08:58 +00:00
$description = t ( " Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %user for the current user's page and %user-wildcard for every user page. %front is the front page. " , array ( '%user' => 'user' , '%user-wildcard' => 'user/*' , '%front' => '<front>' ));
2007-07-16 06:40:25 +00:00
2009-05-07 15:29:08 +00:00
if ( module_exists ( 'php' ) && $access ) {
2010-06-25 20:25:54 +00:00
$options += array ( BLOCK_VISIBILITY_PHP => t ( 'Pages on which this PHP code returns <code>TRUE</code> (experts only)' ));
2009-12-13 18:19:01 +00:00
$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' );
2007-07-16 06:40:25 +00:00
}
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'path' ][ 'visibility' ] = array (
2007-07-16 06:40:25 +00:00
'#type' => 'radios' ,
'#title' => t ( 'Show block on specific pages' ),
'#options' => $options ,
2010-06-25 20:25:54 +00:00
'#default_value' => isset ( $block -> visibility ) ? $block -> visibility : BLOCK_VISIBILITY_NOTLISTED ,
2007-07-16 06:40:25 +00:00
);
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'path' ][ 'pages' ] = array (
2007-07-16 06:40:25 +00:00
'#type' => 'textarea' ,
2009-12-13 18:19:01 +00:00
'#title' => '<span class="element-invisible">' . $title . '</span>' ,
2009-10-16 23:48:38 +00:00
'#default_value' => isset ( $block -> pages ) ? $block -> pages : '' ,
2007-07-16 06:40:25 +00:00
'#description' => $description ,
);
}
2012-04-19 17:45:46 +00:00
// Configure the block visibility per language.
if ( module_exists ( 'language' ) && language_multilingual ()) {
$configurable_language_types = language_types_get_configurable ();
$existing_language_settings = db_query ( " SELECT type, langcode FROM { block_language} WHERE module = :module AND delta = :delta " , array (
':module' => $form [ 'module' ][ '#value' ],
':delta' => $form [ 'delta' ][ '#value' ],
)) -> fetchAll ();
$default_langcode_options = array ();
$default_language_type = $configurable_language_types [ 0 ];
foreach ( $existing_language_settings as $setting ) {
$default_langcode_options [] = $setting -> langcode ;
// Overwrite default language type if we have it set. Although this
// theoretically would allow per language type association, our UI
// only allows language type association overall for a block, so we
// only need a single value.
$default_language_type = $setting -> type ;
}
2012-06-15 17:03:15 +00:00
// Fetch languages.
$languages = language_list ( LANGUAGE_ALL );
foreach ( $languages as $language ) {
2012-04-19 17:45:46 +00:00
// @TODO $language->name is not wrapped with t(), it should be replaced
// by CMI translation implementation.
$langcodes_options [ $language -> langcode ] = $language -> name ;
}
$form [ 'visibility' ][ 'language' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Languages' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
'#group' => 'visibility' ,
'#weight' => 5 ,
);
// If there are multiple configurable language types, let the user pick
// which one should be applied to this visibility setting. This way users
// can limit blocks by interface language or content language for exmaple.
$language_types = language_types_info ();
$language_type_options = array ();
foreach ( $configurable_language_types as $type_key ) {
$language_type_options [ $type_key ] = $language_types [ $type_key ][ 'name' ];
}
$form [ 'visibility' ][ 'language' ][ 'language_type' ] = array (
'#type' => 'radios' ,
'#title' => t ( 'Language type' ),
'#options' => $language_type_options ,
'#default_value' => $default_language_type ,
'#access' => count ( $language_type_options ) > 1 ,
);
$form [ 'visibility' ][ 'language' ][ 'langcodes' ] = array (
'#type' => 'checkboxes' ,
'#title' => t ( 'Show this block only for specific languages' ),
'#default_value' => $default_langcode_options ,
'#options' => $langcodes_options ,
'#description' => t ( 'Show this block only for the selected language(s). If you select no languages, the block will be visibile in all languages.' ),
);
}
2009-12-07 21:16:31 +00:00
// Per-role visibility.
$default_role_options = db_query ( " SELECT rid FROM { block_role} WHERE module = :module AND delta = :delta " , array (
2009-10-16 23:48:38 +00:00
':module' => $block -> module ,
':delta' => $block -> delta ,
2009-07-01 08:04:19 +00:00
)) -> fetchCol ();
2010-04-23 05:39:43 +00:00
$role_options = array_map ( 'check_plain' , user_roles ());
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'role' ] = array (
2009-07-01 08:04:19 +00:00
'#type' => 'fieldset' ,
2009-12-07 21:16:31 +00:00
'#title' => t ( 'Roles' ),
2009-07-01 08:04:19 +00:00
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-12-14 13:32:53 +00:00
'#group' => 'visibility' ,
2010-01-15 10:59:21 +00:00
'#weight' => 10 ,
2009-07-01 08:04:19 +00:00
);
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'role' ][ 'roles' ] = array (
2009-07-01 08:04:19 +00:00
'#type' => 'checkboxes' ,
2009-12-07 21:16:31 +00:00
'#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
);
2009-12-07 21:16:31 +00:00
// Per-user visibility.
$form [ 'visibility' ][ 'user' ] = array (
2008-11-22 10:27:25 +00:00
'#type' => 'fieldset' ,
2009-12-07 21:16:31 +00:00
'#title' => t ( 'Users' ),
2008-11-22 10:27:25 +00:00
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-12-14 13:32:53 +00:00
'#group' => 'visibility' ,
2010-01-15 10:59:21 +00:00
'#weight' => 20 ,
2008-11-22 10:27:25 +00:00
);
2009-12-07 21:16:31 +00:00
$form [ 'visibility' ][ 'user' ][ 'custom' ] = array (
2008-11-22 10:27:25 +00:00
'#type' => 'radios' ,
2009-12-07 21:16:31 +00:00
'#title' => t ( 'Customizable per user' ),
2008-11-22 10:27:25 +00:00
'#options' => array (
2010-06-25 20:25:54 +00:00
BLOCK_CUSTOM_FIXED => t ( 'Not customizable' ),
BLOCK_CUSTOM_ENABLED => t ( 'Customizable, visible by default' ),
BLOCK_CUSTOM_DISABLED => t ( 'Customizable, hidden by default' ),
2008-11-22 10:27:25 +00:00
),
'#description' => t ( 'Allow individual users to customize the visibility of this block in their account settings.' ),
2010-06-25 20:25:54 +00:00
'#default_value' => isset ( $block -> custom ) ? $block -> custom : BLOCK_CUSTOM_FIXED ,
2008-11-22 10:27:25 +00:00
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2007-07-16 06:40:25 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Save block' ),
);
return $form ;
}
2010-07-24 17:53:55 +00:00
/**
2011-11-03 10:53:06 +00:00
* Form validation handler for block_admin_configure () .
2010-07-24 17:53:55 +00:00
*
* @ see block_admin_configure ()
* @ see block_admin_configure_submit ()
*/
2007-07-16 06:40:25 +00:00
function block_admin_configure_validate ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'module' ] == 'block' ) {
2009-09-18 00:04:24 +00:00
$custom_block_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {block_custom} WHERE bid <> :bid AND info = :info' , 0 , 1 , array (
2009-05-28 11:31:20 +00:00
':bid' => $form_state [ 'values' ][ 'delta' ],
':info' => $form_state [ 'values' ][ 'info' ],
2009-09-18 00:04:24 +00:00
)) -> fetchField ();
2009-08-28 19:44:05 +00:00
if ( empty ( $form_state [ 'values' ][ 'info' ]) || $custom_block_exists ) {
2010-01-09 23:03:22 +00:00
form_set_error ( 'info' , t ( 'Ensure that each block description is unique.' ));
2007-07-16 06:40:25 +00:00
}
}
}
2010-07-24 17:53:55 +00:00
/**
2011-11-03 10:53:06 +00:00
* Form submission handler for block_admin_configure () .
2010-07-24 17:53:55 +00:00
*
* @ see block_admin_configure ()
* @ see block_admin_configure_validate ()
*/
2007-07-16 06:40:25 +00:00
function block_admin_configure_submit ( $form , & $form_state ) {
if ( ! form_get_errors ()) {
2011-06-09 05:34:15 +00:00
$transaction = db_transaction ();
try {
db_update ( 'block' )
2009-08-26 10:10:01 +00:00
-> fields ( array (
2011-06-09 05:34:15 +00:00
'visibility' => ( int ) $form_state [ 'values' ][ 'visibility' ],
2009-08-26 10:10:01 +00:00
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
2011-06-09 05:34:15 +00:00
'custom' => ( int ) $form_state [ 'values' ][ 'custom' ],
'title' => $form_state [ 'values' ][ 'title' ],
2009-08-26 10:10:01 +00:00
))
2011-06-09 05:34:15 +00:00
-> condition ( 'module' , $form_state [ 'values' ][ 'module' ])
-> condition ( 'delta' , $form_state [ 'values' ][ 'delta' ])
2009-08-26 10:10:01 +00:00
-> execute ();
2011-06-09 05:34:15 +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' ));
foreach ( array_filter ( $form_state [ 'values' ][ 'roles' ]) as $rid ) {
$query -> values ( array (
'rid' => $rid ,
'module' => $form_state [ 'values' ][ 'module' ],
'delta' => $form_state [ 'values' ][ 'delta' ],
));
}
$query -> execute ();
2012-04-19 17:45:46 +00:00
// Store regions per theme for this block.
2011-06-09 05:34:15 +00:00
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 == BLOCK_REGION_NONE ? '' : $region ),
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
'status' => ( int ) ( $region != BLOCK_REGION_NONE ),
))
-> execute ();
}
2012-04-19 17:45:46 +00:00
// Update the block visibility settings if we have settings to store
// for the existing languages.
if ( module_exists ( 'language' ) && isset ( $form_state [ 'values' ][ 'langcodes' ])) {
db_delete ( 'block_language' )
-> condition ( 'module' , $form_state [ 'values' ][ 'module' ])
-> condition ( 'delta' , $form_state [ 'values' ][ 'delta' ])
-> execute ();
$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 ();
}
2011-06-09 05:34:15 +00:00
module_invoke ( $form_state [ 'values' ][ 'module' ], 'block_save' , $form_state [ 'values' ][ 'delta' ], $form_state [ 'values' ]);
}
catch ( Exception $e ) {
$transaction -> rollback ();
watchdog_exception ( 'block' , $e );
throw $e ;
}
2007-07-16 06:40:25 +00:00
drupal_set_message ( t ( 'The block configuration has been saved.' ));
2012-06-13 01:37:07 +00:00
cache_invalidate ( array ( 'content' => TRUE ));
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/block' ;
2007-07-16 06:40:25 +00:00
}
}
/**
2011-11-30 03:40:49 +00:00
* Form constructor for the add block form .
2011-11-03 10:53:06 +00:00
*
* @ see block_menu ()
2010-07-24 17:53:55 +00:00
* @ see block_add_block_form_validate ()
* @ see block_add_block_form_submit ()
* @ ingroup forms
2007-07-16 06:40:25 +00:00
*/
2009-09-18 00:12:48 +00:00
function block_add_block_form ( $form , & $form_state ) {
2009-11-11 08:28:50 +00:00
return block_admin_configure ( $form , $form_state , 'block' , NULL );
2007-07-16 06:40:25 +00:00
}
2010-07-24 17:53:55 +00:00
/**
2011-11-03 10:53:06 +00:00
* Form validation handler for block_add_block_form () .
2010-07-24 17:53:55 +00:00
*
* @ see block_add_block_form ()
* @ see block_add_block_form_submit ()
*/
2007-07-16 06:40:25 +00:00
function block_add_block_form_validate ( $form , & $form_state ) {
2009-09-18 00:04:24 +00:00
$custom_block_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {block_custom} WHERE info = :info' , 0 , 1 , array ( ':info' => $form_state [ 'values' ][ 'info' ])) -> 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 ) {
2010-01-09 23:03:22 +00:00
form_set_error ( 'info' , t ( 'Ensure that each block description is unique.' ));
2007-07-16 06:40:25 +00:00
}
}
/**
2011-11-03 10:53:06 +00:00
* Form submission handler for block_add_block_form () .
2010-07-24 17:53:55 +00:00
*
* Saves the new custom block .
*
* @ see block_add_block_form ()
* @ see block_add_block_form_validate ()
2007-07-16 06:40:25 +00:00
*/
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 (
2010-05-13 07:53:02 +00:00
'body' => $form_state [ 'values' ][ 'body' ][ 'value' ],
2009-05-28 11:31:20 +00:00
'info' => $form_state [ 'values' ][ 'info' ],
2010-05-13 07:53:02 +00:00
'format' => $form_state [ 'values' ][ 'body' ][ 'format' ],
2009-05-28 11:31:20 +00:00
))
-> execute ();
2010-03-28 11:16:29 +00:00
// Store block delta to allow other modules to work with new block.
$form_state [ 'values' ][ 'delta' ] = $delta ;
2009-05-28 11:31:20 +00:00
$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-12-07 21:16:31 +00:00
'title' => $form_state [ 'values' ][ 'title' ],
2009-05-28 11:31:20 +00:00
'module' => $form_state [ 'values' ][ 'module' ],
2009-12-07 21:16:31 +00:00
'theme' => $theme -> name ,
2009-05-28 11:31:20 +00:00
'status' => 0 ,
'weight' => 0 ,
2009-12-07 21:16:31 +00:00
'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
2012-04-19 17:45:46 +00:00
// Store regions per theme for this block.
2009-08-27 19:40:36 +00:00
foreach ( $form_state [ 'values' ][ 'regions' ] as $theme => $region ) {
db_merge ( 'block' )
-> key ( array ( 'theme' => $theme , 'delta' => $delta , 'module' => $form_state [ 'values' ][ 'module' ]))
-> fields ( array (
2009-12-02 19:39:05 +00:00
'region' => ( $region == BLOCK_REGION_NONE ? '' : $region ),
2009-08-27 19:40:36 +00:00
'pages' => trim ( $form_state [ 'values' ][ 'pages' ]),
'status' => ( int ) ( $region != BLOCK_REGION_NONE ),
))
-> execute ();
}
2009-07-01 08:04:19 +00:00
2012-04-19 17:45:46 +00:00
// 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 ();
}
2007-07-16 06:40:25 +00:00
drupal_set_message ( t ( 'The block has been created.' ));
2012-06-13 01:37:07 +00:00
cache_invalidate ( array ( 'content' => TRUE ));
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/block' ;
2007-07-16 06:40:25 +00:00
}
/**
2011-11-30 03:40:49 +00:00
* Form constructor for the custom block deletion form .
2011-11-03 10:53:06 +00:00
*
2010-07-24 17:53:55 +00:00
* @ 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 .
*
2011-11-03 10:53:06 +00:00
* @ see block_menu ()
2010-07-24 17:53:55 +00:00
* @ see block_custom_block_delete_submit ()
2007-07-16 06:40:25 +00:00
*/
2009-11-11 08:28:50 +00:00
function block_custom_block_delete ( $form , & $form_state , $module , $delta ) {
$block = block_load ( $module , $delta );
2009-10-16 23:48:38 +00:00
$custom_block = block_custom_block_get ( $block -> delta );
2009-08-28 19:44:05 +00:00
$form [ 'info' ] = array ( '#type' => 'hidden' , '#value' => $custom_block [ 'info' ] ? $custom_block [ 'info' ] : $custom_block [ 'title' ]);
2009-10-16 23:48:38 +00:00
$form [ 'bid' ] = array ( '#type' => 'hidden' , '#value' => $block -> delta );
2007-07-16 06:40:25 +00:00
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
}
/**
2011-11-03 10:53:06 +00:00
* Form submission handler for block_custom_block_delete () .
2010-07-24 17:53:55 +00:00
*
* @ see block_custom_block_delete ()
2007-07-16 06:40:25 +00:00
*/
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 ();
2010-03-09 12:09:52 +00:00
db_delete ( 'block_role' )
-> condition ( 'module' , 'block' )
-> condition ( 'delta' , $form_state [ 'values' ][ 'bid' ])
-> execute ();
2012-04-19 17:45:46 +00:00
db_delete ( 'block_language' )
-> 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' ])));
2012-06-13 01:37:07 +00:00
cache_invalidate ( array ( 'content' => TRUE ));
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
/**
2010-08-30 00:22:03 +00:00
* Processes variables for block - admin - display - form . tpl . php .
2007-09-01 05:31:09 +00:00
*
* 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 ) {
2010-08-30 00:22:03 +00:00
$variables [ 'block_regions' ] = $variables [ 'form' ][ 'block_regions' ][ '#value' ];
if ( isset ( $variables [ 'block_regions' ][ BLOCK_REGION_NONE ])) {
$variables [ 'block_regions' ][ BLOCK_REGION_NONE ] = t ( 'Disabled' );
}
2007-09-01 05:31:09 +00:00
2010-08-30 00:22:03 +00:00
foreach ( $variables [ '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 ();
2010-08-30 00:22:03 +00:00
// 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.
2010-09-24 21:36:22 +00:00
$region = ( isset ( $block [ 'region' ][ '#default_value' ]) ? $block [ 'region' ][ '#default_value' ] : BLOCK_REGION_NONE );
2010-08-30 00:22:03 +00:00
// 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 ;
2007-09-01 05:31:09 +00:00
}
2009-02-03 18:55:32 +00:00
$variables [ 'form_submit' ] = drupal_render_children ( $variables [ 'form' ]);
2007-09-01 05:31:09 +00:00
}