You are here

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

Check to see if the contexts are actually enabled.

Parameters

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

Return value

string Messsage indicating the contexts are enabled

Throws

\HudtException Calls the update a failure, preventing it from registering the update_N.

1 call to Contexts::checkEnabled()
Contexts::enable in src/Contexts.php
Enables an array of contexts and checks to make sure they were enabled.

File

src/Contexts.php, line 76

Class

Contexts
Public method for managing Contexts that verifies the changes.

Namespace

HookUpdateDeployTools

Code

public static function checkEnabled($contexts = array()) {
  $contexts = (array) $contexts;
  $t = get_t();
  $enabled = $t('enabled');
  $not_enabled = $t('not-enabled');
  $report = array();
  self::canSwitch();

  // Get a list of enabled contexts.
  $enabled_contexts = context_enabled_contexts(TRUE);

  // Check to see if each context is enabled.
  foreach ($contexts as $context) {
    if (!empty($enabled_contexts[$context])) {

      // This context is enabled.
      $report[$context] = $enabled;
    }
    else {
      $report[$context] = $not_enabled;
    }
  }
  if (in_array($not_enabled, $report)) {

    // Something was not enabled. Fail the update.
    $message .= 'Some of the contexts that were supposed to be enabled, are not showing as enabled. Please investigate the problem and re-run this update. Report: !report';
    $variables = array(
      '!report' => $report,
    );
    throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
  }
  else {

    // It was a success.
    $message .= "The requested contexts were enabled successfully. Report: !report";
    $variables = array(
      '!report' => $report,
    );
    return Message::make($message, $variables, WATCHDOG_INFO);
  }
}