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 css_injector's page as well considering it seems to be a bug between the two modules working together.

Comments

new2lw’s picture

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;
  }
}
wjaspers’s picture

subscribing

Anticosti’s picture

I just encountered the same issue :(
I really need this to work.
Can anyone have a look into this for us.... please :(
Let's hope a fix will come soon.

Cheers,

wbobeirne’s picture

StatusFileSize
new845 bytes

It was a really simple fix, this plugin just didn't get tested in the move to Drupal 7. file_create_path() was removed in d7, so I used the file_create_filename() function instead.

Attached is a patch that should work.

wjaspers’s picture

Status: Active » Needs review
Issue tags: -Context
+++ b/plugins/context_reaction_css_injector.inc
@@ -24,7 +24,8 @@ class context_reaction_css_injector extends context_reaction {
-            drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']);
+          	$path = file_create_filename("css_injector_" . $css_rule . ".css", "public://css_injector/");
+            drupal_add_css($path, 'module', $css_rule['media'], $css_rule['preprocess']);
           }
         }
       }

There's a tab on the line which begins with $path. Please update this to use spaces. Otherwise, this patch looks good.

wbobeirne’s picture

StatusFileSize
new846 bytes

Ah, slipped by me. Here's a tabless patch.

tekante’s picture

Status: Needs review » Fixed

Fixed with commit http://drupalcode.org/project/context.git/commit/be834259593b07b7c58ac31...

Provided patch was adjusted to ensure that only rules enabled in the context resulted in additions and to leverage the css injector module to get the correct path to the saved css file.

Note that the commit message incorrectly identifies this as issue 1770846 (wbobeirne's uid)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.