-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_titlelink.module
99 lines (91 loc) · 2.9 KB
/
block_titlelink.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
// Variable Table Prefex
define('BLOCK_TITLELINK_VARIABLE_PREFIX','block_titlelink_');
function block_titlelink_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'block_admin_configure') {
$block->module = $form['module']['#value'];
$block->delta = $form['delta']['#value'];
$link = _block_titlelink_get_link($block);
//Assign weight to block title so it appears above link
if(!isset($form['block_settings']['#weight'])) {
$form['block_settings']['#weight'] = -1;
}
$form['block_titlelink'] = array(
'#type' => 'fieldset',
'#title' => t('Block Title Link settings'),
'#collapsible' => TRUE,
'#weight' => 0,
);
$form['block_titlelink']['title_path'] = array(
'#type' => 'textfield',
'#title' => t('Title Path'),
'#default_value' => $link,
'#description' => t('URL for Block Title to link to.'),
);
$form['#validate'][] = 'block_titlelink_validate';
}
}
/**
* Implementation of hook_validation()
**/
function block_titlelink_validate($form, &$form_state) {
if ($form_state['values']['form_id'] == 'block_admin_configure') {
if (isset($form_state['values']['title_path']) && user_access('administer blocks')) {
$module = arg(4);
$delta = arg(5);
//TODO: Validate Link based on URL and PATH
$link = $form_state['values']['title_path'];
//Save Form Link
if($module && $delta && $link) {
$name = $module . '_' . $delta;
//Delete link if empty
if(strlen($link) == 0) {
_block_titlelink_delete_link($name);
}
else {
_block_titlelink_save_link($name,$link);
}
}
}
}
block_admin_configure_validate($form, $form_state);
}
/**
* TODO: Function to pre-process block to add title link
**/
function block_titlelink_preprocess_block(&$vars, $hook) {
if($hook == 'block') {
$var_name = $vars['block']->module . '_' . $vars['block']->delta;
$vars['block']->title_link = _block_titlelink_get_link($vars['block']);
}
}
/**
* Function to return a title link of a block
* @param array $block - Block to search by
* @return string Title Link
**/
function _block_titlelink_get_link($block) {
if(!isset($block->module) && !isset($block->delta)) {
return FALSE;
}
$varname = BLOCK_TITLELINK_VARIABLE_PREFIX . $block->module . '_' . $block->delta;
return variable_get($varname,NULL);
}
/**
* Function to save a title link block
* @param string $name - Variable name (module . '_' . delta)
* @param string $value - A valid drupal path or URL
* @return void
**/
function _block_titlelink_save_link($name,$value) {
$varname = BLOCK_TITLELINK_VARIABLE_PREFIX . $name;
variable_set($varname, $value);
}
/**
* Function to delete a link variable
* @param string $name
**/
function _block_titlelink_delete_link($name) {
$varname = BLOCK_TITLELINK_VARIABLE_PREFIX . $name;
variable_del($varname);
}