badgr_badge-8.x-1.x-dev/badgr_badge.module
badgr_badge.module
<?php
/**
* @file
* Contains badgr_badge.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_help().
*/
function badgr_badge_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the badgr_badge module.
case 'help.page.badgr_badge':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Bridge between drupal and badgr api service') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function badgr_badge_theme() {
return [
'badgr_badge' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function badgr_badge_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($entity->getType() == 'badgr_badges' && $view_mode == 'teaser') {
$link = 'badgr_badge.addtobackpack';
$build['backpack_ajax_link'] = [
'#type' => 'link',
'#title' => t('Backpack link for @title', ['@title' => $entity->getTitle()]),
'#attached' => ['library' => ['core/drupal.ajax']],
'#attributes' => ['class' => ['use-ajax']],
'#url' => Url::fromRoute($link, ['nojs' => 'ajax', 'badge' => $entity->id(), 'acheived_date' => time()]),
'#prefix' => '<div class="backpack-link backpack-link-' . $entity->id() . ' btn btn-info">',
'#suffix' => '</div>',
];
$build['backpack_ajax_link']['destination'] = [
'#type' => 'container',
'#attributes' => ['id' => ["backpack{$entity->id()}"], 'class' => 'btn'],
];
$build['#cache']['max-age'] = 0;
}
}
/**
* Implements hook_preprocess_node().
*/
function badgr_badge_preprocess_node(&$variables) {
// Get the node's content type
$type = $variables['node']->getType();
// Get its view mode
$mode = $variables['view_mode'];
if ($type == 'badgr_badges' && $mode == 'teaser') {
$variables['display_submitted'] = FALSE;
}
}
