Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am trying to create an entity with entity_metadata_wrapper from within hook_boot. I know everyone will say "use hook_init" but for performance reasons, I'm going w/ hook_boot :) .

I have a couple modules loaded like so:

require_once DRUPAL_ROOT . '/includes/common.inc';
drupal_load('module', 'entity');
drupal_load('module', 'field');

This is what I have:

$entity = entity_create('myentity', array('type' => 'click'));
$ew = entity_metadata_wrapper('myentity',$entity);
$ew->field_myentity_url_reference->set($row->rid);
$ew->save();

The above works fine if run normally (like from form_alter or something), but not from hook_boot.

I am getting the following errors:

Warning: class_implements(): object or string expected in entity_create() (line 367 of /data/disk/host/distro/005/drupal-7.18.1-dev/sites/mysite.com/modules/entity/entity.module).
Warning: in_array() expects parameter 2 to be array, boolean given in entity_create() (line 367 of /data/disk/host/distro/005/drupal-7.18.1-dev/sites/mysite.com/modules/entity/entity.module).
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of /data/disk/host/distro/005/drupal-7.18.1-dev/sites/mysite.com/modules/entity/includes/entity.wrapper.inc).

Anyone know what else I need to drupal_load to get this to work?

EDIT - Figure out how to do this:

I used the background process module: http://drupal.org/project/background_process. I added the code below to hook_boot:

//load the required modules needed to do background_process
require_once DRUPAL_ROOT . '/includes/common.inc';
require_once DRUPAL_ROOT . '/includes/path.inc';
drupal_load('module', 'background_process');
drupal_load('module', 'progress');

background_process_start('shurly_create_click_log', $row);//  drupal_load('module', 'entity');

Then I put the entity code in a new function:

function shurly_create_click_log ($row){
    $entity = entity_create('linkpin', array('type' => 'click'));
    $ew = entity_metadata_wrapper('linkpin',$entity);
    $ew->field_linkpin_url_reference->set($row->rid);
    $ew->save();
}

Basically, the shurly_create_click_log function gets ran at full bootstrap in the background, so it doesn't slow down my pageload/redirect from hook_boot.

share|improve this question
I figured out how to do this... not by calling it directly though. See my edit above. – that0n3guy Mar 4 at 18:48
You should put your answer in an answer, and accept it. That's totally acceptable in Stack Exchange land, and will stop your question from showing up as unanswered. – davidcl Mar 4 at 23:32

1 Answer

Here's a solution.

Add this to the top of your hook_boot() function:

function hook_boot() {
  global $user, $base_path;
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
...
}

drupal_bootstrap keeps track of what level it has been called so this trick effectively pushes hook_boot to being later in the boot process.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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