unlock-8.x-1.0/unlock.module
unlock.module
<?php
/**
* @file
* Contains unlock.module.
*/
use Drupal\block\Entity\Block;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\unlock\Form\SettingsForm;
/**
* Implements hook_theme().
*/
function unlock_theme() {
return [
'unlock_button' => [
'variables' => [
'label' => '',
'type' => 'show',// hide
'level' => 'login',// checkout
],
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function unlock_theme_suggestions_HOOK(array $variables) {
$suggestions = [];
$type = $variables['type'];
$level = $variables['level'];
$suggestions[] = 'unlock_button__' . $type;
$suggestions[] = 'unlock_button__' . $level;
$suggestions[] = 'unlock_button__' . $type . '__' . $level;
return $suggestions;
}
/**
* Implements hook_form_alter().
*/
function unlock_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$route_name = Drupal::routeMatch()->getRouteName();
[$type, , $form_name] = array_pad(explode('.', $route_name), 3, NULL);
$is_field_config = $form_id === 'field_config_edit_form';
if (($type === 'entity') && (($form_name === 'edit_form') || $is_field_config)) {
$entity = $form_state->getFormObject()->getEntity();
if ($entity instanceof ThirdPartySettingsInterface) {
$form['third_party_settings']['#tree'] = TRUE;
$form['third_party_settings']['unlock'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => t('Unlock Feature'),
'#group' => 'additional_settings',
];
$form['third_party_settings']['unlock']['status'] = [
'#type' => 'checkbox',
'#title' => t('Active'),
'#default_value' => $entity->getThirdPartySetting('unlock', 'status'),
];
$form['third_party_settings']['unlock']['type'] = [
'#type' => 'radios',
'#title' => t('Until paid'),
'#default_value' => $entity->getThirdPartySetting('unlock', 'type') ?: 0,
'#options' => [
0 => t('Hidden'),
1 => t('Visible'),
],
'#required' => TRUE,
];
$network = [];
$network[0] = t('- Global -');
$network = array_merge($network, SettingsForm::NETWORKS);
$form['third_party_settings']['unlock']['network'] = [
'#type' => 'select',
'#title' => t('Network'),
'#options' => $network,
'#description' => t('You can configure global network settings <a href="@url">there</a>', ['@url' => Url::fromRoute('unlock.settings', [], ['query' => Drupal::service('redirect.destination')->getAsArray()])->toString()]),
'#default_value' => $entity->getThirdPartySetting('unlock', 'network'),
];
$form['third_party_settings']['unlock']['lock_address'] = [
'#type' => 'textfield',
'#title' => t('Lock address'),
'#description' => t('This will be a blockchain address starting with "0x…"'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $entity->getThirdPartySetting('unlock', 'lock_address'),
];
$form['third_party_settings']['unlock']['login_text'] = [
'#type' => 'textfield',
'#title' => t('Login text'),
'#description' => t('This text will be displayed for anonymous users. You can use "@text" for make only part of text as link. In other case all text string will be transformed to link. You can use "@type" placeholder for "hide/show" word replacement according to "Until paid" setting.', ['@text' => '<link>text</link>']),
'#maxlength' => 500,
'#default_value' => $entity->getThirdPartySetting('unlock', 'login_text') ?: SettingsForm::LOGIN_TEXT,
];
$form['third_party_settings']['unlock']['checkout_text'] = [
'#type' => 'textfield',
'#title' => t('Checkout text'),
'#description' => t('This text will be displayed for pay for hide/show information. You can use "@text" for make only part of text as link. In other case all text string will be transformed to link. You can use "@type" placeholder for "hide/show" word replacement according to "Until paid" setting.', ['@text' => '<link>text</link>']),
'#maxlength' => 500,
'#default_value' => $entity->getThirdPartySetting('unlock', 'checkout_text') ?: SettingsForm::CHECKOUT_TEXT,
];
$form['#validate'][] = 'unlock_config_form_validate';
}
}
}
function unlock_config_form_validate($form, FormStateInterface $form_state) {
$values = $form_state->getValue('third_party_settings')['unlock'] ?? '';
$urls = Drupal::state()->get('unlock_config_urls');
$url = Url::fromRouteMatch(Drupal::routeMatch())->toString();
if (!empty($values['status'])) {
$urls[$url] = $url;
if (empty($values['lock_address'])) {
$form_state->setErrorByName('third_party_settings][unlock][lock_address', t('Lock address can not be empty when activated Unlock feature.'));
}
}
else {
unset($urls[$url]);
}
Drupal::state()->set('unlock_config_urls', $urls);
}
function unlock_preprocess_block(&$variables) {
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if (!($block instanceof ThirdPartySettingsInterface)) {
return;
}
if (!$block->getThirdPartySetting('unlock', 'status')) {
return;
}
$visible_until_paid = $block->getThirdPartySetting('unlock', 'type');
$unlock_button = Drupal::service('unlock.client')->getUnlockButton($block);
if (!$visible_until_paid) {
// Hidden until paid
if ($unlock_button) {
$variables['content'] = $unlock_button;
}
}
else {
// Visible until paid
if ($unlock_button) {
$variables['content'] = [
'main' => $variables['content'],
'unlock' => $unlock_button,
];
// $variables['content']['unlock_button'] = $unlock_button;
// $variables['content']['unlock_button']['#weight'] = -100;
}
else {
$variables['content'] = [];
}
}
}
}
/**
* Implements hook_entity_view().
*/
function unlock_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($entity->getEntityType() && $entity->bundle()) {
try {
$entity_type_instance = Drupal::entityTypeManager()
->getStorage($entity->getEntityType()->getBundleEntityType())
->load($entity->bundle());
} catch (Throwable $exception) {
return;
}
if (!($entity_type_instance instanceof ThirdPartySettingsInterface)) {
return;
}
if (!$entity_type_instance->getThirdPartySetting('unlock', 'status')) {
// Check lock by field
try {
_unlock_by_field($build);
} catch (Throwable $exception) {
}
return;
}
// Lock by content type
$visible_until_paid = $entity_type_instance->getThirdPartySetting('unlock', 'type');
$unlock_client = Drupal::service('unlock.client');
$unlock_button = $unlock_client->getUnlockButton($entity_type_instance);
if (!$visible_until_paid) {
// Hidden until paid
if ($unlock_button) {
foreach (Element::children($build) as $child) {
$build[$child]['#access'] = FALSE;
}
$build['unlock'] = $unlock_button;
}
}
else {
// Visible until paid
if ($unlock_button) {
$build['unlock'] = $unlock_button;
$build['unlock']['#weight'] = 100;
}
else {
foreach (Element::children($build) as $child) {
$build[$child]['#access'] = FALSE;
}
}
}
}
}
/**
* @param array $build
*
* @return array
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function _unlock_by_field(array &$build): array {
$unlock_client = Drupal::service('unlock.client');
$field_storage = Drupal::entityTypeManager()
->getStorage('field_config');
foreach (Element::children($build) as $child) {
$field_config_id = [
$build[$child]['#entity_type'] ?? '',
$build[$child]['#bundle'] ?? '',
$build[$child]['#field_name'] ?? '',
];
$field_config_id = implode('.', $field_config_id);
$field_config = $field_storage->load($field_config_id);
if ($field_config) {
if (!($field_config instanceof ThirdPartySettingsInterface)) {
continue;
}
if (!$field_config->getThirdPartySetting('unlock', 'status')) {
continue;
}
$visible_until_paid = $field_config->getThirdPartySetting('unlock', 'type');
$unlock_button = $unlock_client->getUnlockButton($field_config);
if (!$visible_until_paid) {
if ($unlock_button) {
// hide field
$build[$child] = $unlock_button;
}
}
else {
if ($unlock_button) {
// show field
$build[$child][] = $unlock_button;
}
else {
$build[$child]['#access'] = FALSE;
}
}
}
}
return $build;
}
