-1

I'm trying to create nodes pragmatically but I haven't been able to fill content for fields that aren't title or body. For example if i had an event type with a field called "ActivityId" how would i fill in that field pragmatically? The machine name for that field is field_activityid

function createNode($CalObj){
     global $user;

     $node = entity_create('node', array(
    'type' => 'event',
    'uid' => 1,
    'status' => 1,
    'comment' => 0,
    'promote' => 0,
  ));
     $entity = entity_metadata_wrapper('node', $node);
     $entity->language(LANGUAGE_NONE);
     $entity->title = (string)$CalObj->ActivityName;
     $entity->body->set(array(
    'value' => (string)$CalObj->ActivityId,
    'summary' => 'PROGMATIC SUMMARY',
    'format' => 'full_html',
  ));
  $entity->save();
 }

adding the following line:

$entity->activityid->set(array(
      'value' => "HJSDJHSJSKNJSDNJKSDNKJDSFJKN",
    ));

gives me the following error message:

EntityMetadataWrapperException: Unknown data property activityid. in EntityStructureWrapper->getPropertyInfo() (line 354 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

EDIT:

$entity->field_activityid->set(array(
      'value' => "HJSDJHSJSKNJSDNJKSDNKJDSFJKN",
    ));

this makes the original error go away but the new error is:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. Value at node()->field_activityid: . in EntityMetadataWrapper->set() (line 122 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

the field is just a simple text field(the single line without a body or summary and 255 character limit)

  • How did you add the field? If through the UI it'll br prefixed with field_, e.g. field_activityid – Clive Aug 1 '17 at 21:22
  • I made the field using the UI. appending the field_ fixed the original issue but raised a different one. – blu Aug 2 '17 at 13:01
0

You create a node pro like that.

$complaint_body = 'Your node complaint body text';
$node = new stdClass();  // Create a new node object
$node->type = 'company';  // Content type
$node->language = LANGUAGE_NONE;  // Or e.g. 'en' if locale is enabled
node_object_prepare($node);  //Set some default values

$node->title = 'Your node title';
$node->body[$node->language][0]['value'] = $complaint_body;
$node->body[$node->language][0]['summary'] = text_summary($complaint_body);
$node->body[$node->language][0]['format'] = 'full_html';

$node->status = 1;   // (1 or 0): published or unpublished
$node->promote = 0;  // (1 or 0): promoted to front page or not
$node->sticky = 0;  // (1 or 0): sticky at top of lists or not
$node->comment = 1;  // 2 = comments open, 1 = comments closed, 0 = comments hidden
// Add author of the node
$node->uid = 1;
// Set created date
$node->date = 'complaint_post_date';
$node->created = strtotime('complaint_post_date');

$path = 'content/mytest-' . date('YmdHis');
$node->path = array('alias' => $path);
// Save the node
node_save($node);

Add custom fields

$node->field_complaint_address[LANGUAGE_NONE][0]['value'] = 'This is custom cck filed to get complaint address';

Add a node reference to a node

$node->field_review_company_name[$node->language][0]['nid'] = 'company_node_id';

Add a term reference to a node

$node->field_company_industry[$node->language][0]['tid'] = 'company_term_id';

Thankyou

0

Thanks to clive for the tip.

$entity->field_activityid = "HJSDJHSJSKNJSDNJKSDNKJDSFJKN";

Essentially just the full machine name and setting it equal to the value you are storing it as. Using Set and an array pair didn't work for me.

This example worked for custom text field that was added to the content type using the Drupal UI.

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.