function hook_block_save

You are here

7 block.api.php hook_block_save($delta = '', $edit = array())

Save the configuration options from hook_block_configure().

This hook allows you to save the block-specific configuration settings defined within your hook_block_configure().

Parameters

$delta: Which block is being configured. This is a unique identifier for the block within the module, defined in hook_block_info().

$edit: The submitted form data from the configuration form.

For a detailed usage example, see block_example.module.

See also

hook_block_configure()

hook_block_info()

Related topics

11 functions implement hook_block_save()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_block_save in modules/aggregator/aggregator.module
Implements hook_block_save().
block_block_save in modules/block/block.module
Implements hook_block_save().
block_custom_block_save in modules/block/block.module
Saves a user-created block in the database.
blog_block_save in modules/blog/blog.module
Implements hook_block_save().
book_block_save in modules/book/book.module
Implements hook_block_save().

... See full list

2 invocations of hook_block_save()
block_admin_configure_submit in modules/block/block.admin.inc
Form submission handler for block_admin_configure().
StatisticsReportsTestCase::testPopularContentBlock in modules/statistics/statistics.test
Tests the "popular content" block.

File

modules/block/block.api.php, line 188
Hooks provided by the Block module.

Code

function hook_block_save($delta = '', $edit = array()) {
  // This example comes from node.module.
  if ($delta == 'recent') {
    variable_set('node_recent_block_count', $edit['node_recent_block_count']);
  }
}

Comments

<?php
/**
* Implements hook_block_save()
*/
function mymodule_block_save($delta) {
 
  if (
$delta == 'myblock_delta') {
   
   
$block = block_load('mymodule','myblock_delta');
   
   
// Get the previous fid of the file
   
$prev_fid = variable_get('mymodule_myfile_fid', 0);
   
   
// Has the file a new value?
   
if ($prev_fid != $edit['mymodule_myfile_fid']) {
     
     
// File is being replaced or deleted?
     
if ($prev_fid > 0) {
       
// Delete the previous file
       
$file = file_load($prev_image_fid);
           
       
// Remove the reference to this file in the file_usage table
       
file_usage_delete($file, 'mymodule', 'block', $block->bid);
           
       
// Remove the file itself
       
file_delete($file);
      }
    }
   
   
variable_set('mymodule_myfile_fid', $edit['mymodule_myfile_fid']);
       
   
// If the new fid has a different value then the previous fid,
    // make a reference to this file in the file_usage table and
    // update its status to 'FILE_STATUS_PERMAMENT' to prevent it
    // from being deleted by cron.
   
if ($edit['mymodule_myfile_fid'] > 0 && $edit['mymodule_myfile_fid'] != $prev_fid) {
     
// Load the file
     
$file = file_load($edit['mymodule_myfile_fid']);

     

// Change the status from temporary to permanent
     
$file->status = FILE_STATUS_PERMANENT;
     
file_save($file);

     

// Add a reference to the file in the file_usage table
     
file_usage_add($file, 'mymodule', 'block', $block->bid);
    }    
  }
}
?>

I found a problem using this code: function block_load() returns only first result for query 'SELECT * FROM {block} WHERE module = :module AND delta = :delta' but this query can lead to multiple results because block table alternative key is defined as combination of fields "module", "delta" and "theme". Moreover this query is not ordered by any field so subsequent calls to block_load may return different block bid: this can lead to problem when removing file usage records.
In my case i solved using

<?php
 
global $theme_key;
 
$block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme', array(':module' => 'mytheme', ':delta' => 'myblock_delta', ':theme' => $theme_key))->fetchObject();
?>

instead of

<?php
$block
= block_load('mymodule','myblock_delta');
?>

note that this solution can lead to problems if you change the administration theme.