Created
March 12, 2015 07:34
-
-
Save erikfrerejean/205930c0cf273b9d9318 to your computer and use it in GitHub Desktop.
Drupal blocks exposed as CTools plugins. This is just a quick POC for some internal debate, by far not the best implementation but you'll get the idea ;).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = block_plugin | |
core = 7.x | |
dependencies[] = ctools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_ctools_plugin_type(). | |
*/ | |
function block_plugin_ctools_plugin_type() { | |
return array( | |
'block' => array( | |
'classes' => array( | |
'handler', | |
), | |
), | |
); | |
} | |
/** | |
* Implements hook_ctools_plugin_directory(). | |
*/ | |
function block_plugin_ctools_plugin_directory($module, $plugin) { | |
$directory = ''; | |
switch ("{$module}-{$plugin}") { | |
case 'block_plugin-block': | |
$directory = 'plugins/blocks'; | |
break; | |
} | |
return $directory; | |
} | |
/** | |
* Function to get block plugins | |
* | |
* @param string $name (optional) If set, only return the requested plugin | |
* @return array $plugins | |
*/ | |
function block_plugin_get_block($name = NULL) { | |
ctools_include('plugins'); | |
$plugins = ctools_get_plugins('block_plugin', 'block'); | |
return !empty($name) && !empty($plugins[$name]) | |
? $plugins[$name] | |
: $plugins; | |
} | |
/** | |
* Retrieves a block plugin based upon the provided delta | |
* | |
* @param string $delta | |
*/ | |
function block_plugin_get_block_by_delta($delta) { | |
$blocks = &drupal_static(__FUNCTION__); | |
if (NULL === $blocks) { | |
$blocks_from_cache = cache_get(__FUNCTION__); | |
if (!empty($blocks_from_cache->data)) { | |
$blocks = $blocks_from_cache->data; | |
} | |
else { | |
$blocks = array(); | |
} | |
} | |
if (!isset($blocks[$delta])) { | |
foreach (block_plugin_get_block() as $plugin => $block) { | |
$block_handler = new $block['handler']['class']; | |
$block_delta = $block_handler->getBlockDelta(); | |
if (!isset($blocks[$block_delta])) { | |
$blocks[$block_delta] = $plugin; | |
} | |
} | |
} | |
$block_plugin = block_plugin_get_block($plugin); | |
return new $block_plugin['handler']['class']; | |
} | |
/** | |
* Implements hook_block_info(). | |
*/ | |
function block_plugin_block_info() { | |
$blocks = array(); | |
foreach (block_plugin_get_block() as $block) { | |
$block_handler = new $block['handler']['class']; | |
$blocks[$block_handler->getBlockDelta()] = $block_handler->getBlockInfo(); | |
} | |
return $blocks; | |
} | |
/** | |
* Implements hook_block_configure(). | |
*/ | |
function block_plugin_block_configure($delta = '') { | |
$block = block_plugin_get_block_by_delta($delta); | |
return $block->getConfigureForm(); | |
} | |
/** | |
* Implements hook_block_save(). | |
*/ | |
function block_plugin_block_save($delta = '', array $edit = array()) { | |
$block = block_plugin_get_block_by_delta($delta); | |
$block->saveConfigureForm(); | |
} | |
/** | |
* Implements hook_block_view(). | |
*/ | |
function block_plugin_block_view($delta = '') { | |
$block = block_plugin_get_block_by_delta($delta); | |
return $block->view(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CTools plugin definition. | |
*/ | |
$plugin = array( | |
'title' => t('TestBlockPlugin'), | |
'handler' => array( | |
'class' => 'TestBlockPlugin', | |
), | |
); | |
interface BlockPluginInterface { | |
public function getBlockInfo(); | |
public function getBlockDelta(); | |
public function getConfigureForm(); | |
public function saveConfigureForm(); | |
public function view(); | |
} | |
abstract class BlockPluginBase implements BlockPluginInterface { | |
protected $block_info = ''; | |
protected $block_cache = DRUPAL_CACHE_PER_PAGE; | |
public function getBlockInfo() { | |
return array( | |
'info' => $this->block_info, | |
'cache' => $this->block_cache, | |
); | |
} | |
public function getBlockDelta() { | |
return substr(md5(get_called_class()), 0, 10); | |
} | |
public function getConfigureForm() { | |
return array(); | |
} | |
public function saveConfigureForm() { | |
if (!empty($this->getConfigureForm())) { | |
// | |
} | |
} | |
public function view() { | |
return array( | |
'subject' => $this->getBlockSubject(), | |
'content' => $this->getBlockContent(), | |
); | |
} | |
// | |
protected function getBlockSubject() { | |
return ''; | |
} | |
protected function getBlockContent() { | |
return array(); | |
} | |
} | |
class TestBlockPlugin extends BlockPluginBase { | |
protected $block_info = 'Test plugin'; | |
protected function getBlockSubject() { | |
return t('Test block plugin'); | |
} | |
protected function getBlockContent() { | |
return array( | |
'#markup' => '<p>TestContent</p>', | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment