I have a rules condition to check the current domain machine name, and it works great. Except that every once and a while, the function doesn't return an array with the machine_name, so we get error notices saying

Notice: Undefined index: machine_name.

This happens on checkout pages sometimes, which can of course scare a customer away. It has happened in other contexts besides rules, as well.

For the commerce part, it seems to happen if there is a product pricing rule that changes the price of a product. But that shouldn't affect the fundamental domain_get_domain function call. That is still the same.

Any insight is appreciated. Thanks!

Comments

agentrickard’s picture

Very odd. Any idea of steps to reproduce?

jazzdrive3’s picture

This is in the context of a rule condition. I have a rule condition component, and one is a custom condition that compares the domain machine name. Here is the code for it, modified from the domain_rules module:

  $current_domain = domain_get_domain();
  $current_domain = $current_domain['machine_name'];
  $subdomain = trim($subdomain);


  switch ($operator) {
    case '!=':
      return ($subdomain != $current_domain);
    case '~':
      return fnmatch($subdomain, $current_domain);
    case 'regexp':
      return (preg_match('#' . $subdomain . '#Ui', $current_domain));
    case '=': default:
      return ($subdomain == $current_domain);
  }

The domain_get_domain() call only returns an array with the following indexes:

- domain_id
- subdomain
- active_alias_id
- redirect
- site_grant

That is not the full array I get in other contexts, which includes the indexes machine_name, is_default, path, scheme, sitename, and the alias array.

The rule condition itself works fine in other areas. So I go to the shopping cart, and condition checks fine and returns correctly. But as soon as I go to the first checkout page, which calls the same rule from the same event (Calculate the price of a product), the condition gets a truncated domain array.

I also pulled straight from the globals variable to check, and it was the same. Which makes sense, since that is all the function itself does anyway.

I haven't done anything with the hook_domain_load for this site, either.

Thanks.

jazzdrive3’s picture

In the meantime, I was able to hack around it with this code after getting the domain:

  if(empty($current_domain['machine_name'])) {
    $current_domain['machine_name'] = domain_load_machine_name($current_domain['domain_id']);
  }

So I had to manually call the domain_load_machine_name function.

agentrickard’s picture

Yeah, that's something to do with execution order of the requests. It looks like domain_get_domain() is being set very, very early. The data you are getting back is from after domain_alias_domain_bootstrap_lookup() is run, which means that your code is executing before domain_init().

See line 125 of domain.module. That's the data you don't currently have.

This code could use a smart refactor. domain_get_domain() is really just a wrapper around the global $_domain varialbe, and it's designed to get us used to the patterns that are coming in Drupal 8.

However, domain_get_domain() might need to be a _smarter_ wrapper than it currently is. Perhaps we should check to ensure that the full domain has been loaded (and checking for machine_name might do that).

agentrickard’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)