You are here

phone_international.module in International Phone 3.x

Same filename and directory in other branches
  1. 8.2 phone_international.module
  2. 8 phone_international.module

File

phone_international.module
View source
<?php

/**
 * @file
 * Contains phone_international.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function phone_international_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the sb_testing module.
    case 'help.page.phone_international':
      $text = file_get_contents(dirname(__FILE__) . '/README.md');
      if (!Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = Drupal::service('plugin.manager.filter');
        $settings = Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
    default:
  }
}

/**
 * Implements hook_library_info_alter().
 */
function phone_international_library_info_alter(array &$libraries, $module) {
  $config = \Drupal::config('phone_international.settings');
  $cdn = $config
    ->get('cdn');
  if ('phone_international' == $module && !$cdn) {
    if (isset($libraries['phone_international'])) {

      // Set intl-tel-input library path.
      $intltel_path = _phone_international_get_path();
      $minified = file_exists($intltel_path . '/js/intlTelInput.min.js');
      $intltel_js_path = '/' . $intltel_path . '/js';
      $intltel_js = $minified ? $intltel_js_path . '/intlTelInput.min.js' : $intltel_js_path . '/intlTelInput.js';
      $libraries['phone_international.general']['js'] = [
        $intltel_js => [
          'minified' => $minified,
        ],
      ];
      $minified_css = file_exists($intltel_path . '/css/intlTelInput.min.css');
      $intltel_css_path = '/' . $intltel_path . '/css';
      $intltel_css = $minified_css ? $intltel_css_path . '/intlTelInput.min.css' : $intltel_css_path . '/intlTelInput.css';
      $libraries['phone_international.general']['css']['theme'] = [
        $intltel_css => [
          'minified' => $minified,
        ],
      ];
    }
  }
}

/**
 * Get the location of the intl-tel-input library.
 *
 * @return string
 *   The location of the library, or FALSE if the library isn't installed.
 */
function _phone_international_get_path() {
  if (function_exists('libraries_get_path')) {
    return libraries_get_path('intl-tel-input');
  }

  // The following logic is taken from libraries_get_libraries()
  $searchdir = [];

  // Similar to 'modules' and 'themes' directories inside an installation
  // profile, installation profiles may want to place libraries into a
  // 'libraries' directory.
  $searchdir[] = 'profiles/' . \Drupal::installProfile() . '/libraries';

  // Always search libraries.
  $searchdir[] = 'libraries';

  // Also search sites/<domain>/*.
  $searchdir[] = \Drupal::service('site.path') . '/libraries';
  foreach ($searchdir as $dir) {
    if (file_exists($dir . '/intl-tel-input/build/js/intlTelInput.min.js') || file_exists($dir . '/intl-tel-input/build/js/intlTelInput.js')) {
      return $dir . '/intl-tel-input/build';
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
phone_international_help Implements hook_help().
phone_international_library_info_alter Implements hook_library_info_alter().
_phone_international_get_path Get the location of the intl-tel-input library.