1

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.

2
  • I figured out how to do this... not by calling it directly though. See my edit above.
    – that0n3guy
    Mar 4, 2013 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, 2013 at 23:32

1 Answer 1

-3

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.

1
  • 1
    There's no reason to do this, ever. You're breaking the whole point of hook_boot() by doing this, which is specifically to not perform a full bootstrap, and your cached page performance will degrade for no reason. This is what hook_init() is for.
    – Les Lim
    Nov 20, 2013 at 4:25

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.