Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created October 21, 2010 02:39
Show Gist options
  • Save bangpound/637828 to your computer and use it in GitHub Desktop.
Save bangpound/637828 to your computer and use it in GitHub Desktop.
Boxes plugin for ctools content types.
<?php
// $Id$
/**
* Base class for all ctools content types.
*/
abstract class boxes_ctools_content extends boxes_box {
public $content_type;
public $content_type_plugin;
/**
* Implementation of boxes_content::__construct().
*/
protected function __construct($content_type) {
parent::__construct();
ctools_include('content');
$this->content_type = $content_type;
$this->content_type_plugin = ctools_get_content_type($this->content_type);
}
/**
* Implementation of boxes_content::options_defaults().
*/
public function options_defaults() {
$defaults = array('subtype' => NULL);
if (empty($this->content_type_plugin['defaults'])) {
return $defaults;
}
return array_merge($this->content_type_plugin['defaults'], $defaults);
}
/**
* Implementation of boxes_content::options_form().
*/
public function options_form() {
$plugin = $this->content_type_plugin;
$op = $this->new ? 'add' : 'edit';
$form_state = array(
'conf' => $this->options,
'op' => $op,
);
$form = array();
if ($this->new) {
$types = ctools_content_get_subtypes($this->content_type);
$options = array();
foreach ($types as $key => $subtype) {
$options[$subtype['category']][$key] = $subtype['title'];
}
$form['subtype'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => 'Subtype',
);
}
else {
$form['subtype'] = array(
'#type' => 'value',
'#value' => $this->options['subtype'],
);
}
// Populate the form array.
$plugin[$op .' form']($form, $form_state);
// Add the form validator function as an element validator.
if (function_exists('boxes_'. $plugin[$op .' form'] .'_validate')) {
$form['#element_validate'][] = 'boxes_'. $plugin[$op .' form'] .'_validate';
}
return $form;
}
/**
* Implementation of boxes_content::options_form().
*/
public function render() {
$type = $this->content_type;
$conf = $this->options;
$content = ctools_content_render($type, $this->options['subtype'], $conf);
$title = isset($this->title) ? check_plain($this->title) : NULL;
return array(
'delta' => $this->delta, // Crucial.
'title' => $title,
'subject' => $content->subject,
'content' => $content->content,
);
}
}
<?php
// $Id$
/**
* Blocks as a block.
*/
class boxes_ctools_content_block extends boxes_ctools_content {
/**
* Implementation of boxes_content::__construct().
*/
protected function __construct() {
parent::__construct('block');
}
}
<?php
// $Id$
/**
* Node as a block.
*/
class boxes_ctools_content_node extends boxes_ctools_content {
/**
* Implementation of boxes_content::__construct().
*/
protected function __construct() {
parent::__construct('node');
}
}
/**
* Validate the node selection.
*/
function boxes_ctools_node_content_type_edit_form_validate(&$form, &$form_state) {
// This tricks the content type form validation.
$form_id = $form_state['values']['form_id'];
if ($form_id == 'boxes_add_form' || $form_id == 'boxes_box_form') {
$form_state['op'] = 'add';
}
ctools_node_content_type_edit_form_validate($form, $form_state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment