Skip to content
Open

#2709 #404

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions modules/access_misc/access_misc.install
Original file line number Diff line number Diff line change
Expand Up @@ -2254,3 +2254,77 @@ function access_misc_update_10035() {
}
return t('field_rp_description already correct type or does not exist.');
}

/**
* Create inline blocks for tools_case_study layout: Allocation Information - title, Software - Title.
*/
function access_misc_update_10036() {
$storage = \Drupal::entityTypeManager()->getStorage('block_content');
$blocks = [
[
'label' => 'Allocation Information - title',
'body' => '<h2 class="h4 text-base p-2 font-bold leading-5 text-white m-0 bg-md-teal uppercase w-full">Allocation Information</h2>',
],
[
'label' => 'Software - Title',
'body' => '<h2 class="h4 text-base p-2 font-bold leading-5 text-white m-0 bg-md-teal uppercase w-full">Software</h2>',
],
];
$created = [];
foreach ($blocks as $data) {
$existing = $storage->loadByProperties(['info' => $data['label']]);
if (empty($existing)) {
$block = $storage->create([
'type' => 'basic',
'info' => $data['label'],
'body' => [
'value' => $data['body'],
'format' => 'full_html',
],
]);
$block->save();
$created[] = $data['label'];
}
}
if ($created) {
return t('Created blocks: @labels.', ['@labels' => implode(', ', $created)]);
}
return t('Blocks already exist, skipped.');
}

/**
* Create inline blocks for tools_case_study layout: Research Topic - title, Project Summary - title.
*/
function access_misc_update_10037() {
$storage = \Drupal::entityTypeManager()->getStorage('block_content');
$blocks = [
[
'label' => 'Research Topic - title',
'body' => '<h2 class="h4 text-xl p-2 font-bold leading-5 text-dark-teal m-0 bg-light-teal uppercase w-full mt-4">Research Topic</h2>',
],
[
'label' => 'Project Summary - title',
'body' => '<h2 class="h4 text-xl p-2 font-bold leading-5 text-dark-teal m-0 bg-light-teal uppercase w-full mt-4">Project Summary</h2>',
],
];
$created = [];
foreach ($blocks as $data) {
$existing = $storage->loadByProperties(['info' => $data['label']]);
if (empty($existing)) {
$block = $storage->create([
'type' => 'basic',
'info' => $data['label'],
'body' => [
'value' => $data['body'],
'format' => 'full_html',
],
]);
$block->save();
$created[] = $data['label'];
}
}
if ($created) {
return t('Created blocks: @labels.', ['@labels' => implode(', ', $created)]);
}
return t('Blocks already exist, skipped.');
}
103 changes: 103 additions & 0 deletions modules/access_misc/access_misc.module
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ use Drupal\symfony_mailer\Entity\MailerTransport;
use Drupal\views\ViewExecutable;


/**
* Implements hook_theme().
*/
function access_misc_theme() {
return [
'tcs_case_study_researchers_block' => [
'variables' => [
'researchers' => [],
],
'template' => 'tcs-case-study-researchers-block',
],
'tcs_case_study_tools_used_block' => [
'variables' => [
'tools' => [],
],
'template' => 'tcs-case-study-tools-used-block',
],
];
}

/**
* Implements hook_mailer_build().
*
Expand Down Expand Up @@ -199,6 +219,12 @@ function access_misc_views_data() {
* Implements hook_views_pre_render().
*/
function access_misc_views_pre_render(ViewExecutable $view) {
if ($view->id() == 'tool_case_study') {
$view->element['#attached']['library'][] = 'access_misc/copyclip';
}
if ($view->id() == 'tool_case_study_slideshow') {
$view->element['#attached']['library'][] = 'aspTheme/carousel';
}
if ($view->id() == 'infrastructure_news') {
$view->element['#attached']['library'][] = 'access_misc/add_select';
}
Expand Down Expand Up @@ -496,6 +522,83 @@ function access_misc_preprocess_views_view_field(&$variables) {
$variables['output'] = Markup::create($affinity_groups);
}

// Overwrite Access Tools on /tools/researcher-stories page on the
// /admin/structure/views/view/tool_case_study/edit/page_1.
if ($view->id() == 'tool_case_study' && $view->current_display == 'page_1' && $field->options['id'] == 'nothing') {
$current_output = (string) $variables['output'];

if (preg_match('/ACCESS_TOOLS:[\d,\s]+/', $current_output)) {
$node = $variables['row']->_entity ?? NULL;
$replacement = '';

if ($node && $node->hasField('field_tcs_access_tools_used') && !$node->get('field_tcs_access_tools_used')->isEmpty()) {
$items = '';
foreach ($node->get('field_tcs_access_tools_used') as $item) {
$term = $item->entity;
if (!$term) {
continue;
}

$link_url = '';
$image_url = '';

if ($term->hasField('field_tsc_link_to_tool') && !$term->get('field_tsc_link_to_tool')->isEmpty()) {
$link_url = $term->get('field_tsc_link_to_tool')->uri;
}

if ($term->hasField('field_tool_logo') && !$term->get('field_tool_logo')->isEmpty()) {
$file = $term->get('field_tool_logo')->entity;
if ($file) {
$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri());
}
}

if ($link_url && $image_url) {
$alt = htmlspecialchars($term->getName(), ENT_QUOTES, 'UTF-8');
$href = htmlspecialchars($link_url, ENT_QUOTES, 'UTF-8');
$src = htmlspecialchars($image_url, ENT_QUOTES, 'UTF-8');
$items .= '<li class="w-[45%]">';
$items .= '<a href="' . $href . '" target="_blank" rel="noopener noreferrer" class="block">';
$items .= '<img src="' . $src . '" alt="' . $alt . '" class="tcs-tools-used-block__logo">';
$items .= '</a>';
$items .= '</li>';
}
}

if ($items) {
$replacement = '<ul class="tcs-tools-used-block__item flex flex-wrap gap-4 my-5 list-none p-0">' . $items . '</ul>';
}
}

$new_output = preg_replace('/ACCESS_TOOLS:[\d,\s]+/', $replacement, $current_output);
$variables['output'] = Markup::create($new_output);
}
}

// Replace COPYCLIP_GOES_HERE placeholder with a copy-link button on the
// researcher stories page.
if ($view->id() == 'tool_case_study' && $view->current_display == 'page_1' && $field->options['id'] == 'nothing') {
$current_output = (string) $variables['output'];

if (strpos($current_output, 'COPYCLIP_GOES_HERE') === FALSE) {
return;
}

$node = $variables['row']->_entity ?? NULL;
$title = $node ? $node->label() : '';
$clean_id = \Drupal\Component\Utility\Html::getId($title);

$copyclip_html = '<button class="text-sm copyclip ms-4 absolute right-0 z-10" style="top: 0.5rem;" onclick="copyclip(\'https://support.access-ci.org/tools/researcher-stories#' . $clean_id . '\', event)">'
. '<span class="default-message block leading-5 text-dark-teal">'
. '<i class="bi-link"></i><br>Copy link</span>'
. '<span class="copied-message text-dark-teal hidden d-none">'
. '<i class="bi-check"></i><br>Copied!</span>'
. '</button>';

$new_output = str_replace('COPYCLIP_GOES_HERE', $copyclip_html, $current_output);
$variables['output'] = Markup::create($new_output);
}

}

/**
Expand Down
151 changes: 151 additions & 0 deletions modules/access_misc/src/Plugin/Block/TcsCaseStudyResearchersBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Drupal\access_misc\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Displays researchers listed on a tools_case_study node.
*
* @Block(
* id = "tcs_case_study_researchers_block",
* admin_label = @Translation("TCS Case Study Researchers"),
* category = @Translation("Custom"),
* )
*/
class TcsCaseStudyResearchersBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* The file URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrlGenerator;

/**
* Constructs a new TcsCaseStudyResearchersBlock.
*
* @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 $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
* The file URL generator.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
EntityTypeManagerInterface $entity_type_manager,
RouteMatchInterface $route_match,
FileUrlGeneratorInterface $file_url_generator,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
$this->fileUrlGenerator = $file_url_generator;
}

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

/**
* {@inheritdoc}
*/
public function build() {
$node = $this->routeMatch->getParameter('node');
if (!$node || $node->bundle() !== 'tools_case_study') {
return [];
}

if ($node->get('field_tcs_researcher_name')->isEmpty()) {
return [];
}

$researchers = [];
$delta = 0;
foreach ($node->get('field_tcs_researcher_name') as $item) {
/** @var \Drupal\user\UserInterface $user */
$user = $item->entity;
if (!$user) {
$delta++;
continue;
}

$photo_url = NULL;
if ($user->hasField('user_picture') && !$user->get('user_picture')->isEmpty()) {
$file = $user->get('user_picture')->entity;
if ($file) {
$photo_url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());
}
}

$institution = NULL;
if ($node->hasField('field_tcs_institution') && !$node->get('field_tcs_institution')->isEmpty()) {
$institution_item = $node->get('field_tcs_institution')->get($delta)
?? $node->get('field_tcs_institution')->get(0);
if ($institution_item) {
$institution = $institution_item->value;
}
}

$researchers[] = [
'uid' => $user->id(),
'name' => $user->getDisplayName(),
'photo_url' => $photo_url,
'institution' => $institution,
];
$delta++;
}

if (empty($researchers)) {
return [];
}

return [
'#theme' => 'tcs_case_study_researchers_block',
'#researchers' => $researchers,
'#cache' => [
'tags' => $node->getCacheTags(),
'contexts' => ['route'],
],
];
}

}
Loading