You are here

public static function Blocks::enable in Hook Update Deploy Tools 7

Enables and sets the region for a block in a specific theme.

Parameters

string $module: The machine name of the module that created the block. Use 'block' if was one created in the block UI.

string $block_delta: the block delta (machine name of the block)

string $region: The name or number of the region to place the block

string $theme: The name of the theme to target. Defaults to default theme.

Return value

string The message to output to the hook_update_N.

Throws

HudtException In the event that the enable was unssuccessful.

File

src/Blocks.php, line 124

Class

Blocks
Public methods for working with Blocks.

Namespace

HookUpdateDeployTools

Code

public static function enable($module, $block_delta, $region, $theme = NULL) {
  try {

    // Gather the theme.
    $theme = !empty($theme) ? $theme : variable_get('theme_default', NULL);
    Check::notEmpty('$theme', $theme);
    Check::notEmpty('$module', $module);
    Check::notEmpty('$block_delta', $block_delta);
    Check::notEmpty('$region', $region);
    $vars = array(
      '@module' => $module,
      '@block_delta' => $block_delta,
      '@theme' => $theme,
      '@region' => $region,
    );
    if ($region == -1) {

      // This is a disable attempt. Warn and fail.
      $message = 'Trying to set block @module:@block_delta to region:-1 would disable the block.  If that is the intention, use Blocks:disable instead.';
      throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
    }
    $block_original = self::load($module, $block_delta, $theme);
    if ($block_original->region === (string) $region && $block_original->status === '1') {
      $message = 'The block @module:@block_delta for theme:@theme was already enabled in region:@region. No change.';
    }
    else {

      // The block needs to be updated.
      $fields = array(
        'region' => $region,
      );
      self::updateInstancePropertiesSilent($module, $block_delta, $theme, $fields);
      $block_new = self::load($module, $block_delta, $theme);

      // Verify that it stuck.
      if ($block_new->region === (string) $region && $block_new->status === '1') {
        $message = 'The block @module:@block_delta for theme:@theme was enabled in region:@region.';
      }
      else {

        // The change did not stick.
        throw new HudtException('The block @module:@block_delta for theme:@theme was NOT enabled in region:@region.', $vars, WATCHDOG_ERROR, TRUE);
      }
    }
  } catch (\Exception $e) {
    $vars['!error'] = method_exists($e, 'logMessage') ? $e
      ->logMessage() : $e
      ->getMessage();
    if (!method_exists($e, 'logMessage')) {

      // Not logged yet, so log it.
      $message = 'Blocks::enable @module:@block_delta for theme:@theme region:@region failed because: !error';
      Message::make($message, $vars, WATCHDOG_ERROR);
    }
    throw new HudtException('Caught Exception: Update aborted!  !error', $vars, WATCHDOG_ERROR, FALSE);
  }
  $return_msg = Message::make($message, $vars, WATCHDOG_INFO, 2);
  return $return_msg;
}