Created
January 7, 2014 22:09
-
-
Save larowlan/8307837 to your computer and use it in GitHub Desktop.
Blocks as plugins
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 | |
/** | |
* @file | |
* Contains \Drupal\forum\Plugin\Block\ActiveTopicsBlock. | |
*/ | |
namespace Drupal\forum\Plugin\Block; | |
/** | |
* Provides an 'Active forum topics' block. | |
* | |
* @Block( | |
* id = "forum_active_block", | |
* admin_label = @Translation("Active forum topics"), | |
* category = @Translation("Lists (Views)") | |
* ) | |
*/ | |
class ActiveTopicsBlock extends ForumBlockBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function build() { | |
$query = db_select('forum_index', 'f') | |
->fields('f') | |
->addTag('node_access') | |
->addMetaData('base_table', 'forum_index') | |
->orderBy('f.last_comment_timestamp', 'DESC') | |
->range(0, $this->configuration['block_count']); | |
return array( | |
drupal_render_cache_by_query($query, 'forum_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 | |
/** | |
* @file | |
* Contains \Drupal\forum\Plugin\Block\ForumBlockBase. | |
*/ | |
namespace Drupal\forum\Plugin\Block; | |
use Drupal\block\BlockBase; | |
use Drupal\Core\Session\AccountInterface; | |
/** | |
* Provides a base class for Forum blocks. | |
*/ | |
abstract class ForumBlockBase extends BlockBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function defaultConfiguration() { | |
return array( | |
'cache' => DRUPAL_CACHE_CUSTOM, | |
'properties' => array( | |
'administrative' => TRUE, | |
), | |
'block_count' => 5, | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function access(AccountInterface $account) { | |
return $account->hasPermission('access content'); | |
} | |
/** | |
* Overrides \Drupal\block\BlockBase::blockForm(). | |
*/ | |
public function blockForm($form, &$form_state) { | |
$form['block_count'] = array( | |
'#type' => 'select', | |
'#title' => t('Number of topics'), | |
'#default_value' => $this->configuration['block_count'], | |
'#options' => drupal_map_assoc(range(2, 20)), | |
); | |
return $form; | |
} | |
/** | |
* Overrides \Drupal\block\BlockBase::blockSubmit(). | |
*/ | |
public function blockSubmit($form, &$form_state) { | |
$this->configuration['block_count'] = $form_state['values']['block_count']; | |
} | |
} |
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 | |
/** | |
* @file | |
* Contains \Drupal\forum\Plugin\Block\NewTopicsBlock. | |
*/ | |
namespace Drupal\forum\Plugin\Block; | |
/** | |
* Provides a 'New forum topics' block. | |
* | |
* @Block( | |
* id = "forum_new_block", | |
* admin_label = @Translation("New forum topics"), | |
* category = @Translation("Lists (Views)") | |
* ) | |
*/ | |
class NewTopicsBlock extends ForumBlockBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function build() { | |
$query = db_select('forum_index', 'f') | |
->fields('f') | |
->addTag('node_access') | |
->addMetaData('base_table', 'forum_index') | |
->orderBy('f.created', 'DESC') | |
->range(0, $this->configuration['block_count']); | |
return array( | |
drupal_render_cache_by_query($query, 'forum_block_view'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment