yoti-8.x-2.5/yoti.module

yoti.module
<?php

/**
 * @file
 * Enables yoti login system capability.
 */

use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Drupal\yoti\YotiHelper;
use Yoti\Entity\Profile;
use Drupal\yoti\Models\YotiUserModel;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Component\Utility\Html;

require_once __DIR__ . '/sdk/boot.php';

/**
 * Display these fields.
 */
function yoti_map_params() {
  return YotiHelper::getUserProfileAttributes();
}

/**
 * Implements hook_theme().
 */
function yoti_theme($existing, $type, $theme, $path) {
  return [
    'yoti_button' => [
      'variables' => [
        'button_id' => NULL,
        'scenario_id' => NULL,
        'client_sdk_id' => NULL,
        'button_text' => NULL,
        'is_linked' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_block().
 */
function yoti_preprocess_block(&$variables) {
  // Append block ID to button ID to ensure uniqueness.
  if ($variables['plugin_id'] == 'yoti_block') {
    $variables['content']['#button_id'] .= '-' . $variables['elements']['#id'];
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function yoti_entity_extra_field_info() {
  $map = yoti_map_params();

  $fields = [];
  foreach ($map as $param => $label) {
    $fields['user']['user']['display'][$param] = [
      'label' => $label,
      'description' => $label,
      'weight' => 10,
    ];
  }

  $fields['user']['user']['display']['yoti_unlink'] = [
    'label' => t('Unlink'),
    'description' => t('Unlink Yoti account'),
    'weight' => 15,
  ];

  return $fields;
}

/**
 * Implements hook_ENTITY_TYPE_view() for user entities.
 */
function yoti_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display, $view_mode) {
  $map = yoti_map_params();

  $user = \Drupal::currentUser();

  $dbProfile = YotiUserModel::getYotiUserById($account->id());
  if (!$dbProfile) {
    return;
  }

  $userProfileArr = unserialize($dbProfile['data']);

  foreach ($map as $field => $label) {
    // Ensure we only display visible fields.
    if (!$display->getComponent($field)) {
      continue;
    }

    $field_content = [];

    if ($field === Profile::ATTR_SELFIE) {
      if ($user->id() !== $account->id() &&
        !$user->hasPermission(YotiHelper::YOTI_PERMISSION_VIEW_SELFIE)
      ) {
        continue;
      }

      // Yoti user selfie file name.
      $selfieFileName = NULL;
      if (isset($userProfileArr[YotiHelper::ATTR_SELFIE_FILE_NAME])) {
        $selfieFileName = $userProfileArr[YotiHelper::ATTR_SELFIE_FILE_NAME];
      }
      $selfieFullPath = YotiHelper::uploadDir() . "/{$selfieFileName}";
      if (!empty($selfieFileName) && is_file($selfieFullPath)) {
        $field_content = [
          '#theme' => 'image',
          '#uri' => Url::fromRoute('yoti.bin-file', [
            'field' => YotiHelper::YOTI_BIN_FIELD_SELFIE,
            'user_id' => $account->id(),
          ])->toString(),
          '#width' => 100,
        ];
      }
    }
    elseif (!empty($userProfileArr[$field])) {
      $field_content['#plain_text'] = $userProfileArr[$field];
    }

    if (empty($field_content)) {
      $field_content['#markup'] = '<i>(empty)</i>';
    }

    $field_content['#prefix'] = '<h4 class="label">' . Html::escape($label) . '</h4> ';

    $build[$field] = [
      '#type' => 'item',
      '#id' => 'yoti-profile-' . $field,
      'content' => $field_content,
    ];
  }

  // Ensure we only display unlink for the current user's profile.
  if (($user->id() === $account->id()) && $display->getComponent('yoti_unlink')) {
    // Build Yoti unlink button.
    $unlinkUrl = Url::fromRoute('yoti.unlink');
    $link_options = [
      'attributes' => [
        'id' => [
          'yoti-unlink-button',
        ],
      ],
    ];
    $unlinkUrl->setOptions($link_options);

    $build['yoti_unlink'] = [
      '#type' => 'item',
      '#markup' => \Drupal::l(t('Unlink Yoti account'), $unlinkUrl),
    ];
  }
}

/**
 * Implements hook_user_login().
 */
function yoti_user_login($account) {
  $activityDetails = YotiHelper::getYotiUserFromStore();
  if ($activityDetails && empty($_SESSION['yoti_nolink']) && !isset($_REQUEST['yoti_nolink'])) {
    // Link user account.
    $helper = \Drupal::service('yoti.helper');
    $helper->createYotiUser($account->id(), $activityDetails);
  }

  // Remove session.
  unset($_SESSION['yoti_nolink']);
  YotiHelper::clearYotiUserStore();
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc