certificate-4.0.0-alpha1/certificate.api.php
certificate.api.php
<?php
/**
* @file certificate.api.php
* Document certificate hooks.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Implementation of hook_entity_access().
*
* Any module wishing to award a certificate based on arbitrary criteria should
* implement this hook. The $entity is the entity in question, the $account is
* the user viewing the entity.
*
* Implementations should check that $operation == 'certificate'
*
* @return \Drupal\Core\Access\AccessResult
* Forbidden if user should not be shown the menu tab or link.
* Allowed if user should be able to download a certificate.
*/
function hook_entity_access(EntityInterface $entity, $operation, AccountInterface $user) {
if ($operation == 'certificate') {
if (get_score($user) > passing_score($entity)) {
return \Drupal\Core\Access\AccessResult::allowed();
}
else {
return \Drupal\Core\Access\AccessResult::forbidden();
}
}
}
/**
* Implementation of certificate_template_id_alter().
*
* Modify the valid certificates.
*
* @param $template_ids
* Array of valid certificates.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity generating the certificate.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account generating the certificate.
*/
function hook_certificate_template_id_alter(&$template_ids, EntityInterface $entity, AccountInterface $account) {
if ($entity->id() % 2 == 0) {
// Set certificate to use mycert_1 when the ID is even.
$template_ids = ['mycert_1'];
}
}
