View source
<?php
namespace Drupal\node_type_count\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class NodeTypeCountController extends ControllerBase {
public function nodeTypeCountPublished() {
$header = array(
t('Title'),
t('Type'),
t('Published'),
t('UnPublished'),
);
$result = node_type_get_names();
if (is_array($result)) {
foreach ($result as $node_type_machine_name => $content_type_title) {
$result_arr['title'] = check_plain($content_type_title);
$result_arr['machine_name'] = $node_type_machine_name;
$result_arr['published'] = NodeTypeCountController::nodeCountState(NODE_PUBLISHED, $node_type_machine_name);
$result_arr['unpublished'] = NodeTypeCountController::nodeCountState(NODE_NOT_PUBLISHED, $node_type_machine_name);
$result_final[$node_type_machine_name] = $result_arr;
}
}
$rows = array();
foreach ($result_final as $row) {
$rows[] = array(
'data' => (array) $row,
);
}
$build = array(
'#markup' => '<p>' . t('The layout here is a themed as a table
that is sortable by clicking the header name.') . '</p>',
);
$build['tablesort_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
public function userRoleCount() {
$header = array(
t('Role Name'),
t('Role Machine Name'),
t('Number of Users'),
);
$results = user_role_names();
if (is_array($results)) {
foreach ($results as $user_role_machine_name => $content_type_title) {
$result_arr['title'] = check_plain($content_type_title);
$result_arr['machine_name'] = $user_role_machine_name;
$result_arr['count'] = NodeTypeCountController::userCountByRole($user_role_machine_name);
$result_final[$user_role_machine_name] = $result_arr;
}
}
$rows = array();
foreach ($result_final as $row) {
$rows[] = array(
'data' => (array) $row,
);
}
$build = array(
'#markup' => '<p>' . t('The layout here is a themed as a table
that is sortable by clicking the header name.') . '</p>',
);
$build['tablesort_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
public function nodeCountState($status, $type) {
$query = \Drupal::entityQuery('node')
->condition('status', $status)
->condition('type', $type);
$result = $query
->count()
->execute();
return $result;
}
public function userCountByRole($role_type_machine_name) {
$query = db_select('user__roles', 'ur')
->fields('ur', array(
'entity_id',
))
->condition('roles_target_id', $role_type_machine_name);
$results = $query
->countQuery()
->execute()
->fetchField();
return $results;
}
}