Block Submit class & wrapper API function for subscriptions
Alan D. - September 29, 2009 - 03:14
| Project: | Simplenews |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | minor |
| Assigned: | Unassigned |
| Status: | active |
Description
Hi just a couple trivial feature requests.
1) Adding a class to the block submission to help themers
<?php
$class = 'simplenews';
if (isset($submit_text)) {
$class .= ($submit_text == t('Subscribe')) ? '-subscribe' : '-unsubscribe');
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($submit_text) ? $submit_text : t('Save'),
'#attributes' => array('class' => $class)
);
?>In case other people need a similar thing and this is not pushed into the module, a simple form alter can add the class:
<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'simplenews_block_form_') === 0) { // Must be === and not ==
$form_id = 'simplenews_block_form';
}
switch ($form_id) {
case 'simplenews_block_form':
$class = ' simplenews-'. (($form['submit']['#value'] == t('Subscribe')) ? ' subscribe' : 'unsubscribe');
$form['submit'] += array('#attributes' => array()); // E_ALL error prevention
$form['submit']['#attributes'] += array('class' => ''); // And another
$form['submit']['#attributes']['class'] .= $class;
break;
}
}
?>2) Is it possible to create a new function simplenews_get_subscriptions()? I need to get the subscription list and was forced to duplicate a lot of the internal logic to get the list of names / tids. This should be statically cached if used multiple times per request (like if it becomes a storage mechanisum for translated names).
<?php
function simplenews_get_subscriptions() {
if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_TRANSLATE) {
// Per language terms.
$tterms = i18ntaxonomy_vocabulary_get_terms(variable_get('simplenews_vid', ''), $language->language);
foreach ($tterms as $tid => $name) {
$options[$tid] = check_plain($name);
}
}
else {
foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
// Localize terms.
$options[$newsletter->tid] = check_plain(tt('taxonomy:term:'. $newsletter->tid .':name', $newsletter->name, $language->language));;
}
else {
// Untranslated.
$options[$newsletter->tid] = check_plain($newsletter->name);
}
}
}
return $options;
}
// and in _simplenews_subscription_manager_form (maybe other places)
$form['subscriptions']['newsletters'] = array(
'#type' => 'checkboxes',
'#options' => simplenews_get_subscriptions(),
'#default_value' => ((array) $subscription->tids), // I do not think you need to initiate all defaults to zero (I have not tested this)
);
?>Thanks
