core/modules/help/src/Plugin/Block/HelpBlock.php | 35 ++++++++---- .../node/src/Tests/NodeTranslationUITest.php | 1 - .../src/Plugin/Block/StatisticsPopularBlock.php | 65 ++++++++++++++++------ 3 files changed, 73 insertions(+), 28 deletions(-) diff --git a/core/modules/help/src/Plugin/Block/HelpBlock.php b/core/modules/help/src/Plugin/Block/HelpBlock.php index abe3c25..c62e505 100644 --- a/core/modules/help/src/Plugin/Block/HelpBlock.php +++ b/core/modules/help/src/Plugin/Block/HelpBlock.php @@ -9,6 +9,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Block\StatefulBlockPluginInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -24,7 +25,14 @@ * admin_label = @Translation("Help") * ) */ -class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface { +class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface, StatefulBlockPluginInterface { + + /** + * Stores the help text associated with the active menu item. + * + * @var string + */ + protected $help; /** * The module handler. @@ -86,6 +94,19 @@ public static function create(ContainerInterface $container, array $configuratio } /** + * {@inheritdoc} + */ + protected function blockAccess(AccountInterface $account) { + $this->help = $this->getActiveHelp($this->request); + if ($this->help) { + return AccessResult::allowed(); + } + else { + return AccessResult::forbidden(); + } + } + + /** * Returns the help associated with the active menu item. * * @param \Symfony\Component\HttpFoundation\Request $request @@ -105,15 +126,9 @@ protected function getActiveHelp(Request $request) { * {@inheritdoc} */ public function build() { - $help = $this->getActiveHelp($this->request); - if (!$help) { - return []; - } - else { - return [ - '#children' => $help, - ]; - } + return array( + '#children' => $this->help, + ); } /** diff --git a/core/modules/node/src/Tests/NodeTranslationUITest.php b/core/modules/node/src/Tests/NodeTranslationUITest.php index 29f15bc..c392750 100644 --- a/core/modules/node/src/Tests/NodeTranslationUITest.php +++ b/core/modules/node/src/Tests/NodeTranslationUITest.php @@ -33,7 +33,6 @@ class NodeTranslationUITest extends ContentTranslationUITestBase { 'route.menu_active_trails:main', 'route.menu_active_trails:tools', 'timezone', - 'url', 'user.roles' ]; diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index 621f6cf..dca94cc 100644 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -9,6 +9,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Block\StatefulBlockPluginInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -20,7 +21,28 @@ * admin_label = @Translation("Popular content") * ) */ -class StatisticsPopularBlock extends BlockBase { +class StatisticsPopularBlock extends BlockBase implements StatefulBlockPluginInterface { + + /** + * Number of day's top views to display. + * + * @var int + */ + protected $day_list; + + /** + * Number of all time views to display. + * + * @var int + */ + protected $all_time_list; + + /** + * Number of most recent views to display. + * + * @var int + */ + protected $last_list; /** * {@inheritdoc} @@ -37,7 +59,23 @@ public function defaultConfiguration() { * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - return AccessResult::allowedIfHasPermission($account, 'access content'); + $access = AccessResult::allowedIfHasPermission($account, 'access content'); + if ($account->hasPermission('access content')) { + $daytop = $this->configuration['top_day_num']; + if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, $this->t("Today's:")))) { + return AccessResult::forbidden()->inheritCacheability($access); + } + $alltimetop = $this->configuration['top_all_num']; + if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, $this->t('All time:')))) { + return AccessResult::forbidden()->inheritCacheability($access); + } + $lasttop = $this->configuration['top_last_num']; + if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, $this->t('Last viewed:')))) { + return AccessResult::forbidden()->inheritCacheability($access); + } + return $access; + } + return AccessResult::forbidden()->inheritCacheability($access); } /** @@ -86,25 +124,18 @@ public function blockSubmit($form, FormStateInterface $form_state) { public function build() { $content = array(); - if ($this->configuration['top_day_num'] > 0) { - $result = statistics_title_list('daycount', $this->configuration['top_day_num']); - if ($result) { - $content['top_day'] = node_title_list($result, $this->t("Today's:")); - $content['top_day']['#suffix'] = '
'; - } + if ($this->day_list) { + $content['top_day'] = $this->day_list; + $content['top_day']['#suffix'] = '
'; } - if ($this->configuration['top_all_num'] > 0) { - $result = statistics_title_list('totalcount', $this->configuration['top_all_num']); - if ($result) { - $content['top_all'] = node_title_list($result, $this->t('All time:')); - $content['top_all']['#suffix'] = '
'; - } + if ($this->all_time_list) { + $content['top_all'] = $this->all_time_list; + $content['top_all']['#suffix'] = '
'; } - if ($this->configuration['top_last_num'] > 0) { - $result = statistics_title_list('timestamp', $this->configuration['top_last_num']); - $content['top_last'] = node_title_list($result, $this->t('Last viewed:')); + if ($this->last_list) { + $content['top_last'] = $this->last_list; $content['top_last']['#suffix'] = '
'; }