Tuesday 20 July 2010

Solving simplenews block theming in Drupal

Ok,

Some weeks ago I,ve installed Simplenews module for one of my Drupal developments, as you may notice there are a lots of discussions going regarding displaying of subscribe/save button in the Simplenews block, most of them - how to change default SAVE when user is not logged in to Subscribe (or whatever).
First I,ve tested well known function themename_simplenews_block () - no results,
I went through all .module code and of course I didn,t found any implementation oh the about function, of course there is simplemenys_theme (implementation of hook_theme) on line 2428, but as you can see;


function simplenews_theme() {
return array(
'simplenews_block' => array(
'template' => 'simplenews-block',
'arguments' => array('tid' => NULL),
'pattern' => 'simplenews_block__',
),
'simplenews_status' => array(
'template' => 'simplenews-status',
'file' => 'simplenews.admin.inc',
'arguments' => array('status' => NULL, 'source' => NULL),
),
'simplenews_newsletter_subject' => array(
'arguments' => array('name' => NULL, 'title' => NULL, 'language' => NULL),
),
'simplenews_newsletter_body' => array(
'template' => 'simplenews-newsletter-body',
'arguments' => array('node' => NULL, 'language' => NULL),
'pattern' => 'simplenews_newsletter_body__',
),
'simplenews_newsletter_footer' => array(
'template' => 'simplenews-newsletter-footer',
'arguments' => array('node' => NULL, 'key' => NULL, 'language' => NULL),
'pattern' => 'simplenews_newsletter_footer__',
),
'simplenews_subscription_list' => array(
'file' => 'simplenews.admin.inc',
'arguments' => array('form' => NULL),
),
);
}
?>

It is rendering only main elements - not any specific page or block ones as themselves.
Getting inspired by Documentation of custom modules and http://drupal.org/node/569312,
Here is full process.
First - from (on line 2471) simplenews.module copy this and;


function template_preprocess_simplenews_block(&$variables) {
global $user;
$tid = $variables['tid'];

// Set default values in case of missing permission.
$variables['form'] = '';
$variables['subscription_link'] = '';
$variables['newsletter_link'] = '';
$variables['issue_list'] = '';
$variables['rssfeed'] = '';

// Block content variables
$variables['message'] = check_plain(variable_get('simplenews_block_m_'. $tid, t('Stay informed on our latest news!')));
if (user_access('subscribe to newsletters')) {
$variables['form'] = drupal_get_form('simplenews_block_form_'. $tid);
$variables['subscription_link'] = l(t('Manage my subscriptions'), 'newsletter/subscriptions');
}
$variables['newsletter_link'] = l(t('Previous issues'), 'taxonomy/term/'. $tid);
$recent = simplenews_recent_newsletters($tid, variable_get('simplenews_block_i_'. $tid, 5));
$variables['issue_list'] = theme('item_list', $recent, t('Previous issues'), 'ul');
$term = taxonomy_get_term($tid);
$variables['rssfeed'] = theme('feed_icon', url('taxonomy/term/'. $tid .'/0/feed'), t('@newsletter feed', array('@newsletter' => $term->name)));

// Block content control variables
$variables['use_form'] = variable_get('simplenews_block_f_'. $tid, 1);
$variables['use_issue_link'] = variable_get('simplenews_block_l_'. $tid, 1);
$variables['use_issue_list'] = variable_get('simplenews_block_i_status_'. $tid, 0);
$variables['use_rss'] = variable_get('simplenews_block_r_'. $tid, 1);

// Additional variables
$variables['subscribed'] = empty($user->uid) ? FALSE : (simplenews_user_is_subscribed($user->mail, $tid) == TRUE);
$variables['user'] = !empty($user->uid);
}
?>

paste it to your themes template.php file and rename it to yourtheme__preprocess_simplenews_block.
Then create a custom module and using hook_form_alter do the staff with your subscription form.
My example here - mycoolmodule.module;


function mycoolmodule_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'simplenews_block_form_1') {
$form['submit']['#value'] = t('Subscribe');
}
?>

Sounds easy, but some suggestions - first this changes both values to "subscribe", but be careful, because this form offers and unsubscribe option too. - anyway this worked fine for me on two different machines.
Second form_id here is simplenews_block_form_1 , this goes out from - $variables['form'] = drupal_get_form('simplenews_block_form_'. $tid);, so at the end you will have a from with id="simplenews-block-form-1-1" for your later CSS works.

No comments:

Post a Comment