You are here

responsive_menu.install in Responsive and off-canvas menu 4.0.x

Contains install and update functions.

File

responsive_menu.install
View source
<?php

/**
 * @file
 * Contains install and update functions.
 */

/**
 * Add new config for the breakpoint css file path.
 */
function responsive_menu_update_8001() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('breakpoint_css_filepath', 'public://css')
    ->save(TRUE);
}

/**
 * Add new config to determine whether to use the breakpoint.
 */
function responsive_menu_update_8002() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('use_breakpoint', TRUE)
    ->save(TRUE);
}

/**
 * Add new config to determine whether to use the breakpoint.
 */
function responsive_menu_update_8003() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('allow_admin', FALSE)
    ->save(TRUE);
}

/**
 * Add new config for page wrappers.
 */
function responsive_menu_update_8004() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('wrapper_admin', TRUE)
    ->save(TRUE);
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('wrapper_theme', FALSE)
    ->save(TRUE);
}

/**
 * Add new config for keyboard extension.
 */
function responsive_menu_update_8005() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('extension_keyboard', TRUE)
    ->save(TRUE);
}

/**
 * Add new config for pagedim setting.
 */
function responsive_menu_update_8006() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->set('pagedim', 'pagedim')
    ->save(TRUE);
}

/**
 * Delete keyboard extension config.
 */
function responsive_menu_update_8007() {
  \Drupal::configFactory()
    ->getEditable('responsive_menu.settings')
    ->clear('extension_keyboard')
    ->save();
}

/**
 * Implements hook_requirements().
 */
function responsive_menu_requirements($phase) {
  $requirements = [];
  if ($phase !== 'runtime') {
    return $requirements;
  }

  // Check whether the libraries are installed.
  $libraries = [
    'mmenu' => [
      'title' => 'mmenu library',
      'version' => '8.4.0',
      'path' => '/libraries/mmenu',
      'version_path' => '/libraries/mmenu/package.json',
      'severity' => REQUIREMENT_ERROR,
      'download_url' => 'https://github.com/FrDH/mmenu-js/releases/latest',
      'homepage_url' => 'http://mmenu.frebsite.nl',
    ],
    'superfish' => [
      'title' => 'superfish library',
      'version' => '1.7.0',
      'path' => '/libraries/superfish',
      'version_path' => '/libraries/superfish/package.json',
      'severity' => REQUIREMENT_WARNING,
      'download_url' => 'https://api.github.com/repos/joeldbirch/superfish/zipball',
      'homepage_url' => 'https://superfish.joelbirch.co',
    ],
    'hammerjs' => [
      'title' => 'hammerjs library',
      'version' => '2.0.7',
      'path' => '/libraries/hammerjs',
      'version_path' => '/libraries/hammerjs/hammer.min.js',
      'severity' => REQUIREMENT_WARNING,
      'download_url' => 'https://api.github.com/repos/hammerjs/hammer.js/zipball',
      'homepage_url' => 'http://hammerjs.github.io',
    ],
  ];
  foreach ($libraries as $key => $library) {
    if (isset($library['version'])) {
      $version = responsive_menu_get_library_version($library['version_path']);
    }
    else {
      $version = $library['title'];
    }
    $t_args = [
      '@title' => $library['title'],
      '@version' => $version,
      '@required' => isset($library['version']) ? $library['version'] : '',
      '@path' => $library['path'],
      ':download_href' => $library['download_url'],
      ':homepage_href' => $library['homepage_url'],
    ];

    // Does this library exist?
    $library_exists = file_exists(DRUPAL_ROOT . $library['path']);

    // Does the library meet the version requirements?
    if (isset($library['version'])) {
      $library_installed = $library_exists && responsive_menu_check_library_version($version, $library['version']);
    }
    else {
      $library_installed = $library_exists;
    }

    // The requirements array for rendering.
    $requirements['responsive_menu' . $key] = [
      'title' => 'Responsive menu: ' . $library['title'],
      'value' => $library_installed ? t('@version (Installed)', $t_args) : t('@version (Not installed)', $t_args),
      'severity' => $library_installed ? REQUIREMENT_OK : $library['severity'],
    ];
    if (!$library_installed) {
      if ($library_exists && $version > 0) {
        $requirements['responsive_menu' . $key]['value'] = t('Version @version is installed but @required is required', $t_args);
      }
      else {
        $requirements['responsive_menu' . $key]['value'] = t('Library not installed');
      }
      $requirements['responsive_menu' . $key]['description'] = t('The @title must be installed at @path and must be version @required or greater. The download url is :download_href', $t_args);
    }
  }
  return $requirements;
}

/**
 * Returns a version string.
 *
 * @param string $version_path
 *   The path to the file containing the version.
 *
 * @return string
 *   A version string or '0' if not found.
 */
function responsive_menu_get_library_version($version_path) {
  if (!file_exists(DRUPAL_ROOT . $version_path)) {
    return '0';
  }

  // The hammerjs library does not contain the version number in the
  // package.json file.
  if (strpos($version_path, '.json') === FALSE) {
    preg_match('/\\sv(.+?)\\s/', file_get_contents(DRUPAL_ROOT . $version_path), $matches);
    return isset($matches[1]) ? $matches[1] : '0';
  }

  // Otherwise load the package.json file and process.
  $json = file_get_contents(DRUPAL_ROOT . $version_path);
  if (!$json) {
    return '0';
  }
  $package_array = json_decode($json, TRUE);
  if (isset($package_array['version'])) {
    return $package_array['version'];
  }
  return '0';
}

/**
 * Checks a version string against a requirement string.
 *
 * @param string $version
 *   The discovered version to check.
 * @param string $requirement
 *   The required version to check against.
 *
 * @return bool
 *   Success or failure boolean.
 */
function responsive_menu_check_library_version($version, $requirement) {
  if ($version >= $requirement) {
    return TRUE;
  }
  return FALSE;
}

Functions

Namesort descending Description
responsive_menu_check_library_version Checks a version string against a requirement string.
responsive_menu_get_library_version Returns a version string.
responsive_menu_requirements Implements hook_requirements().
responsive_menu_update_8001 Add new config for the breakpoint css file path.
responsive_menu_update_8002 Add new config to determine whether to use the breakpoint.
responsive_menu_update_8003 Add new config to determine whether to use the breakpoint.
responsive_menu_update_8004 Add new config for page wrappers.
responsive_menu_update_8005 Add new config for keyboard extension.
responsive_menu_update_8006 Add new config for pagedim setting.
responsive_menu_update_8007 Delete keyboard extension config.