Skip to content

Commit

Permalink
Issue #215: Create block bc_dc_content_summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Brockbank authored and lkmorlan committed Dec 12, 2023
1 parent 8308b26 commit c1eac7c
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions html/modules/custom/bc_dc/src/Plugin/Block/BcDcContentSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Drupal\bc_dc\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a content summary block for the dashboard.
*
* @Block(
* id = "bc_dc_content_summary",
* admin_label = @Translation("Dashboard content summary"),
* context_definitions = {
* "user" = @ContextDefinition("entity:user")
* }
* )
*/
class BcDcContentSummary extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity_type.manager service.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
);
}

/**
* {@inheritdoc}
*/
public function build() {
$user = $this->getContextValue('user');
$nodeStorage = $this->entityTypeManager->getStorage('node');

$build = [];

$query = $nodeStorage
->getQuery()
->condition('status', 1)
->condition('type', 'data_set')
->condition('uid', $user->id())
->accessCheck(FALSE);
$nids = $query->execute();

if ($nids) {
$total_nodes = count($nids);
$total_bookmarks = 0;
foreach ($nids as $nid) {
$node = $nodeStorage->load($nid);
$total_bookmarks += bc_dc_count_node_bookmarks($node);
}

$args = [
'@count' => $total_nodes,
'@bookmarks' => $total_bookmarks,
];
$build['message'] = [
'#type' => 'markup',
'#markup' => $this->t('You have @count published metadata records that have been bookmarked @bookmarks times.', $args),
'#prefix' => '<p class="p-2">',
'#suffix' => '</p>',
];
}
else {
$build['message'] = [
'#type' => 'markup',
'#markup' => $this->t('You currently have no published metadata records.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
}

$options = [
'attributes' => [
'class' => [
'btn',
'btn-primary',
],
],
];
$url = Url::fromRoute('bc_dc.user_manage_tab', ['user' => $user->id()], $options);
$build['link'] = [
'#type' => 'link',
'#url' => $url,
'#title' => $this->t('Manage my published meta data records'),
];

return $build;
}

}

0 comments on commit c1eac7c

Please sign in to comment.