simple_paypal_field-8.x-1.1/simple_paypal_field.module

simple_paypal_field.module
<?php

/**
 * @file
 * Module hooks and functions.
 */

use Drupal\Core\Template\Attribute;

/**
 * Implements hook_library_info_build().
 */
function simple_paypal_field_library_info_build() {
  $config = Drupal::config('simple_paypal_field.settings');
  $client_id = $config->get('paypal.mode') === 'live'
    ? ($config->get('paypal.client_id') ?: 'sb')
    : 'sb';
  $libraries['paypal_sdk'] = [
    'js' => [
      "https://www.paypal.com/sdk/js?client-id=$client_id" => [
        'type' => 'external',
        'preprocess' => FALSE,
        'minified' => FALSE,
      ],
    ],
  ];
  return $libraries;
}

/**
 * Implements hook_theme().
 */
function simple_paypal_field_theme($existing, $type, $theme, $path) {
  return [
    'paypal_smart_buttons' => [
      'variables' => [
        'settings' => [],
        'amount' => '0.01',
        'attributes' => [],
      ],
    ],
    'paypal_smart_buttons_formatter' => [
      'variables' => [
        'field' => NULL,
        'field_key' => 0,
        'settings' => [],
      ],
    ],

    'paypal_smart_buttons_formatter_disabled' => [
      'variables' => [
        'item' => NULL,
        'settings' => [],
      ],
    ],
  ];
}

/**
 * Implements template_preprocess_HOOK().
 */
function template_preprocess_paypal_smart_buttons(&$variables) {
  $variables['attributes'] = new Attribute($variables['attributes']);
  $variables['attributes']['data-settings'] = json_encode(
    array_filter(
      $variables['settings']
    )
  );
  $variables['attributes']['data-value'] = $variables['amount'];
}

/**
 * Implements template_preprocess_HOOK().
 */
function template_preprocess_paypal_smart_buttons_formatter(&$variables) {
  /** @var \Drupal\simple_paypal_field\Plugin\Field\FieldType\SimplePayPalField $field */
  $field = $variables['field'];
  $definition = $field->getFieldDefinition();
  $id = implode(
    '-',
    [
      $definition->getTargetEntityTypeId(),
      $definition->getTargetBundle(),
      $definition->getName(),
      $field->getEntity()->id(),
      $variables['field_key'],
    ]
  );
  $attributes = ['id' => $id];
  $variables['buttons'] = [
    '#theme' => 'paypal_smart_buttons',
    '#attributes' => $attributes,
    '#amount' => $field->getDataDefinition()->getSetting('amount'),
    '#settings' => $variables['settings']['button_settings'],
  ];
}

/**
 * Implements template_preprocess_HOOK().
 */
function template_preprocess_paypal_smart_buttons_formatter_disabled(&$variables) {
}

/**
 * Implements hook_field_widget_info_alter().
 */
function simple_paypal_field_field_widget_info_alter(array &$info) {
  $info['boolean_checkbox']['field_types'][] = 'simple_paypal_field';
  $info['boolean_checkbox']['field_types'][] = 'auto_off_paypal_field';
}

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

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