Skip to content

Instantly share code, notes, and snippets.

@jackfoust
Last active August 29, 2015 14:08
Show Gist options
  • Save jackfoust/344c5a81a3ef7061b87e to your computer and use it in GitHub Desktop.
Save jackfoust/344c5a81a3ef7061b87e to your computer and use it in GitHub Desktop.
#drupal Form and Block Render to display site alert message. Drupal 7
<?php
/**
* @file
* Create form for alert message entry and render a block with message if message is not empty. Blocked place in header in this example with custom CSS.
*/
/**
* Implements hook_menu().
*/
function example_alert_blocks_menu() {
$items = array();
$items['admin/alert-message'] = array(
'title' => 'Configure Alert Message',
'description' => '',
'position' => 'left',
'weight' => 100,
'page callback' => 'drupal_get_form',
'page arguments' => array('example_alert_blocks_config'),
'access arguments' => array('access content overview'),
);
return $items;
}
function example_alert_blocks_config() {
$form = array();
$form['example_alert_message'] = array(
'#type' => 'textfield',
'#title' => t('Alert Message'),
'#default_value' => variable_get('example_alert_message', ''),
'#maxlength' => 200,
'#size' => 200,
'#description' => t('Leave blank for none.'),
);
$form = system_settings_form($form);
$form['#submit'][] = 'example_alert_blocks_cc';
return $form;
}
function example_alert_blocks_cc() {
drupal_flush_all_caches();
drupal_set_message(t('Caches cleared.'));
}
/**
* Implements hook_block_info().
*/
function example_alert_blocks_block_info() {
$blocks = array();
$blocks['example_alert_block'] = array('info' => t('Alert Notification'), 'cache' => DRUPAL_NO_CACHE);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function example_alert_blocks_block_view($block_name = '') {
if ($block_name == 'example_alert_block') {
$alert_message = variable_get('example_alert_message', '');
if (empty($alert_message)) {
$block = array();
}
else {
$block = array(
'subject' => t('<NONE>'),
'content' => '<p>' . variable_get('example_alert_message', '') . '</p>',
);
}
return $block;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment