DocumentsLastGroupBlock.php in Opigno Learning path 3.x
File
src/Plugin/Block/DocumentsLastGroupBlock.php
View source
<?php
namespace Drupal\opigno_learning_path\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
class DocumentsLastGroupBlock extends BlockBase {
protected $groupId;
public function build() {
$this->groupId = $gid = $this->configuration["group"];
$tid = _tft_get_group_tid($gid);
$content = _tft_folder_content($tid, FALSE, $gid);
$content = array_slice($content ?: [], 0, 4);
foreach ($content as $index => $item) {
$content[$index] = [
'#theme' => 'opigno_documents_last_group_item',
'#type' => $item["type"] == 'file' ? 'file' : 'folder',
'#item' => $item,
'#label' => $item["name"],
'#link' => $this
->itemLink($item),
];
}
$build['content'] = [
'#theme' => 'opigno_documents_last_group_block',
'content' => $content,
];
return $build;
}
public function itemLink($item) {
if ($item["type"] == 'file') {
$tft_url = Url::fromUri("internal:/tft/download/file/{$item['id']}")
->toString();
}
else {
$tft_url = Url::fromRoute('tft.group', [
'group' => $this->groupId,
], [
'fragment' => "term/{$item['id']}",
])
->toString();
}
return $tft_url;
}
}