Skip to content

Instantly share code, notes, and snippets.

@tylor
Created October 9, 2011 04:00
Show Gist options
  • Save tylor/1273261 to your computer and use it in GitHub Desktop.
Save tylor/1273261 to your computer and use it in GitHub Desktop.
Extend boxes box, implement hook_boxes_plugins().
<?php
/**
* Simple custom text box.
*/
class multilingual_box extends boxes_box {
/**
* Implementation of boxes_content::options_defaults().
*/
public function options_defaults() {
$defaults = array();
$languages = language_list('enabled');
foreach ($languages[1] as $language) {
$defaults += array(
$language->language . '-body' => '',
$language->language . '-title' => '',
);
}
return $defaults;
}
/**
* Implementation of boxes_content::options_form().
*/
public function options_form() {
$form = array();
$languages = language_list('enabled');
foreach ($languages[1] as $language) {
$form[$language->language] = array(
'#type' => 'fieldset',
'#title' => $language->name,
);
$form[$language->language][$language->language . '-title'] = array(
'#type' => 'textfield',
'#title' => t('Box title'),
'#default_value' => $this->options[$language->language . '-title'],
'#description' => t('The title of the box as shown to the user.'),
);
$form[$language->language][$language->language . '-body'] = array(
'#type' => 'textarea',
'#title' => t('Box body'),
'#default_value' => $this->options[$language->language . '-body'],
'#rows' => 6,
'#description' => t('The content of the block as shown to the user.'),
);
}
return $form;
}
/**
* Implementation of boxes_content::options_form().
*/
public function render() {
global $language;
// Title. English is fallback.
if (!empty($this->options[$language->language . '-title'])) {
$title = check_plain($this->options[$language->language . '-title']);
}
elseif (!empty($this->options['en-title'])) {
$title = check_plain($this->options['en-title']);
}
else {
$title = NULL;
}
// Body. English is fallback.
if (!empty($this->options[$language->language . '-body'])) {
// Filter full HTML.
$content = check_markup($this->options[$language->language . '-body'], 3, FALSE);
}
elseif (!empty($this->options['en-body'])) {
$content = check_markup($this->options['en-body'], 3, FALSE);
}
else {
$content = '';
}
return array(
'delta' => $this->delta, // Crucial.
'title' => $title,
'subject' => $title,
'content' => $content,
);
}
}
<?php
/**
* Implementation of hook_boxes_plugins().
*/
function mymodule_boxes_plugins() {
$info = array();
$info['multilingual_box'] = array(
'title' => 'Multilingual box',
'handler' => array(
'class' => 'multilingual_box',
'file' => 'multilingual_box.inc',
'path' => drupal_get_path('module', 'mymodule') .'/boxes',
'parent' => 'box',
),
);
return $info;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment