You are here

class context_layouts_reaction_block in Context 6

Same name and namespace in other branches
  1. 6.3 context_layouts/plugins/context_layouts_reaction_block.inc \context_layouts_reaction_block
  2. 7.3 context_layouts/plugins/context_layouts_reaction_block.inc \context_layouts_reaction_block

Hierarchy

Expanded class hierarchy of context_layouts_reaction_block

2 string references to 'context_layouts_reaction_block'
context_layouts_context_plugins in context_layouts/context_layouts.module
Implementation of hook_context_plugins(). This is a ctools plugins hook.
context_layouts_context_registry_alter in context_layouts/context_layouts.module
Implementation of hook_context_registry_alter().

File

context_layouts/plugins/context_layouts_reaction_block.inc, line 3

View source
class context_layouts_reaction_block extends context_reaction_block {

  /**
   * Override of is_enabled_region().
   * Check that there is an active layout and it supports the given region.
   */
  protected function is_enabled_region($region) {
    $layout = $this
      ->get_active_layout();
    if ($layout && isset($layout['regions']) && is_array($layout['regions'])) {
      return in_array($region, $layout['regions'], TRUE) && parent::is_enabled_region($region);
    }
    return parent::is_enabled_region($region);
  }

  /**
   * Retrieve the first layout specified found by any active contexts.
   */
  function get_active_layout($info = TRUE) {
    $contexts = $this
      ->get_contexts();
    $layouts = context_layouts_get_layouts();
    if (!empty($contexts) && !empty($layouts)) {
      foreach ($contexts as $context) {
        $values = $this
          ->fetch_from_context($context);
        if (isset($values['layout']) && isset($layouts[$values['layout']])) {
          return $info ? $layouts[$values['layout']] : $values['layout'];
        }
      }
    }

    // Fallback to default layout if provided.
    if (isset($layouts['default'])) {
      return $info ? $layouts['default'] : 'default';
    }
    return FALSE;
  }

  /**
   * Add the layout template to page vars.
   */
  function add_layout_template(&$vars) {
    if ($layout = $this
      ->get_active_layout()) {
      if (!empty($layout['template'])) {
        $vars['template_files'][] = $layout['template'];
      }
    }
  }

  /**
   * Add the layout stylesheet to the CSS.
   */
  function add_layout_stylesheet() {
    if ($layout = $this
      ->get_active_layout()) {
      if (!empty($layout['stylesheet'])) {
        drupal_add_css(drupal_get_path('theme', $layout['theme']) . '/' . $layout['stylesheet']);
      }
    }
  }

  /**
   * Override of editor form.
   */
  function editor_form($context) {
    drupal_add_css(drupal_get_path('module', 'context_layouts') . '/plugins/context_layouts_reaction_block.css');
    $form = parent::editor_form($context);
    if ($layouts = $this
      ->get_layout_options()) {
      $options = $this
        ->fetch_from_context($context);
      $form['layout'] = array(
        // #tree *must* be true for our values to be nested correctly.
        '#tree' => TRUE,
        '#prefix' => '<div class="context-editor-block-layouts">',
        '#suffix' => '</div>',
        '#weight' => -100,
        'layout' => array(
          '#title' => t('Layout'),
          '#options' => $layouts,
          '#type' => 'select',
          '#weight' => -100,
          '#default_value' => isset($options['layout']) ? $options['layout'] : NULL,
        ),
        'update' => array(
          '#value' => t('Change layout'),
          '#type' => 'submit',
        ),
      );
    }
    return $form;
  }

  /**
   * Override of editor form submit.
   */
  function editor_form_submit(&$context, $values) {

    // Someone has changed the layout, assume that the block values are not actually usable here.
    if (isset($context->reactions['block']['layout']) && $context->reactions['block']['layout'] != $values['layout']['layout']) {
      $options = $context->reactions['block'];
    }
    else {
      $options = parent::editor_form_submit($context, $values);
    }
    if (!empty($values['layout']['layout'])) {
      $options['layout'] = $values['layout']['layout'];
    }
    else {
      unset($options['layout']);
    }
    return $options;
  }

  /**
   * Override of options form.
   */
  function options_form($context) {
    $form = parent::options_form($context);
    $options = $this
      ->fetch_from_context($context);

    // Only alter the options form if the theme provides layouts.
    $theme_key = variable_get('theme_default', 'garland');
    $layouts = $this
      ->get_layout_options();
    if (!empty($layouts)) {
      $form['layout'] = array(
        '#title' => t('Layout'),
        '#description' => t('Choose one of the layouts provided by the default theme.'),
        '#options' => $layouts,
        '#type' => 'select',
        '#weight' => -100,
        '#default_value' => $options['layout'] ? $options['layout'] : NULL,
        '#attributes' => array(
          'class' => 'context-blockform-layout',
        ),
      );

      // Add js.
      // @TODO: Move this to a theme function or somewhere that will get called even
      // if the form is using a cached version of itself (e.g. when validate fails).
      drupal_add_js(drupal_get_path('module', 'context_layouts') . '/plugins/context_layouts_reaction_block.js');
      drupal_add_js(array(
        'contextLayouts' => array(
          'layouts' => $this
            ->get_layout_regions(),
        ),
      ), 'setting');
    }
    return $form;
  }

  /**
   * Override of submit handler.
   */
  function options_form_submit($values) {
    $options = parent::options_form_submit($values);

    // Only alter the options form if the theme provides layouts.
    $theme_key = variable_get('theme_default', 'garland');
    $layouts = context_layouts_get_layouts($theme_key);

    // Check that this is a valid layout.
    if (!empty($values['layout']) && isset($layouts[$values['layout']])) {
      $layout = $values['layout'];
      $options['layout'] = $layout;

      // Remove blocks that don't belong to regions in this layout.
      if (isset($layouts[$layout]['regions'])) {
        foreach ($options['blocks'] as $bid => $block) {
          if (!in_array($block['region'], $layouts[$layout]['regions'])) {
            unset($options['blocks'][$bid]);
          }
        }
      }
    }
    return $options;
  }

  /**
   * Get layout options for the given theme.
   */
  protected function get_layout_options($theme_key = NULL) {
    $theme_key = !isset($theme_key) ? variable_get('theme_default', 'garland') : $theme_key;
    $layouts = context_layouts_get_layouts($theme_key);
    $layout_options = array();
    if (!empty($layouts)) {
      $layout_options[0] = '<' . t('Site default') . '>';
      foreach ($layouts as $layout => $info) {
        $layout_options[$layout] = isset($info['name']) ? $info['name'] : $layout_options;
      }
    }
    return $layout_options;
  }

  /**
   * Get a layout to region map for the given theme.
   */
  protected function get_layout_regions($theme_key = NULL) {
    $theme_key = !isset($theme_key) ? variable_get('theme_default', 'garland') : $theme_key;
    $layouts = context_layouts_get_layouts($theme_key);
    if (!empty($layouts)) {
      $layout_regions = array();
      foreach ($layouts as $layout => $info) {
        $layout_regions[$layout] = is_array($info['regions']) ? $info['regions'] : array();
      }
    }
    return $layout_regions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_layouts_reaction_block::add_layout_stylesheet function Add the layout stylesheet to the CSS.
context_layouts_reaction_block::add_layout_template function Add the layout template to page vars.
context_layouts_reaction_block::editor_form function Override of editor form. Overrides context_reaction_block::editor_form
context_layouts_reaction_block::editor_form_submit function Override of editor form submit. Overrides context_reaction_block::editor_form_submit
context_layouts_reaction_block::get_active_layout function Retrieve the first layout specified found by any active contexts.
context_layouts_reaction_block::get_layout_options protected function Get layout options for the given theme.
context_layouts_reaction_block::get_layout_regions protected function Get a layout to region map for the given theme.
context_layouts_reaction_block::is_enabled_region protected function Override of is_enabled_region(). Check that there is an active layout and it supports the given region. Overrides context_reaction_block::is_enabled_region
context_layouts_reaction_block::options_form function Override of options form. Overrides context_reaction_block::options_form
context_layouts_reaction_block::options_form_submit function Override of submit handler. Overrides context_reaction_block::options_form_submit
context_reaction::$description property
context_reaction::$plugin property
context_reaction::$title property
context_reaction::fetch_from_context function Retrieve options from the context provided.
context_reaction::get_contexts function Retrieve active contexts that have values for this reaction.
context_reaction::__clone function Clone our references when we're being cloned.
context_reaction::__construct function Constructor. Do not override.
context_reaction_block::block_list function An alternative version of block_list() that provides any context enabled blocks.
context_reaction_block::block_sort static function Sort callback.
context_reaction_block::build_block protected function Build a block's content. Largely taken from block_list().
context_reaction_block::execute function Execute.
context_reaction_block::get_blocks function Helper function to generate a list of blocks from a specified region. If provided a context object, will generate a full list of blocks for that region distinguishing between system blocks and context-provided blocks.
context_reaction_block::is_editable protected function Determine whether inline editing requirements are met and that the current user may edit.
context_reaction_block::json_decode protected function Compatibility wrapper around json_decode().
context_reaction_block::max_block_weight protected function Generate the safe weight range for a block being added to a region such that there are enough potential unique weights to support all blocks.
context_reaction_block::rebuild_needed function Check or set whether a rebuild of the block info cache is needed.
context_reaction_block::render_ajax function Block renderer for AJAX requests. Triggered when $_GET['context_block'] is set. See ->execute() for how this is called.
context_reaction_block::settings_form function Settings form for variables. Overrides context_reaction::settings_form
context_reaction_block::_json_decode static function From http://www.php.net/manual/en/function.json-decode.php#91216 with modifications for consistency with output of json_decode().