These errors have to do with context module and css_injector module not getting along. Could some one with a little php knowledge help me fix this?

Fatal error: Call to undefined function file_create_path() in /home/content/58/7409458/html/sites/all/modules/context/plugins/context_reaction_css_injector.inc on line 27

after the code change below I get this error:

Notice: Undefined offset: 0 in _css_injector_load_rule() (line 163 of /home/content/58/7409458/html/sites/all/modules/css_injector/css_injector.module).

class context_reaction_css_injector extends context_reaction {

  function options_form($context) {
    $list = array();
    foreach (_css_injector_load_rule() as $css_rule) {
      $list[$css_rule['crid']] = $css_rule['title'];
    }
    ksort($list);

    return array(
      '#title' => $this->title,
      '#description' => $this->description,
      '#options' => $list,
      '#type' => 'checkboxes',
      '#default_value' => $this->fetch_from_context($context),
    );
  }

  function execute() {
    $contexts = $this->get_contexts();
    foreach ($contexts as $context) {
      if (!empty($context->reactions[$this->plugin])) {
        foreach ($context->reactions[$this->plugin] as $crid) {
          if ($css_rule = _css_injector_load_rule($crid)) {
            drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']);
          }
        }
      }
    }
  }
}

Not knowing a lot about php I changed the following after looking up the fuction for line 27 on drupals site and seeing that it was still using a drupal 6 function. Code may not even be close but it got rid of the error on contexts side and brought up a new error.

drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']);

to

php drupal_add_css('sites/default/files/css_injector/' . $css_rule);

The code for the css_injector module is quite a bit longer but the error falls into an if statement. Not that the code may need to be changed elsewhere but I'll just post that section for now. Specifically the error targets the line of the first return.

  if (is_numeric($crid)) {
    return $rules[$crid];
  }
  else {
    return $rules;
  }
}

Any help or being pointed in the right direction would be greatly appreciated. I'm going to post this bug to context's page as well considering it seems to be a bug between the two modules working together.

Comments

new2lw’s picture

Issue tags: +Context

Here's the whole function that the if statement is in. The undefined offset error comes up in the first return of the last if statement.

function _css_injector_load_rule($crid = NULL, $reset = FALSE) {
  static $rules;
  // TODO: Change to drupal_static_fast pattern.
  if (!isset($rules) || $reset) {
    if (!$reset && ($cache = cache_get('css_injector:rules')) && !empty($cache->data)) {
      $rules = $cache->data;
    }
    else {
      $rules = array();
      $results = db_query("SELECT * FROM {css_injector_rule}", array(), array('fetch' => PDO::FETCH_ASSOC))->fetchAllAssoc('crid');
      foreach ($results as $id => $rule) {
        $rules[$id] = $rule;
      }
      cache_set('css_injector:rules', $rules);
    }
  }

  if (is_numeric($crid)) {
    return $rules[$crid];
  }
  else {
    return $rules;
  }
}
rfay’s picture

Category: bug » support
Status: Active » Postponed (maintainer needs more info)
Issue tags: -Context

If I'm not mistaken you're asking for support for a custom module?

You may want to look at and test #1044670-12: Fix file handling resulting in Incorrect path to css file on Windows

Why are you using file_create_path(), which is not a valid function in D7?

TimelessDomain’s picture

Title: Notice: Undefined offset: 0 in _css_injector_load_rule() (line 163 » Context Module Error - Notice: Undefined offset: 0 in _css_injector_load_rule() (line 163
Status: Postponed (maintainer needs more info) » Active

I am getting this error on path alias-based context triggers that are adding css injector files

Notice: Undefined offset: 0 in _css_injector_load_rule() (line 162 of \yoursite.com\sites\all\modules\css_injector\css_injector.module).

rfay’s picture

Status: Active » Postponed (maintainer needs more info)

How do you add css injector files with Context? Please give complete instructions on how to recreate this.

wbobeirne’s picture

Status: Active » Postponed (maintainer needs more info)

@rfay While CSS Injector is a seperate module, Context provides a reaction plugin (found at context/plugins/context_reaction_css_injector.inc) for it, making it a part of the context module. The steps to add a css injector reaction are:

1) Install & enable both Context and CSS Injector
2) Create a CSS Injection. I believe you should also make it not activate on any pages (So that it only activates with the context condition)
3) Create a new context condition. Set condition to be something controllable (i.e. not sitewide) or else you'll have a hard time turning it off because you always get errors. Set reaction to be CSS injection, select the injection you made.

I'm not actually getting the error this post describes after using a patch that I wrote (http://drupal.org/node/1132842#comment-5632580) but I don't know why that would change anything since my patch doesn't change the if statement that throws this error, and is virtually the same as the initial fix the original poster made.

EDIT: I've only just realized that I'm in the CSS Injector queue, and not the context queue. Some of the wording in my post may be odd.

wbobeirne’s picture

Status: Postponed (maintainer needs more info) » Active

Status: Postponed (maintainer needs more info) » Active
kenorb’s picture

Status: Active » Postponed (maintainer needs more info)