1

I have a problem i've struggled with for a long time, and it's starting to really piss me off, so i hope you can help me.

The thing is i'm writing a custom script (not a module) that needs access to the drupal user object, and it's profile2 fields, so i found out i could use the bootstrapper for this purpose.

Loading fields from the users profile works just fine, but when i try to make changes programmatically (user_save function for example), it fails with the following error, which makes absolutely no sense to me:

Fatal error: Class 'Drupal\gmap\GmapDefaults' not found in (root directory)\sites\all\modules\gmap\gmap.module on line 32

A function that would return this error could be something like this:

function custom_add_role_to_user($user, $role_name) {
              // For convenience, we'll allow user ids as well as full user objects.

              // If the user doesn't already have the role, add the role to that user.
              $key = array_search($role_name, $user->roles);
              if ($key == FALSE) {
                // Get the rid from the roles table.
                $roles = user_roles(TRUE);
                $rid = array_search($role_name, $roles);
                if ($rid != FALSE) {
                  $new_role[$rid] = $role_name;
                  $all_roles = $user->roles + $new_role; // Add new role to existing roles.
                  user_save($user, array('roles' => $all_roles)); // <---- This function throws the above error...
                }
              }
            }

If i don't call user_save(), everything works just fine, and until now i have been doing fine with direct sql queries, but i need to know why this doesn't work.

I've tried navigating to the GMap class to see what's actually going on at line 32 (Still not knowing why my simple script keeps referring to some random Google Map Module..?)

Here it is:

function gmap_defaults() {
 include_once(drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapDefaults.php');
 return GmapDefaults::getInstance()->getDefaults();
}

So why is the bootstrapper calling this random module, and what is wrong with the module first of all???

It seems like it doesn't get to create the namespace required correctly. But i don't see where it goes wrong?

drupal_get_path returns the right path... I've tried to put a static path instead, to see if that made any difference... It didn't...

Thanks in advance for your help.

2 Answers 2

1

The correct answer could be to also use the necessary namespacing when instantiating the GmapDefaults class:

include_once(DRUPAL_ROOT . base_path() . drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapDefaults.php');

$macro = !empty($field['settings']['gmap_macro']) ? $field['settings']['gmap_macro'] : '[gmap ]';
$map = array_merge(Drupal\gmap\GmapDefaults::getInstance()->getDefaults(), gmap_parse_macro($macro));

Note that I also converted the original include to an absolute path based include to make sure the path is correct...

I just prepended the necessary namespace before "GmapDefaults::" and it works.

0

So i solved it, and i gotta say it is a bit embarrassing...

It turns out i just needed to change the working directory of the script, one level up as i've put it in a subdirectory...

But i still dont understand why its just the google maps module that fails.... well anyway - guess i learned my lesson :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.