Skip to content
Snippets Groups Projects
Commit d58ca04b authored by Eric Bremner's avatar Eric Bremner
Browse files

Merge branch 'feature/ISTWCMS-5666-a5kulkar-Add-feed-for-content_type_use' into '1.0.x'

ISTWCMS-5666 Add a feed for content type use, matching WCMS 2

See merge request !250
parents 2c36caaf 4a2ad6ce
No related branches found
No related tags found
1 merge request!250ISTWCMS-5666 Add a feed for content type use, matching WCMS 2
<?php
namespace Drupal\uw_cfg_common\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\uw_cfg_common\Service\UWServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* Provides route responses for the uw_cfg_common module.
*/
/**
* Provides route responses for the Example module.
*/
class ContentTypeUsePage extends ControllerBase {
/**
* Entity type manager from the core.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface\
*/
protected $entityTypeManager;
/**
* UW service.
*
* @var Drupal\uw_cfg_common\Service\UWServiceInterface
*/
protected $uwService;
/**
* ContentTypeUseController constructor.
*
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager from the core.
* @param Drupal\uw_cfg_common\Service\UWServiceInterface $uwService
* UW Service.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
UWServiceInterface $uwService
) {
$this->entityTypeManager = $entityTypeManager;
$this->uwService = $uwService;
}
/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('uw_cfg_common.uw_service')
);
}
/**
* Returns a json of the content type usage.
*
* @return Symfony\Component\HttpFoundation\JsonResponse
* A simple renderable array.
*/
public function contentTypeUse(): JsonResponse {
// Get the content types.
$content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
// Fetch the content types in type array.
foreach ($content_types as $content_type) {
$cts[] = $content_type->get('type');
}
// Call to the service to get the count of content types used.
$list = $this->uwService->getContentTypeUsage($cts, TRUE);
return new JsonResponse($list);
}
}
......@@ -540,4 +540,58 @@ class UWService implements UWServiceInterface {
}
}
/**
* {@inheritDoc}
*/
public function getContentTypeUsage(
array $content_types,
bool $show_null_content_types = FALSE
): array {
// Node content types array.
$list = [];
// Step through the content types and get info.
foreach ($content_types as $ct) {
// Query to get all nodes of the content type.
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->condition('type', $ct);
// Get the total number of nodes.
$total = $query->count()->execute();
// If there are no nodes of the content type,
// break out of this iteration, as we do not
// want to display no nodes, however if flag
// is set to show null nodes we will show them.
if ($total == 0 && !$show_null_content_types) {
continue;
}
// Load the content type, we need to use current as this
// will load multiple, current will load the first entry.
$content_type = current(
$this->entityTypeManager->getStorage('node_type')
->loadByProperties(['type' => $ct])
);
// Add the condition of published to query.
$query->condition('status', 1);
// Set the number of published nodes.
$published = $query->count()->execute();
// Set up the list to be used as rows in the table.
$list[$ct] = [
'name' => $content_type->get('name'),
'total' => $total,
'published' => $published,
'unpublished' => $total - $published,
];
}
return $list;
}
}
......@@ -195,4 +195,20 @@ interface UWServiceInterface {
*/
public function uwMonthNameShort(int $month = NULL);
/**
* Function to get the content type usage.
*
* @param array $content_types
* Array of content types.
* @param bool $show_null_content_types
* Flag to show null content type usage.
*
* @return array
* Array of content type usage.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getContentTypeUsage(array $content_types, bool $show_null_content_types): array;
}
......@@ -27,3 +27,10 @@ uw_ldap.view_user:
username: NULL
requirements:
_permission: 'administer site configuration'
uw_cfg_common.content_type_usage_page:
path: '/content_type_use.json'
defaults:
_controller: '\Drupal\uw_cfg_common\Controller\ContentTypeUsePage::contentTypeUse'
_title: 'Content type Use'
requirements:
_permission: 'access content'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment