function CertificateController::certificatePage in Certificate 4.x
Full tab
Parameters
$entity:
AccountInterface $account:
Return value
type
File
- src/
Controller/ CertificateController.php, line 83
Class
Namespace
Drupal\certificate\ControllerCode
function certificatePage(AccountInterface $account = NULL) {
if (!($entity = $this
->getEntityFromRoute())) {
return AccessResult::neutral();
}
$account = \Drupal::currentUser();
// Get all templates for this entity combo
$render = [];
$valid_certs = [];
$global_certs = CertificateMapping::getGlobalCertificateMappings();
$certificate_mappers = Drupal::service('plugin.manager.certificate_mapper');
$map_defs = $certificate_mappers
->getDefinitions();
/* @todo add find field name function */
foreach ($entity
->getFields() as $field) {
if ($field
->getFieldDefinition()
->getType() == 'entity_reference' && $field
->getSetting('target_type') == 'certificate_mapping') {
$certs = $field
->referencedEntities();
}
}
//Default to load a page
$render['info']['#markup'] = '';
foreach ($map_defs as $map_key => $maps) {
$plugin = $certificate_mappers
->createInstance($map_key, [
'of' => 'configuration values',
]);
$matches = $plugin
->processMapping($entity, $account) ?? [];
foreach ($matches as $match) {
foreach ($certs as $local) {
if ($local
->isMatch($map_key, $match)) {
$valid_certs["{$map_key}.{$match}"][] = $local
->get('cid')->value;
}
}
// If local is not set, check the global mappings
if (!isset($valid_certs["{$map_key}.{$match}"])) {
foreach ($global_certs as $global) {
if ($global
->isMatch($map_key, $match) && $global
->get('cid')->value !== '-1') {
$valid_certs["{$map_key}.{$match}"][] = $global
->get('cid')->value;
}
}
}
elseif ($valid_certs["{$map_key}.{$match}"] == '-1') {
unset($valid_certs["{$map_key}.{$match}"]);
}
}
}
if (empty($valid_certs)) {
$render['info']['#markup'] = t('Sorry, there is no certificate available.');
return $render;
}
// Return single certs right away
if (count($valid_certs) === 1 && count(reset($valid_certs)) === 1) {
$template = CertificateTemplate::load(current(reset($valid_certs)));
return $this
->certificateDownload($entity, $account, $template);
}
// Return markup if we need to present messages
$render['info']['#markup'] = t('You are eligible for multiple certificates.');
$render['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Type'),
$this
->t('Download'),
],
];
foreach ($valid_certs as $cert_name => $vals) {
foreach ($vals as $val) {
$params = [
$entity
->getEntityTypeId() => $entity
->id(),
'user' => $account
->id(),
'certificate_template' => $val,
];
$render['table'][$val] = [
'type' => [
'#markup' => $cert_name,
],
'download' => Link::createFromRoute(t('Download certificate'), "certificate.{$entity->getEntityTypeId()}.pdf", $params)
->toRenderable(),
];
}
}
return $render;
}