You are here

class Context_Block_Disable_Reaction in Context Block Disable 7.2

Same name and namespace in other branches
  1. 7 plugins/context_block_disable_reaction.inc \Context_Block_Disable_Reaction

Context reaction plugin for disabling blocks.

Hierarchy

Expanded class hierarchy of Context_Block_Disable_Reaction

1 string reference to 'Context_Block_Disable_Reaction'
context_block_disable_context_plugins in ./context_block_disable.module
Implements hook_context_plugins().

File

plugins/context_block_disable_reaction.inc, line 13
Context reaction plugin for disabling blocks.

View source
class Context_Block_Disable_Reaction extends context_reaction_block {

  // The block this plugin needs to consider.
  protected $reaction_block = NULL;

  /**
   * Implements options_form() callback.
   *
   * Overrides the parent function in order to pull out only the part of the
   * form we care about, and adjust the selected values due to that change.
   */
  public function options_form($context) {

    // Rebuild the block info cache if necessary.
    $this
      ->get_blocks(NULL, NULL, $this
      ->rebuild_needed());
    $this
      ->rebuild_needed(FALSE);
    $settings = $this
      ->fetch_from_context($context);
    $form = array(
      '#tree' => TRUE,
    );
    $modules = module_list();
    $form['selector'] = array(
      '#type' => 'item',
      '#tree' => TRUE,
    );
    foreach ($this
      ->get_blocks() as $block) {
      $group = isset($block->context_group) ? $block->context_group : $block->module;
      if (!isset($form['selector'][$group])) {
        $form['selector'][$group] = array(
          '#type' => 'checkboxes',
          '#title' => isset($block->context_group) ? $block->context_group : $modules[$block->module],
          '#options' => array(),
        );
      }
      $form['selector'][$group]['#options'][$block->bid] = check_plain($block->info);

      // Set up the selected blocks.
      if (!empty($settings['blocks'][$block->bid])) {
        $form['selector'][$group]['#default_value'][] = $block->bid;
      }
    }
    ksort($form['selector']);
    return $form;
  }

  /**
   * Implements options_form_submit() callback.
   *
   * Overrides the parent function in order to handle the lack of regions in
   * this plugin.
   */
  public function options_form_submit($values) {
    $blocks = array();
    $block_info = $this
      ->get_blocks();
    foreach ($values['selector'] as $module => $selections) {
      foreach ($selections as $selected) {
        if (!empty($selected)) {
          $blocks[$selected] = array(
            'module' => $block_info[$selected]->module,
            'delta' => $block_info[$selected]->delta,
            'region' => $block_info[$selected]->region,
            'weight' => $block_info[$selected]->weight,
          );
        }
      }
    }
    return array(
      'blocks' => $blocks,
    );
  }

  /**
   * Disables selected modules if a condition implements this plugin.
   *
   * @param array &$data
   *   The block data array (subject, content).
   *
   * @see hook_block_view_alter()
   */
  public function execute(&$data) {

    // Exit early if no reaction block was set or data has no content.
    if (empty($this->reaction_block) || empty($data['content'])) {
      return;
    }
    $block = $this->reaction_block;

    // Construct a bid that matches what we use on the settings form and what
    // context_reaction_block also uses.
    $bid = $block->module . '-' . $block->delta;
    $contexts = $this
      ->get_contexts();
    foreach ($contexts as $context) {
      if (!empty($context->reactions[$this->plugin])) {
        $blocks = $context->reactions[$this->plugin]['blocks'];
        if (!empty($blocks[$bid])) {

          // Swap non-render arrays into render arrays when configured as such.
          if (variable_get('context_block_disable_force_array', FALSE)) {
            if (!is_array($data['content'])) {
              $data['content'] = array(
                '#markup' => $data['content'],
              );
            }
          }
          if (is_array($data['content'])) {

            // Leave the actual block contents alone, just stop users from being
            // able to access it.
            $data['content']['#access'] = FALSE;
          }
          else {
            $data['content'] = NULL;
          }
        }
      }
    }
  }

  /**
   * Sets the block this plugin should use for comparison operations.
   *
   * @param object $block
   *   A block object that at minimum contains the module and delta properties.
   */
  public function set_reaction_block($block) {
    $this->reaction_block = $block;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Context_Block_Disable_Reaction::$reaction_block protected property
Context_Block_Disable_Reaction::execute public function Disables selected modules if a condition implements this plugin. Overrides context_reaction_block::execute
Context_Block_Disable_Reaction::options_form public function Implements options_form() callback. Overrides context_reaction_block::options_form
Context_Block_Disable_Reaction::options_form_submit public function Implements options_form_submit() callback. Overrides context_reaction_block::options_form_submit
Context_Block_Disable_Reaction::set_reaction_block public function Sets the block this plugin should use for comparison operations.
context_reaction::$description property
context_reaction::$plugin property
context_reaction::$title property
context_reaction::fetch_from_context function Retrieve options from the context provided. 1
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_get_blocks_by_region function Get a renderable array of a region containing all enabled blocks.
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::context_block_ajax_rendering_allowed private function Allow modules to selectively allow ajax rendering of a specific block
context_reaction_block::editable_block protected function Add markup for making a block editable.
context_reaction_block::editable_region protected function Add markup for making a region editable.
context_reaction_block::editor_form function Context editor form for blocks. 1
context_reaction_block::editor_form_submit function Submit handler context editor form. 1
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_check function Determine if there is an active context editor block, and set a flag. We will set a flag so that we can make sure that blocks with empty content have some default content. This is needed so the save of the context inline editor does not remove the…
context_reaction_block::is_editable_region protected function Determine whether inline editing requirements are met and that the current user may edit.
context_reaction_block::is_enabled_region protected function Return a list of enabled regions for which blocks should be built. Split out into a separate method for easy overrides in extending classes. 1
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::system_region_list protected function Provide caching for system_region_list since it can get called frequently. Evaluate for removal once https://drupal.org/node/1873450 lands or system_region_list is otherwise cached in core