You are here

class context_reaction_addassets_base in Context Add Assets 7

Same name and namespace in other branches
  1. 6 plugins/context_reaction_addassets_base.inc \context_reaction_addassets_base

Expose themes as context reactions.

Hierarchy

Expanded class hierarchy of context_reaction_addassets_base

3 string references to 'context_reaction_addassets_base'
context_reaction_addcss.inc in plugins/context_reaction_addcss.inc
The Context reaction plugin to add CSS files
context_reaction_addjs.inc in plugins/context_reaction_addjs.inc
The Context reaction plugin to add JS files
context_reaction_addless.inc in plugins/context_reaction_addless.inc
The Context reaction plugin to add Less files

File

plugins/context_reaction_addassets_base.inc, line 13
The Context reaction plugin to add asset files

View source
class context_reaction_addassets_base extends context_reaction {
  var $search_scope;
  function __construct($plugin, $info) {
    parent::__construct($plugin, $info);

    // defaults are for js - but should never be picked up.
    $this->title = t('JS from Themes');
    $this->search_type = 'js';
  }

  /**
   * Editor form.
   */
  function editor_form($context) {
    $form = $this
      ->options_form($context);
    return $form;
  }

  /**
   * Submit handler for editor form.
   */
  function editor_form_submit($context, $values) {
    return $values;
  }

  /**
   * Prepare formatted form array showing grouped assets
   *  grouped by location and show as checkboxes
   */
  function options_form($context) {
    $values = $this
      ->fetch_from_context($context);
    $options = $this
      ->_context_addassets_search();
    $options = !$options ? array() : $options;
    $options_array = array();
    foreach ($options as $key => $value) {
      $path = $key;
      $key = explode(' -- ', $value);
      $value = $key[1];
      $key = trim($key[0]);
      $options_array[$key][$path] = $value;
    }
    $form['#tree'] = TRUE;
    foreach ($options_array as $key => $items) {
      $form[$key] = array(
        '#type' => 'item',
        '#title' => $key,
      );
      foreach ($items as $path => $file_name) {
        $form[$key][$path] = array(
          '#title' => $file_name,
          '#type' => 'checkbox',
          '#return_value' => $path,
          '#default_value' => isset($values[$key][$path]) ? $values[$key][$path] : '',
        );
      }
    }
    if (count($form) < 2) {
      $link_options['query'] = drupal_get_destination();
      $form['help'] = array(
        '#type' => 'item',
        '#title' => t('No Assets Found'),
        '#description' => l('May you need to expand your search?', _context_addassets_admin_path(), $link_options),
      );
    }
    return $form;
  }

  /**
   * Filters deselected files from the listing.
   *
   * Reduces unnecessary conflicts when exporting to Features.
   */
  function options_form_submit($values) {
    foreach (array_keys($values) as $key) {
      $values[$key] = array_filter($values[$key]);
    }
    return $values;
  }

  /**
   * Implementation of the built-in context plugin class execution
   */
  function execute() {
    $contexts = context_active_contexts();
    $classes = array();

    //dpm($contexts);
    foreach ($contexts as $key => $value) {
      if (!empty($value->reactions[$this->plugin])) {
        foreach ($value->reactions[$this->plugin] as $path_array) {
          if (is_array($path_array)) {
            foreach ($path_array as $path) {
              if ($path) {
                $ext = explode('.', $path);
                $ext = drupal_strtolower($ext[count($ext) - 1]);
                switch ($ext) {
                  case 'js':
                    drupal_add_js($path, array(
                      'group' => JS_DEFAULT,
                    ));
                    break;
                  case 'css':
                  case 'less':
                  case 'sass':
                  case 'scss':
                    drupal_add_css($path, array(
                      'group' => CSS_THEME,
                    ));
                    break;
                }
              }
            }
          }
        }
      }
    }
  }

  /**
   *  Scan active themes for js files.
   *
   *	@return Array
   *		An array indexed by file paths containing strings describing each path "Theme Key - File Name"
   */
  function _context_addassets_search() {
    $found_files = _context_addassets_scandir($this->search_type, $this->search_scope);
    return $found_files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::settings_form function Settings form. Provide variable settings for your reaction. 2
context_reaction::__clone function Clone our references when we're being cloned.
context_reaction_addassets_base::$search_scope property 3
context_reaction_addassets_base::editor_form function Editor form.
context_reaction_addassets_base::editor_form_submit function Submit handler for editor form.
context_reaction_addassets_base::execute function Implementation of the built-in context plugin class execution
context_reaction_addassets_base::options_form function Prepare formatted form array showing grouped assets grouped by location and show as checkboxes Overrides context_reaction::options_form
context_reaction_addassets_base::options_form_submit function Filters deselected files from the listing. Overrides context_reaction::options_form_submit
context_reaction_addassets_base::_context_addassets_search function * Scan active themes for js files. * *
context_reaction_addassets_base::__construct function Constructor. Do not override. Overrides context_reaction::__construct 3