Warning message

Newsletter category Drupal Developer newsletter format has not been set to HTML. There will be no statistics recorded for this newsletter.

Drupal: Set content in a region programmatically

This CodeLet will allow you to set some text in a region you specify from configuration page. The regions are from the current theme. The desired content will be displayed in the region when the page is rendered.

Module Info

name = SEO Custom utility
description = SEO custom Utility module.
core = 7.x

CodeLet: 

/**
* @file
*
* Provide an administrator interface for adding SEO text & images for specific page
*
* DrupalD
*/

/**
* Implementation of hook_menu
*
* DrupalD
*/
function seo_util_menu() {
$item['admin/config/development/seo-util'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => t('SEO text'),
'page callback' => 'seo_util_overview',
'access arguments' => array('Administer site configuration')
);

$item['admin/config/development/seo-util/manage'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => t('Manage text'),
'page callback' => 'seo_util_overview',
'access arguments' => array('Administer site configuration'),
'weight' => -10,
);

$item['admin/config/development/seo-util/config'] = array(
'type' => MENU_LOCAL_TASK,
'title' => t('Settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('seo_util_config'),
'access arguments' => array('Administer site configuration'),
'weight' => -7,
);

return $item;
}

/**
* seo_util_overview
*
* Display SEO text overview
*
* DrupalD
*/
function seo_util_overview() {
drupal_add_css(drupal_get_path('module', 'seo_util') .'/seo_util.css');
$am__overview = drupal_get_form('seo_util_global_text');
$output = '

'. drupal_render($am__overview);
$output .= '
'. _get_config_summary() .'

';
return $output;

}

/**
* seo_util_global_text
*
* DrupalD
*/
function seo_util_global_text() {
$form['global_text'] = array(
'#type' => 'textarea',
'#title' => t('Global text'),
'#description' => t('Set the text for all pages. This will be overwritten
by the individual page text. You can use PHP code within @php.
You will need to enable PHP filter module to do this.',
array('@php' => '')),
'#default_value' => variable_get('global_text', ''),
'#weight' => -50,
);

return system_settings_form($form);
}

/**
* seo_util_config
*
* Select the vocabulary for which text to be added
*
* DrupalD
*/
function seo_util_config() {
$am__vocabulary = taxonomy_vocabulary_get_names();
$am__vocab = array();
$am__vocab[0] = t('-- Select --');
foreach ($am__vocabulary as $vocabulary => $ob) {
$am__vocab[$ob->vid] = $ob->name;
}

$form['seo_util_province_vocabulary'] = array(
'#type' => 'select',
'#title' => t('Province Vocabulary'),
'#description' => t('Select a vocabulary which provides a list of
provice & cities'),
'#options' => $am__vocab,
'#required' => TRUE,
'#default_value' => variable_get('seo_util_province_vocabulary',00),
'#weight' => -10,
);

$form['seo_util_course_vocabulary'] = array(
'#type' => 'select',
'#title' => t('Course Vocabulary'),
'#description' => t('Select a vocabulary which provides a list of courses'),
'#options' => $am__vocab,
'#required' => TRUE,
'#default_value' => variable_get('seo_util_course_vocabulary',00),
'#weight' => -9,
);

$form['seo_util_theme_regions'] = array(
'#type' => 'select',
'#title' => t('Theme regions'),
'#description' => t('Select the region where you want to display the
text for each of the pages/path') .'
'.
t('The regions are listed from your default theme. Your default
theme is @theme.', array('@theme' => variable_get('theme_default', ''))),
'#options' => system_region_list(variable_get('theme_default', '')),
'#required' => TRUE,
'#default_value' => variable_get('seo_util_theme_regions', ''),
);

return system_settings_form($form);
}

/**
* Implementation of hook_page_build
*
* @param $page
* DrupalD
*/
function seo_util_page_build(&$page) {
$ss__global_text = variable_get('global_text', '');
$ss__region = variable_get('seo_util_theme_regions', '');
$page[$ss__region]['text']['#markup'] = php_eval($ss__global_text);
}

/**
* _get_config_summary
*
* DrupalD
*/
function _get_config_summary() {
$output = '

'. t('Configuration summary') .'

';

$output .= '

    ';
    $output .= '
  • '. t('Province/City taxonomy')
    .': '. _get_vocabulary(variable_get('seo_util_province_vocabulary', '--')) .'
  • ';
    $output .= '

  • '. t('Course taxonomy')
    .': '. _get_vocabulary(variable_get('seo_util_course_vocabulary', '--')) .'
  • ';
    $output .= '

  • '. t('Default theme region') .': '.
    variable_get('seo_util_theme_regions', '--') .'
  • ';
    $output .= '

  • '. t('Default theme') .': '.
    variable_get('theme_default', '--') .'
  • ';
    $output .= '

';
$output .= t('You can change above configuration from !config page',
array('!config' => l(t('Settings'), 'admin/config/development/seo-util/config')));
return $output;
}

/**
* _get_vocabulary
*
* Enter description here ...
* @param unknown_type $sn__vid
* DrupalD
*/
function _get_vocabulary($sn__vid) {
$names = db_query('SELECT name FROM {taxonomy_vocabulary} WHERE vid = :vid',
array(':vid' => $sn__vid))->fetchAllAssoc('name');
$names = array_values($names);

return $names[0]->name;
}
?>

Drupal: Set content in a region programmatically | Configuration

By DrupalD on Thu, 02/01/2014 - 5:29pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback