class StatisticsPopularBlock

Provides a 'Popular content' block.

Plugin annotation


@Plugin(
  id = "statistics_popular_block",
  admin_label = @Translation("Popular content"),
  module = "statistics"
)

Hierarchy

Expanded class hierarchy of StatisticsPopularBlock

File

drupal/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php, line 23
Contains \Drupal\statistics\Plugin\Block\StatisticsPopularBlock.

Namespace

Drupal\statistics\Plugin\Block
View source
class StatisticsPopularBlock extends BlockBase {

  /**
   * 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;

  /**
   * Overrides \Drupal\block\BlockBase::settings().
   */
  public function settings() {
    return array(
      'top_day_num' => 0,
      'top_all_num' => 0,
      'top_last_num' => 0,
    );
  }

  /**
   * Overrides \Drupal\block\BlockBase::access().
   */
  public function access() {
    if (user_access('access content')) {
      $daytop = $this->configuration['top_day_num'];
      if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, t("Today's:")))) {
        return FALSE;
      }
      $alltimetop = $this->configuration['top_all_num'];
      if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, t('All time:')))) {
        return FALSE;
      }
      $lasttop = $this->configuration['top_last_num'];
      if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, t('Last viewed:')))) {
        return FALSE;
      }
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockForm().
   */
  public function blockForm($form, &$form_state) {

    // Popular content block settings.
    $numbers = array(
      '0' => t('Disabled'),
    ) + drupal_map_assoc(array(
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      15,
      20,
      25,
      30,
      40,
    ));
    $form['statistics_block_top_day_num'] = array(
      '#type' => 'select',
      '#title' => t("Number of day's top views to display"),
      '#default_value' => $this->configuration['top_day_num'],
      '#options' => $numbers,
      '#description' => t('How many content items to display in "day" list.'),
    );
    $form['statistics_block_top_all_num'] = array(
      '#type' => 'select',
      '#title' => t('Number of all time views to display'),
      '#default_value' => $this->configuration['top_all_num'],
      '#options' => $numbers,
      '#description' => t('How many content items to display in "all time" list.'),
    );
    $form['statistics_block_top_last_num'] = array(
      '#type' => 'select',
      '#title' => t('Number of most recent views to display'),
      '#default_value' => $this->configuration['top_last_num'],
      '#options' => $numbers,
      '#description' => t('How many content items to display in "recently viewed" list.'),
    );
    return $form;
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockSubmit().
   */
  public function blockSubmit($form, &$form_state) {
    $this->configuration['top_day_num'] = $form_state['values']['statistics_block_top_day_num'];
    $this->configuration['top_all_num'] = $form_state['values']['statistics_block_top_all_num'];
    $this->configuration['top_last_num'] = $form_state['values']['statistics_block_top_last_num'];
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $content = array();
    if ($this->day_list) {
      $content['top_day'] = $this->day_list;
      $content['top_day']['#suffix'] = '<br />';
    }
    if ($this->all_time_list) {
      $content['top_all'] = $this->all_time_list;
      $content['top_all']['#suffix'] = '<br />';
    }
    if ($this->last_list) {
      $content['top_last'] = $this->last_list;
      $content['top_last']['#suffix'] = '<br />';
    }
    return $content;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockBase::blockValidate public function Adds block type-specific validation for the block form.
BlockBase::form public function Implements \Drupal\block\BlockPluginInterface::form(). Overrides BlockPluginInterface::form 1
BlockBase::getConfig public function Returns the configuration data for the block plugin.
BlockBase::setConfig public function Sets a particular value in the block settings.
BlockBase::submit public function Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface::submit
BlockBase::validate public function Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface::validate
BlockBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
StatisticsPopularBlock::$all_time_list protected property Number of all time views to display.
StatisticsPopularBlock::$day_list protected property Number of day's top views to display.
StatisticsPopularBlock::$last_list protected property Number of most recent views to display.
StatisticsPopularBlock::access public function Overrides \Drupal\block\BlockBase::access(). Overrides BlockBase::access
StatisticsPopularBlock::blockForm public function Overrides \Drupal\block\BlockBase::blockForm(). Overrides BlockBase::blockForm
StatisticsPopularBlock::blockSubmit public function Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockBase::blockSubmit
StatisticsPopularBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
StatisticsPopularBlock::settings public function Overrides \Drupal\block\BlockBase::settings(). Overrides BlockBase::settings