You are here

public static function Contexts::checkContextsExist in Hook Update Deploy Tools 7

Check that the contexts exist.

Parameters

array $contexts: An array of context machine names to check.

bool $strict: Flag for whether this should run strict and throw an exception.

Return value

bool TRUE if the contexts exist. FALSE if not strict.

Throws

HudtException If a context does not exist and $strict = TRUE.

2 calls to Contexts::checkContextsExist()
Contexts::disable in src/Contexts.php
Disables an array of contexts and checks to make sure they were disabled.
Contexts::enable in src/Contexts.php
Enables an array of contexts and checks to make sure they were enabled.

File

src/Contexts.php, line 226

Class

Contexts
Public method for managing Contexts that verifies the changes.

Namespace

HookUpdateDeployTools

Code

public static function checkContextsExist($contexts, $strict = TRUE) {
  $existing_contexts = context_load(NULL, TRUE);
  $report = array();
  $return = TRUE;
  $t = get_t();
  $exists = $t('exists');
  $missing = $t('missing');
  foreach ($contexts as $context) {
    $report[$context] = !empty($existing_contexts[$context]) ? $exists : $missing;
  }
  $variables = array(
    '!report' => $report,
  );
  if (in_array($missing, $report)) {

    // A module is missing.
    if ($strict) {
      $message = "One or more contexts seem to be missing. Report: !report";
      throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
    }
    else {

      // This was not strict, so just issue a notice, but no Exception.
      $message = "One or more of the contexts is missing. Report: !report";
      Message::make($message, $variables, WATCHDOG_NOTICE);
      $return = FALSE;
    }
  }
  return $return;
}