You are here

public function ContentOverview::build in Total Control Admin Dashboard 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/ContentOverview.php \Drupal\total_control\Plugin\Block\ContentOverview::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/ContentOverview.php, line 112

Class

ContentOverview
Provides a 'Content Overview'.

Namespace

Drupal\total_control\Plugin\Block

Code

public function build() {
  $items = [];
  $types = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();
  $config = $this
    ->getConfiguration();
  foreach ($types as $type => $object) {

    // Compare against type option on pane config.
    if (!array_key_exists($type, $config['total_control_types_overview']) || (isset($config['total_control_types_overview']) && $config['total_control_types_overview'][$type]) == $type) {
      $type_query = $this->connection
        ->query("SELECT count(*) FROM {node_field_data} WHERE type = :type and status = 1", [
        ':type' => $type,
      ]);
      $type_count = $type_query
        ->fetchField();
      $content_data[$type] = $this->stringTranslation
        ->formatPlural($type_count, '1 ' . $object
        ->get('name') . ' item', '@count ' . $object
        ->get('name') . ' items');

      // Check if comments module is enabled.
      if ($this->moduleHandler
        ->moduleExists('comment')) {

        // Compare against comment options on pane config.
        if (!array_key_exists($type, $config['total_control_comments_overview']) || (isset($config['total_control_comments_overview']) && $config['total_control_comments_overview'][$type]) == $type) {
          $comment_query = $this->connection
            ->query("SELECT count(DISTINCT c.cid) FROM {comment} c INNER JOIN {comment_field_data} n ON c.cid = n.cid INNER JOIN {node} node WHERE n.entity_id = node.nid AND node.type = :type AND n.status = 1", [
            ':type' => $type,
          ]);
          $comment_count = $comment_query
            ->fetchField();
          $content_data[$type . '_comments'] = $this->stringTranslation
            ->formatPlural($comment_count, '1 comment', '@count comments');

          // Compare against spam option checkbox on pane config.
          if (isset($config['total_control_spam_overview']) && $config['total_control_spam_overview'] == 1) {
            $spam_query = $this->connection
              ->query("SELECT count(DISTINCT c.cid) FROM {comment} c INNER JOIN {comment_field_data} n ON c.cid = n.cid INNER JOIN {node} node WHERE n.entity_id = node.nid AND node.type = :type AND n.status = 0", [
              ':type' => $type,
            ]);
            $spam_count = $spam_query
              ->fetchField();
            $content_data[$type . '_comments_spam'] = $this->stringTranslation
              ->formatPlural($spam_count, '1 spam', '@count spam');
          }
        }
      }
      $line = $content_data[$type];
      $line .= isset($content_data[$type . '_comments']) ? ' with ' . $content_data[$type . '_comments'] : '';
      $line .= isset($content_data[$type . '_comments_spam']) ? ' (' . $content_data[$type . '_comments_spam'] . ')' : '';
      $items[] = $line;
    }
  }
  if (empty($items)) {
    $markup_data = $this
      ->t('No content available.') . ' ' . Link::fromTextAndUrl($this
      ->t('Add content'), new Url('node.add_page'))
      ->toString();
    return [
      '#type' => 'markup',
      '#markup' => $markup_data,
    ];
  }
  $body_data = [
    '#theme' => 'item_list',
    '#list_type' => 'ul',
    '#items' => $items,
  ];
  $markup_data = $this->renderer
    ->render($body_data);
  return [
    '#type' => 'markup',
    '#markup' => $markup_data,
  ];
}