global_gateway-8.x-1.x-dev/modules/global_gateway_address/global_gateway_address.module

modules/global_gateway_address/global_gateway_address.module
<?php

/**
 * @file
 * Basic module file.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_field_info_alter().
 */
function global_gateway_address_field_info_alter(&$info) {
  $info['address']['class']         = '\Drupal\global_gateway_address\Plugin\Field\FieldType\AddressItem';
  $info['address_country']['class'] = '\Drupal\global_gateway_address\Plugin\Field\FieldType\CountryItem';
  $info['address']['provider']         = 'global_gateway_address';
  $info['address_country']['provider'] = 'global_gateway_address';
}

/**
 * Implements hook_field_widget_form_alter().
 */
function global_gateway_address_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  // Prevent country detection for the update/edit node forms
  // and translations form.
  $path = \Drupal::service('path.current')->getPath();
  if (stripos($path, '/edit') !== FALSE
    || stripos($path, '/translations') !== FALSE
  ) {
    return FALSE;
  }
  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items            = $context['items'];
  $field_definition = $items->getFieldDefinition();
  $settings         = $field_definition->getSettings();
  $type             = $field_definition->getType();
  if ($type === 'address') {
    $country = &$element['address']['#default_value']['country_code'];
  }
  elseif ($type === 'address_country') {
    $country = &$element['value']['#default_value'];
  }

  // Don't run detection when current field isn't an address field,
  // or user set default value for widget.
  if (empty($settings['preselect_user_region_enabled']) || !empty($country)) {
    return FALSE;
  }

  if (in_array($type, ['address', 'address_country'])) {
    // Negotiate the country code.
    $country = \Drupal::service('global_gateway_region_negotiator')
      ->negotiateRegion();
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function global_gateway_address_form_field_config_edit_form_alter(&$form) {
  static $method = 'submitPreselectSetting';
  // Move the preselect option checkbox to the default value container.
  $type = \Drupal::routeMatch()->getParameters()->get('field_config')->getType();
  if ($type == 'address_country') {
    if (isset($form['settings']) && isset($form['settings']['preselect_user_region_enabled'])) {
      $element = $form['settings']['preselect_user_region_enabled'];
      unset($form['settings']['preselect_user_region_enabled']);
      $form['default_value']['widget'][0]['value']['#states'] = [
        'visible' => [
          ':input[name="default_value_input[preselect_user_region_enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
      ];
      $form['default_value']['preselect_user_region_enabled'] = $element;
      $submit_class = '\Drupal\global_gateway_address\Plugin\Field\FieldType\CountryItem';
      $form['actions']['submit']['#submit'][] = [$submit_class, $method];
    }
  }
  elseif ($type == 'address') {
    $element = $form['settings']['preselect_user_region_enabled'];
    unset($form['settings']['preselect_user_region_enabled']);
    $form['default_value'] = [
      '#type'                         => 'details',
      '#parents'                      => ['default_value_input'],
      '#title'                        => t('Default value'),
      '#description'                  => t('The default value for this field, used when creating new content.'),
      '#open'                         => TRUE,
      '#tree'                         => TRUE,
      'preselect_user_region_enabled' => $element,
    ];
    $submit_class = '\Drupal\global_gateway_address\Plugin\Field\FieldType\AddressItem';
    $form['actions']['submit']['#submit'][] = [$submit_class, $method];
  }
}

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

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