You are here

context_menu_block.install in Context: Menu Block 7.3

Same filename and directory in other branches
  1. 6.3 context_menu_block.install

Install file for Context: Menu Block.

File

context_menu_block.install
View source
<?php

/**
 * @file
 * Install file for Context: Menu Block.
 */

/**
 * Defines.
 */
define('CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED', '7.x-3.x');

/**
 * Implementation of hook_enable().
 */
function context_menu_block_enable() {
  $t = get_t();
  $info = drupal_parse_info_file(drupal_get_path('module', 'context') . '/context.info');

  // Check to see if Context's version is defined in its .info file.
  // @see context_menu_block_requirements()
  if (!isset($info['version'])) {
    variable_set('context_menu_block_validate', TRUE);
    drupal_set_message($t('Context does not have a version defined in its .info file. This could be because you are using a checkout of its git repository. For Context: Menu Block support, make sure you are using the @branch branch of Context.', array(
      '@branch' => CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED,
    )), 'warning');
  }
  elseif (!_context_menu_block_validate_context($info['version'])) {
    drupal_set_message($t('Context: Menu Block requires Context @branch. Update Context to continue using Context: Menu Block.', array(
      '@branch' => CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED,
    )), 'error');
  }
}

/**
 * Implementation of hook_requirements().
 */
function context_menu_block_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $t = get_t();
    $info = drupal_parse_info_file(drupal_get_path('module', 'context') . '/context.info');

    // Check to see if Context's version is defined in its .info file.
    // If it isn't, assume it's a git checkout and that the user knows what
    // they're doing.
    if (!isset($info['version'])) {
      variable_set('context_menu_block_validate', TRUE);
      $requirements['context_menu_block'] = array(
        'title' => $t('Context'),
        'value' => $t('Version tag not found'),
        'severity' => REQUIREMENT_WARNING,
        'description' => $t('Context does not have a version defined in its .info file. This could be because you are using a checkout of its git repository. For Context: Menu Block support, make sure you are using the @branch branch of Context.', array(
          '@branch' => CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED,
        )),
      );
      return $requirements;
    }
    $requirements['context_menu_block'] = array(
      'title' => $t('Context'),
      'value' => $info['version'],
    );
    if (_context_menu_block_validate_context($info['version'])) {
      $requirements['context_menu_block']['severity'] = REQUIREMENT_OK;
      $requirements['context_menu_block']['description'] = $t('The correct version of Context is enabled for use with Context: Menu Block.');
    }
    else {
      $requirements['context_menu_block']['severity'] = REQUIREMENT_ERROR;
      $requirements['context_menu_block']['description'] = $t('The version of Context enabled is not supported with Context: Menu Block. Download and enable the @branch branch of Context.', array(
        '@branch' => CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED,
      ));
    }
  }
  return $requirements;
}

/**
 * Implementation of hook_uninstall().
 */
function context_menu_block_uninstall() {
  variable_del('context_menu_block_validate');
}

/**
 * Validate the version of Context enabled.
 *
 * @param $version
 *   The version of Context enabled.
 *
 * @return
 *   TRUE if the correct version of Context is enabled, FALSE otherwise.
 */
function _context_menu_block_validate_context($version) {
  $module_version = _context_menu_block_parse_version($version);
  $supported_version = _context_menu_block_parse_version(CONTEXT_MENU_BLOCK_CONTEXT_SUPPORTED);
  if ($module_version['module']['major'] == $supported_version['module']['major']) {
    variable_set('context_menu_block_validate', TRUE);
    return TRUE;
  }
  else {
    variable_set('context_menu_block_validate', FALSE);
    return FALSE;
  }
}

/**
 * Parse a Drupal version string into its constituent parts.
 *
 * @param $version
 *   A version string in standard Drupal format (e.g. 6.x-2.x).
 *
 * @return
 *   An array containing the version information broken down by core and module.
 */
function _context_menu_block_parse_version($version) {
  preg_match('#(\\d+[^.]*)\\.(\\S+?)-(\\d+[^.]*)\\.(\\S+)#', $version, $matches);
  $version = array(
    'core' => array(
      'major' => $matches[1],
      'minor' => $matches[2],
    ),
    'module' => array(
      'major' => $matches[3],
      'minor' => $matches[4],
    ),
  );
  return $version;
}

Functions

Namesort descending Description
context_menu_block_enable Implementation of hook_enable().
context_menu_block_requirements Implementation of hook_requirements().
context_menu_block_uninstall Implementation of hook_uninstall().
_context_menu_block_parse_version Parse a Drupal version string into its constituent parts.
_context_menu_block_validate_context Validate the version of Context enabled.

Constants