Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 36 |
ForumBlockBase | |
0.00% |
0 / 1 |
|
12.50% |
1 / 8 |
90 | |
0.00% |
0 / 36 |
build | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 12 |
|||
buildForumQuery | |
100.00% |
1 / 1 |
1 | |
100.00% |
0 / 0 |
|||
defaultConfiguration | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
blockAccess | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
blockForm | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 9 |
|||
blockSubmit | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
getCacheContexts | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
getCacheTags | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
<?php | |
/** | |
* @file | |
* Contains \Drupal\forum\Plugin\Block\ForumBlockBase. | |
*/ | |
namespace Drupal\forum\Plugin\Block; | |
use Drupal\Core\Access\AccessResult; | |
use Drupal\Core\Block\BlockBase; | |
use Drupal\Core\Cache\Cache; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\Core\Url; | |
/** | |
* Provides a base class for Forum blocks. | |
*/ | |
abstract class ForumBlockBase extends BlockBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function build() { | |
$result = $this->buildForumQuery()->execute(); | |
$elements = array(); | |
if ($node_title_list = node_title_list($result)) { | |
$elements['forum_list'] = $node_title_list; | |
$elements['forum_more'] = array( | |
'#type' => 'more_link', | |
'#url' => Url::fromRoute('forum.index'), | |
'#attributes' => array('title' => $this->t('Read the latest forum topics.')), | |
); | |
} | |
return $elements; | |
} | |
/** | |
* Builds the select query to use for this forum block. | |
* | |
* @return \Drupal\Core\Database\Query\Select | |
* A Select object. | |
*/ | |
abstract protected function buildForumQuery(); | |
/** | |
* {@inheritdoc} | |
*/ | |
public function defaultConfiguration() { | |
return array( | |
'properties' => array( | |
'administrative' => TRUE, | |
), | |
'block_count' => 5, | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function blockAccess(AccountInterface $account) { | |
return AccessResult::allowedIfHasPermission($account, 'access content'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockForm($form, FormStateInterface $form_state) { | |
$range = range(2, 20); | |
$form['block_count'] = array( | |
'#type' => 'select', | |
'#title' => $this->t('Number of topics'), | |
'#default_value' => $this->configuration['block_count'], | |
'#options' => array_combine($range, $range), | |
); | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockSubmit($form, FormStateInterface $form_state) { | |
$this->configuration['block_count'] = $form_state->getValue('block_count'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCacheContexts() { | |
return Cache::mergeContexts(parent::getCacheContexts(), ['user.node_grants:view']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCacheTags() { | |
return Cache::mergeTags(parent::getCacheTags(), ['node_list']); | |
} | |
} |