Spotted this on a node this morning:

warning: key() [function.key]: Passed variable is not an array or object in [snip]/sites/all/modules/comment_notify/comment_notify.module on line 129.

The issue is the #default_value line in the else statement below:

  if (count($available_options) > 1) {
    $form['notify_type'] = array(
      '#type' => 'radios',
      '#options' => $available_options,
      '#default_value' => $preference,
    );
  }
  else {
    $form['notify_type'] = array(
      '#type' => 'hidden',
      '#default_value' => key($available_options),
    );
  }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

greggles’s picture

Interesting. If you go to admin/settings/comment_notify and submit the page, does this error go away? Also, what are your settings on that page?

greggles’s picture

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

Status: Postponed (maintainer needs more info) » Active

Interesting - you're right, when I saved the page, the error went away. I wonder if it will come back...?

Options:
- all 10 content types are enabled for notification
- both 'All comments' and 'Replies to my comment' are selected as subscription modes
- default state for both anonymous and registered users is 'all comments'
- both emails have body text.

greggles’s picture

I think what happened is that you installed the module, but didn't configure it. When that happens it should figure out sane defaults, but that is sometimes tricky for things like this that are based on content type.

Now that I know the steps to repeat (install, don't configure) it should be possible to track this down to some variable_get that doesn't have the right defaults.

webchick’s picture

I got this too. I know I'm supposed to read instructions, but I don't, so it'd be nice if I did not get an error when I haven't configured the module yet.

webchick’s picture

Status: Active » Needs review
FileSize
795 bytes

Well, here's one option, which just doesn't proceed if no available options are found.

This might lead to support requests saying the module doesn't do anything, but I guess that's better than support requests about error messages. :) Plus, if nothing happens, the most logical next place to look would be the settings page.

webchick’s picture

Hm. Actually, I think the real problem is the format of the array is incorrect (or else the code to loop through is incorrect).

  $total_options = array(
    COMMENT_NOTIFY_NODE     => t('All comments'),
    COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  );
  # I interpret this to mean that by default, you want both options enabled.
  $options = variable_get('comment_notify_available_alerts', array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT));
  # Yet, dsm($options) here gets you array(0 => 1, 1 => 2).
  foreach ($options as $key => $available) {
    # Since 0 != 1 and 1 != 2...
    if ($key == $available) {
      # Nothing is marked available.
      $available_options[$available] = $total_options[$available];
    }
  }

Seems like maybe you want this instead.

Dave Reid’s picture

Maybe the comment_notify_available_alerts variable needs to be run through array filter by using:

$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);

Then all we'd need to do is:

  $total_options = array(
    COMMENT_NOTIFY_NODE     => t('All comments'),
    COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  );
  $options = variable_get('comment_notify_available_alerts', array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT));
  foreach ($options as $key => $option) {
    $available_options[$option] = $total_options[$option];
  }
greggles’s picture

Title: key($available_options) is not an array or object » key($available_options) is not an array or object causes PHP notice and defaults don't work.
Status: Needs review » Fixed

Webchick's fix sticks closest to the original intent. Committed! http://drupal.org/cvs?commit=221674

@dave I don't follow how to integrate your idea, but I imagine it's simpler. Patch or it never happened ;)

Status: Fixed » Closed (fixed)

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