-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #215: Create block bc_dc_content_summary
- Loading branch information
Showing
1 changed file
with
120 additions
and
0 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
html/modules/custom/bc_dc/src/Plugin/Block/BcDcContentSummary.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |