You are here

class MostReaded in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x modules/dashboards_statistic/src/Plugin/Dashboard/MostReaded.php \Drupal\dashboards_statistic\Plugin\Dashboard\MostReaded

Show account info.

Plugin annotation


@Dashboard(
  id = "node_most_readed",
  label = @Translation("Show most readed."),
  category = @Translation("Statistics"),
)

Hierarchy

Expanded class hierarchy of MostReaded

File

modules/dashboards_statistic/src/Plugin/Dashboard/MostReaded.php, line 25

Namespace

Drupal\dashboards_statistic\Plugin\Dashboard
View source
class MostReaded extends DashboardBase {
  use ChartTrait;

  /**
   * Entity Type Manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeInfo;

  /**
   * Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache_backend, EntityTypeBundleInfoInterface $entity_type_info, ModuleHandlerInterface $module_handler, Connection $database) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $cache_backend);
    $this->entityTypeInfo = $entity_type_info;
    $this->moduleHandler = $module_handler;
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('dashboards.cache'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('module_handler'), $container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
    $options = [
      'totalcount' => $this
        ->t('Total count'),
      'daycount' => $this
        ->t('Dayly count'),
    ];
    $form['count'] = [
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => isset($configuration['count']) ? $configuration['count'] : FALSE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRenderArray($configuration) : array {
    $stats = [];
    $field = 'totalcount';
    if ($configuration['count'] == 'daycount') {
      $field = 'daycount';
    }
    $cache = $this
      ->getCache($field);
    if (!$cache) {
      $query = $this->database
        ->select('node_field_data', 'nfd');
      $query
        ->join('node_counter', 'nc', 'nc.nid = nfd.nid');
      $query
        ->fields('nfd', [
        'type',
      ]);
      $query
        ->addExpression('SUM(nc.' . $field . ')', 'count');
      $query
        ->groupBy('type');
      $rows = [];
      $stats['types'] = $query
        ->execute()
        ->fetchAllAssoc('type');
      foreach ($stats['types'] as $type => $count) {
        $rows[] = [
          $type,
          $count->count,
        ];
      }
      $this
        ->setCache($field, $rows, CacheBackendInterface::CACHE_PERMANENT, [
        'node_list',
      ]);
    }
    else {
      $rows = $cache->data;
    }
    $this
      ->setLabels([
      $this
        ->t('Node Type'),
      $this
        ->t('Count'),
    ]);
    $this
      ->setRows($rows);
    $build = $this
      ->renderChart($configuration);
    $build['#cache'] = [
      'tags' => [
        'node_list',
      ],
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChartTrait::$empty protected property Empty flag.
ChartTrait::$labels protected property Labels.
ChartTrait::$rows protected property Rows.
ChartTrait::$type protected property Chart type.
ChartTrait::addLabel public function Add a label.
ChartTrait::addRow public function Add a row.
ChartTrait::getAllowedStyles public function Get allowed styles.
ChartTrait::renderChart public function Set all rows.
ChartTrait::setChartType public function Add a label.
ChartTrait::setEmpty public function Set this chart is empty.
ChartTrait::setLabels public function Set all labels.
ChartTrait::setRows public function Set all rows.
DashboardBase::$cache protected property Cache backend.
DashboardBase::getCache protected function Get cache for cid.
DashboardBase::massageFormValues public function Validate settings form.
DashboardBase::setCache protected function Set a new cache entry. Cache is prefixed by pluginid.
DashboardBase::validateForm public function Validate settings form. 1
MostReaded::$database protected property Database.
MostReaded::$entityTypeInfo protected property Entity Type Manager.
MostReaded::$moduleHandler protected property Module Handler.
MostReaded::buildRenderArray public function Build render array. Overrides DashboardBase::buildRenderArray
MostReaded::buildSettingsForm public function Build render array. Overrides DashboardBase::buildSettingsForm
MostReaded::create public static function Creates an instance of the plugin. Overrides DashboardBase::create
MostReaded::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DashboardBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.