5

I want to create a temporary content type, and a node of that type, in code. It preferably should not live past page load. Can I do that?

For the reason to this very strange request, please see Using field widgets in non-field forms.

If you have suggestions for completely different solutions, then please take those to the linked issue, since this question is mainly a "support" question to the current answer to that question.

  • Creating a temporary content type is not different from creating a content type. Is there any reason the code for creating a content type would not work for you? – kiamlaluno Jul 31 '12 at 13:23
  • The problem with "just" creating a content type, as I've currently done with help of Features, is that it shows up everywhere. Suddently there is a "useless" type of content showing up for admins, new permissions to manage, new options in Views, etc. I could add and remove a "real" type during the same page request, but then I fear this will cause frequent field api cache clears, and become a performance problem. It would be much nicer to find a way to manipulate Field API with less DB-overhead. – Letharion Jul 31 '12 at 13:44
  • That is because Drupal doesn't allow hidden content types; if somebody visits one of the administrative pages while your module is creating the content type, it is possible that settings for that content type are visible. If your module creates the content type, and then deletes it in the same page requests, the chance of somebody seeing settings about that content type should be reduced. As far as I know, the Features module doesn't create a content type for then immediately delete it during the same page request, and inside the same function. – kiamlaluno Jul 31 '12 at 14:35
  • The question referenced here has been answered. Seeing as this question is probably not relevant outside of that context, I think it can be closed. – goron Nov 10 '12 at 4:03
1
$node_type = array(
  'type' => 'temporary_node_type', // This is the machine name.
  'name' => t('Temporary Node'), // This is the human name.
  'base' => 'node_content', // If you set this to your module name, then
                            // *your module's* hook_insert(), hook_delete(),
                            // etc. will get called in addition to
                            // hook_node_*()
  'description' => t('Description.')
  // A full list of options is at http://api.drupal.org/hook_node_info.
);

$node_type = node_type_set_defaults($node_type);

node_type_save($node_type);

// Do something with your node type. You can also add fields here if you
// would like.

node_type_delete($node_type);

Note 1: People with the "administrator" role will see this type on node/add every once and a while, depending on how often you do this.

Note 2: You want to use something more then a hard-coded string for your type attribute to avoid collisions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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