info文件:
name = Block More Link
description = Provide a more link for all blocks.
package = Other
version = VERSION
core = 7.x
configure = admin/config/block/morelink
install文件:
<?php
/**
* @file
* Install, update and uninstall functions for the block_morelink module.
*/
/**
* Implements hook_schema().
*/
function block_morelink_schema() {
$schema['block_morelink'] = array(
'description' => 'Stores more link path.',
'fields' => array(
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => "The block's origin module, from {block}.module.",
),
'delta' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => "The block's unique delta within module, from {block}.delta.",
),
'url' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => "The more link url of a block.",
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => "The more link title of a block.",
),
),
'primary key' => array('module', 'delta'),
'indexes' => array(
'url' => array('url'),
),
);
return $schema;
}
/**
* change 'primary key' to array('module', 'delta').
*/
function block_morelink_update_7000(&$sandbox) {
db_drop_primary_key('block_morelink');
db_add_primary_key('block_morelink', array('module', 'delta'));
}
module文件中的代码,你可以在drupal.org上下载最新的模块代码:
http://drupal.org/project/block_morelink
<?php
/**
* Implements hook_form_FORMID_alter().
*
* Adds more link path textfield to add block form.
*/
function block_morelink_form_block_add_block_form_alter(&$form, &$form_state) {
block_morelink_form_block_admin_configure_alter($form, $form_state);
}
function block_morelink_form_block_admin_configure_alter(&$form, &$form_state){
$result = db_query("SELECT url, title FROM {block_morelink} WHERE module = :module AND delta = :delta", array(
':module' => $form['module']['#value'],
':delta' => $form['delta']['#value'],
))->fetch();
$default_morelink_url = empty($result)?'':$result->url;
$default_morelink_title = empty($result)?'':$result->title;
//$default_value = '';
// More link settings.
$form['settings']['morelink'] = array(
'#type' => 'fieldset',
'#title' => t('More link settings'),
'#collapsible' => FALSE,
//'#description' => t('Specify more link url and title.'),
'#weight' => -1,
);
$form['settings']['morelink']['morelink_url'] = array(
'#type' => 'textfield',
'#title' => t('More Link url'),
'#maxlength' => 255,
'#description' => t('The More Link url of the block as shown to the user.') ,
'#default_value' => $default_morelink_url,
'#weight' => 0,
);
$form['settings']['morelink']['morelink_title'] = array(
'#type' => 'textfield',
'#title' => t('More Link title'),
'#maxlength' => 255,
'#description' => t('The More Link title of the block as shown to the user.') ,
'#default_value' => $default_morelink_title,
'#weight' => 1,
);
$form['#submit'][] = 'block_morelink_block_admin_configure_submit';
}
/**
* Form submit handler for block configuration form.
*/
function block_morelink_block_admin_configure_submit($form, &$form_state){
db_delete('block_morelink')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->execute();
if(!empty($form_state['values']['morelink_url'])){
$query = db_insert('block_morelink')->fields(array('url', 'title', 'module', 'delta'));
//foreach (array_filter($form_state['values']['types']) as $type) {
$query->values(array(
'url' => $form_state['values']['morelink_url'],
'title' => $form_state['values']['morelink_title'],
'module' => $form_state['values']['module'],
'delta' => $form_state['values']['delta'],
));
//}
$query->execute();
}
}
/**
* Process variables for block.tpl.php
*
* The $variables array contains the following arguments:
* - $block
*
* @see block.tpl.php
*/
function block_morelink_preprocess_block(&$variables) {
$result = db_query("SELECT url, title FROM {block_morelink} WHERE module = :module AND delta = :delta", array(
':module' => $variables['block']->module,
':delta' => $variables['block']->delta,
))->fetch();
$morelink_url = empty($result)?'':$result->url;
$morelink_title = empty($result)?'':$result->title;
$variables['block']->more = theme('block_more_link',array('url'=>$morelink_url,'title'=>$morelink_title));
}
/**
* Implements hook_menu().
*/
function block_morelink_menu() {
// Block settings.
$items['admin/config/block'] = array(
'title' => 'Block',
'description' => 'Block configuration.',
'position' => 'left',
'weight' => -10,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/block/morelink'] = array(
'title' => 'More link',
'description' => 'the more link lable of block.',
'page callback' => 'drupal_get_form',
'page arguments' => array('block_morelink_label_settings'),
'access arguments' => array('administer site configuration'),
'weight' => -10,
// 'file' => 'system.admin.inc',
);
return $items;
}
function block_morelink_label_settings(){
$form['morelink_label'] = array(
'#type' => 'textfield',
'#title' => t('More Link lable'),
'#maxlength' => 40,
'#description' => t('The More Link lable of the block as shown to the user.') ,
'#default_value' => variable_get('morelink_label', 'more'),
'#weight' => -17,
);
return system_settings_form($form, TRUE);
}
function block_morelink_theme(){
return array(
'block_more_link' => array(
'variables' => array('url' => NULL,'title' => NULL,)
),
);
}
/**
* Returns HTML for a "more" link, like those used in blocks.
*
* @param $variables
* An associative array containing:
* - url: The url of the main page.
* - title: A descriptive verb for the link, like 'Read more'.
*/
function theme_block_more_link($variables) {
$output = "";
if(!empty($variables['url'])){
if (!empty($variables['title'])) {
$morelink_label = $variables['title'];
}
else {
$morelink_label = variable_get('morelink_label', 'more');
}
$output .= '<span class="block-more-link">' . l($morelink_label,$variables['url'],array('attributes'=>array('title'=>$variables['title']))). '</span>';
//$output .='<span class="block-more-link">' . t('<a href="@link" title="@title">More</a>', array('@link' => check_url($variables['url']), '@title' => $variables['title'])) . '</div>';
}
return $output;
}