grant-1.x-dev/grant.module
grant.module
<?php
/**
* @file
* Primary module hooks for Grant module.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function grant_grant_insert(EntityInterface $entity) {
\Drupal::service('grant.main')->grantChangeCacheClear($entity);
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function grant_grant_update(EntityInterface $entity) {
\Drupal::service('grant.main')->grantChangeCacheClear($entity);
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function grant_grant_delete(EntityInterface $entity) {
\Drupal::service('grant.main')->grantChangeCacheClear($entity);
}
/**
* Implements hook_entity_extra_field_info().
*/
function grant_entity_extra_field_info() {
$extra = [];
$configs = \Drupal::service('grant.main')->getGrantConfigs();
foreach ($configs['entities'] as $type => $type_config) {
foreach ($type_config as $bundle => $config) {
$invite_link = $config['assign_invite_link'] ?? FALSE;
if ($invite_link) {
$extra[$type][$bundle]['display']['grant_invite_link'] = [
'label' => t('Grant invite link'),
'description' => t('Open grant invite form.'),
'weight' => 10,
];
}
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*/
function grant_entity_view(
array &$build,
EntityInterface $entity,
EntityViewDisplayInterface $display,
$view_mode,
) {
// Don't show on previews.
if ($entity->isNew()) {
return;
}
$grant_configs = \Drupal::service('grant.main')->getGrantConfigs();
$e_type = $entity->getEntityTypeId();
if (in_array($e_type, array_keys($grant_configs['entities']))) {
$e_bundle = $entity->bundle();
$config = $grant_configs['entities'][$e_type][$e_bundle] ?? [];
$show_invite_link = $config['assign_invite_link'] ?? FALSE;
if ($config == [] || $show_invite_link == FALSE) {
return;
}
$e_uuid = $entity->uuid();
$access = FALSE;
if ($show_invite_link) {
$assign_control = $config['assign_control'] ?? [];
if ($assign_control != []) {
$access = \Drupal::service('grant.main')
->userAssignedGrantHasPermission(
'current',
'grant-assign invite',
$e_type,
$e_uuid,
array_keys($assign_control),
);
}
}
if ($access) {
$link_text = t('Grant invite', ["%uuid" => $e_uuid]);
$url = Url::fromRoute("grant.grant_invite", [
'type' => $e_type,
'uuid' => $e_uuid,
]);
$link = Link::fromTextAndUrl($link_text, $url);
$string = $link->toString();
// @todo Change to template logic.
$markup = '<span class="grant-invite-link">';
$markup .= $string->getGeneratedLink();
$markup .= '</span>';
$build['grant_invite_link'] = ['#markup' => $markup];
}
}
}
