I'm trying to use drupal_execute to create a new node type. (Drupal 6.14)

(I'm actually trying to generate a form to do this as for the following link, but have cut it back to the simple API for testing:

http://2bits.com/articles/creating-nodes-using-mini-forms-anywhere.html)

Anyways, the script works on a test site, but not one with domain access installed:


// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
  
$node = array('type' => 'templatetest');
$form_state['values']['title'] = 'My node';
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = 'Dan Grice';
$form_state['values']['domain_id'] = '-1';  ***I think this is the line I need.. but it doesn't work ***
$form_state['values']['op'] = t('Save');
drupal_execute('templatetest_node_form', $form_state, (object)$node);

The script does generates a templatetest node on the next cron run. (Does drupal_execute rely on cron or does cron just fix up errors?)

However, it also gives me an error in my logs:

Illegal choice 0 in Publish to element.

The only other reports of that error are with domain access modules.

I assume I probably have the domain_id variable wrong or do I need something to activate domain settings?

On my test site without DA it appears to publish instantly and normally. With DA it gives an error and is delayed.

Comments

dangrice’s picture

One more thing. When it does generate a node, it does it as anonymous rather than my user name..

agentrickard’s picture

Category: bug » support

These are not related to Domain Access, but to Drupal's FormsAPI. 'domain' is just the name of the fieldset, not the values passed by the form.

The domain element of the node form has these two items by default.

$form_state['values']['domain_site'] = TRUE || FALSE // a boolean for 'send to all affiliates'
$form_state['values']['domains'] = array() // an array of domain ids.

So you probably want:

$form_state['values']['domains'] = array(-1);

For anonymous, you actually have to pass $node->name, not $node->uid, which is a quirk in core.

dangrice’s picture

That still gives me errors. "An illegal choice is detected"

>> And in my logs. Illegal choice 0 in Publish to element.

All I am trying to do is create a new node (for logged in users). I presume its domain access, since there are only two required fields for node creation. (title, and domain access publish to.) Other sites work fine.
It may be form API spouting back the error.

I've noticed patches having to be added to other modules to allow them to work with domain access. I'm just not sure what needs to be added to a simple drupal_execute to allow node creation.

agentrickard’s picture

No patches, you just need to experiment with the format a little.

Maybe domain_site is also an array. It's a checkbox form element, so maybe Drupal wants the data in an array format.

blackdog’s picture

In our import scripts we set this: $node->domains_raw = array(DOMAIN_ID => DOMAIN_ID);, where DOMAIN_ID is the domain_id, and save with node_save(). I'm guessing you should do something similar.

captainack’s picture

Thanks for the leg-up guys!

I was faced with the same problem, and followed agentrickard's suggestion to experiment a bit. A few dsm's in core later, and I found this to work and to correspond to my current defaults for domain_access:

$form_state['values']['domain_site'] = 1;
$form_state['values']['domains'] = array();
$form_state['values']['domains'][-1] = -1;

I'm not sure how well I can trust this, though. For example, if domains/defaults get shuffled around, what happens?

Am I following the best approach? Would it be possible for domain_access to pick up the defaults during form validation/preparation so that this code isn't needed whenever trying to add nodes programmatically if domain_access is installed?

agentrickard’s picture

I don't understand what this means " For example, if domains/defaults get shuffled around, what happens?"

In the D6 version, the -1 for default domain can be trusted not to change. It is used because the FormAPI won't let you pass a 0 in a checkbox.

DA does pick up the defaults during domain_node_access_records(), unless you override them. But the defaults are the domain from which your script executes, and the status for 'all affiliates' you have set.

agentrickard’s picture

Status: Active » Closed (fixed)