Hi. I'm having some trouble creating a quiz programmatically. I thought it was fine, but what I was doing throws this error in the event that default quiz node properties are set through the admin form...

Notice: Undefined property: Quiz::$vid in DrupalDefaultEntityController->cacheGet() (line 409 of /var/aegir/platforms/.../includes/entity.inc)

Here's what I've been doing...

  global $user;
  $qnode = new stdClass();
  $qnode->type = 'quiz';
  $qnode->nid = NULL;
  $qnode->vid = NULL;
  node_object_prepare($qnode); /* Sets some defaults.
      Invokes hook_prepare() and hook_node_prepare(). */
  $qnode->language = LANGUAGE_NONE;
  $qnode->title = $nodetitle . " quiz";
  $qnode->uid = $user->uid;
  $qnode->comment = 0;
  $qnode = node_submit($qnode); // Prepare node for saving
  node_save($qnode);
  //return quiz id
  return $qnode->nid;

Am I missing something?? I examined the node object in node_save, and it looks very similar. I know I could probably drupal_form_submit, but I'd like to avoid the overhead of form creation, etc. Plus the experimentation involved in figuring out what to feed it is sometimes miserable.

Comments

captainack created an issue. See original summary.

djdevin’s picture

Issue summary: View changes

Hi,

I actually couldn't reproduce this.

But programmatically creating a Quiz node is similar to other nodes.

Try removing your " = NULL" lines since they aren't necessary, maybe there is code somewhere that is checking for the property being set.

  global $user;
  $qnode = new stdClass();
  $qnode->type = 'quiz';
  node_object_prepare($qnode);
  $qnode->language = LANGUAGE_NONE;
  $qnode->title = $nodetitle . " quiz";
  $qnode->uid = $user->uid;
  $qnode->comment = 0;
  $qnode = node_submit($qnode);
  node_save($qnode);
  return $qnode->nid;
captainack’s picture

Hi Devin,

Thanks for your reply.

I also hadn't originally seen the problem on clean installs. I only started seeing this problem when the systemwide defaults and form settings were set in both "/admin/quiz/settings/config" and "/admin/quiz/settings/quiz-form". After that it would pop up. Had you tried setting the quiz form defaults and quiz settings when you tried to reproduce?

And yea, you're right - those lines aren't necessary. I added the = NULL lines while trying to fix it (I threw some debug messages out at node_save() and tried to do exactly the same thing), but that didn't help.

I'm attaching my quiz_node_properties with nid=vid=0 and all quiz* variables just in case, even though I don't expect anyone to be masochistic enough to want to read it ;-).

Thanks again!

captainack’s picture

Title: Programmatic quiz node creation » Quiz creation as non-admin-user with disabled "set defaults" when defaults are set
Status: Active » Needs review
StatusFileSize
new650 bytes

Okay so I found the problem, along with why it was hard for you to reproduce. It's actually NOT exclusive to programmatic creation. I'll start with reproduction steps...

1. As admin, set global quiz form defaults.
2. Try to create a quiz as a non-admin user - make sure that "Remember my settings" is UNCHECKED!
3. The error gets thrown - maybe twice sometimes.

The problem is that quiz_get_defaults loads the entity and then removes nid, uid, and vid. At that point the static cache entries for those entities are corrupted, so the subsequent entity_load on the quiz node itself throws the error when looking through the cache.

I wrote a patch that clones the QNP before modifying it, and looks like it works.

qsiraut’s picture

#4 seems to work indeed, thanks !!

djdevin’s picture

Status: Needs review » Fixed

Thanks!

  • djdevin committed eebc6df on 7.x-5.x authored by captainack
    Issue #2579345 by captainack: Quiz creation as non-admin-user with...

Status: Fixed » Closed (fixed)

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

captainack’s picture

Issue summary: View changes