Для того, чтобы создать программно новую ноду в Drupal 7 нам требуется создать новый тип контента (допустим, что мы хотим создать свой пользовательский тип ноды), поля (fields) для данного типа контента и уже затем саму ноду.
1. Создаем программно новый тип контента.
$type = array(
'type' => 'Blog',
'name' => st('blog'),
'base' => 'node_content',
'description' => st('Это наш блог'),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
2. Задаем поле для нашего типа контента.
field_create_field(array(
'field_name' => 'post_content',
'type' => 'Text',
'cardinality' => 1,
'settings' => array(),
'entity_types' => array('node'),
));
field_create_instance(array(
'field_name' => 'post_content',
'entity_type' => 'node',
'bundle' => 'blog',
'label' => t('Text for post'),
'description' => t('Введите текст для вашего поста'),
'widget' => array(
'type' => 'text_textarea',
'weight' => 0,
'settings' => array('size' => 50),
),
'display' => array(
'default' => array(
'label' => 'above',
'settings' => array(),
'weight' => 1,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
),
),
'required' => TRUE,
));
3. Создаем ноду
$node = new stdClass();
$node->type = 'blog';
$node->title = ' Заголовок поста';
$node->language = LANGUAGE_NONE;
$node->body[LANGUAGE_NONE][0]['value'] = 'Текст нашего поста';
$node->body[LANGUAGE_NONE][0]['summary'] = 'Анонс';
$node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
$node->uid = 1;
$node->status = 1;
$node->promote = 1;
node_save($node);
Таким образом, мы создали тип контента «блог», создали текстовое поле для него «post_content» и создали одну ноду для данного типа.