paid_ads-8.x-1.x-dev/paid_ads.module
paid_ads.module
<?php
/**
* @file
* Module hook and functions.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function paid_ads_help($route_name, RouteMatchInterface $route_match) {
if ($route_name === 'paid_ads.admin.gateway.gateway' && $route_match->getRawParameter('gateway') === 'paypal_gateway') {
$text = t('You should copy here the credentials and mode of one of your applications set in @dash_link', [
'@dash_link' => Link::fromTextAndUrl(t('PayPal Dev Dashboard'), Url::fromUri('https://developer.paypal.com/developer/applications/', [
'absolute' => TRUE,
'attributes' => ['target' => '_blank'],
]))
->toString(),
]);
$output = '';
$output .= '<div>' . $text . '</div>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function paid_ads_theme() {
return [
'paid_ads_paypal_gateway' => [
'variables' => [
'API_key' => NULL,
'amount' => NULL,
'attributes' => [],
'label' => '',
],
],
'paid_boolean_paid' => [
'variables' => [
'item' => NULL,
'text' => '',
],
],
];
}
/**
* Implements hook_entity_field_access().
*/
function paid_ads_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition->getType() == 'paid_boolean') {
if ($account->hasPermission('paid_ads.payment.administer')) {
return AccessResult::allowed();
}
switch ($operation) {
case 'view':
$result = AccessResult::forbidden();
if ($account->hasPermission('paid_ads.field.pay_boolean.any')) {
return AccessResult::allowed();
}
if ($account->hasPermission('paid_ads.field.pay_boolean.own') && $items->getEntity()->uid->target_id === $account->id()) {
$result = AccessResult::allowed();
}
return $result;
case 'edit':
$result = AccessResult::forbidden();
$entity = $items->getEntity();
if ($account->hasPermission('paid_ads.field.edit_paid.' . $entity->bundle())) {
$result = AccessResult::allowed();
}
return $result;
}
}
return AccessResult::neutral();
}
/**
* Implements hook_library_info_build().
*/
function paid_ads_library_info_build() {
$client_id = Drupal::config('paid_ads.settings')
->get('paypal_gateway.client_id');
$libraries['sdk'] = [
'js' => [
"https://www.paypal.com/sdk/js?client-id=$client_id" => [
'type' => 'external',
'preprocess' => FALSE,
'minified' => FALSE,
],
],
];
return $libraries;
}
/**
* Implements template_preprocess_HOOK().
*/
function template_preprocess_paid_boolean_paid(&$variables) {
}
