When creating nodes in code, some fields should just be created as default, with its defaults subfields values as set in the Field UI. The multifield is not set, but is causing an database-error (see attached image).

What is the right way, to create a Multifield in code, with its default subfields values, set in the Field UI administration?

Comments

dave reid’s picture

Status: Active » Postponed (maintainer needs more info)

It might help me debug this for you if I can see what was in the $node object, and what code you're using to save the node programatically.

sbilde’s picture

This is the function for saving the new imported node.

/*
 * Helper function for bulk creating Brand nodes
 */
function _patchwork_facebook_create_brand_nodes($new_brands, $uid) {

  foreach ($new_brands as $page) {

    $node = new stdClass();
    $node->type = "brand";
    $node->title = check_plain(str_replace("'", "", $page->name));
    $node->language = LANGUAGE_NONE;

    node_object_prepare($node);
    $node->uid = 0; // Set anonymous user as creator

    $node->field_facebook_page_unique_id[$node->language][0]['value'] = check_plain($page->id);

    if (isset($page->username)) {
      $node->field_facebook_id[$node->language][0]['value'] = check_plain($page->username);
      // Set the tax-term for the Facebook Username
      $node->field_facebook_username_term[$node->language][0]['target_id'] = _patchwork_facebook_get_term_id_by_name($page->username, $vocabulary = 5); // 'Facebook Usernames' vocabulary
    }
    if (isset($page->category)) {
      // Set the tax-term for the Facebook Page category.
      $node->field_category_term[LANGUAGE_NONE][0]['target_id'] = _patchwork_facebook_get_term_id_by_name($page->category, $vocabulary = 7); // 'Categories' vocabulary
    }

    if (isset($page->likes)) {
      $node->field_brand_fans_count[$node->language][0]['value'] = $page->likes;
    }
    if (isset($page->cover->source)) {
      $node->field_cover_picture[$node->language][0]['value'] = check_plain($page->cover->source);
    }
    if (isset($page->picture->data)) {
      $node->field_profile_picture[$node->language][0]['value'] = check_plain($page->picture->data->url);
    }

    // Set current user as Brand Admin.
    $node->field_brand_admins[$node->language][0]['target_id'] = $uid;

    $node = node_submit($node); // Prepare node for a submit

    dpm($node);
    var_dump($node);

    node_save($node);
  }
}

And this is the node-object just right before 'node_save', which creates the error:

object(stdClass)[235]
  public 'type' => string 'brand' (length=5)
  public 'title' => string 'Kagerullen' (length=10)
  public 'language' => string 'und' (length=3)
  public 'status' => int 1
  public 'promote' => int 0
  public 'sticky' => int 0
  public 'uid' => int 0
  public 'created' => int 1392660201
  public 'revision' => boolean false
  public 'comment' => string '1' (length=1)
  public 'menu' => &
    array (size=12)
      'link_title' => string '' (length=0)
      'mlid' => int 0
      'plid' => int 0
      'menu_name' => string 'main-menu' (length=9)
      'weight' => int 0
      'options' => 
        array (size=0)
          empty
      'module' => string 'menu' (length=4)
      'expanded' => int 0
      'hidden' => int 0
      'has_children' => int 0
      'customized' => int 0
      'parent_depth_limit' => int 8
  public 'field_facebook_page_unique_id' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_facebook_id' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_facebook_username_term' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_category_term' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_brand_fans_count' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_cover_picture' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_profile_picture' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'field_brand_admins' => &
    array (size=1)
      'und' => &
        array (size=1)
          0 => &
            array (size=1)
              ...
  public 'validated' => boolean true

Heres an image of the 'backend' for the node-fields, where each MultiFields is pointed out: http://d.pr/i/cC44

dave reid’s picture

To answer the first part of setting to default value, it looks like it should be automatically handled by field_default_insert. I'll investigate what happens when creating a node via code.

dave reid’s picture

Version: 7.x-1.0-unstable9 » 7.x-1.x-dev
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.6 KB

Ok I think I have this narrowed down. Basically, I think this would be fixed by the core patch in #1899498: Field default values do not get hook_field_presave() run on them. Until that patch lands, however, I think we could work around for now. So let's try this patch.

dave reid’s picture

StatusFileSize
new4.36 KB

With a test as well!

dave reid’s picture

Status: Needs review » Fixed

Confirmed and committed #5 to 7.x-1.x. http://drupalcode.org/project/multifield.git/commit/e341e3c

Thank for helping report the bug in the first place!

sbilde’s picture

Status: Fixed » Needs review
sbilde’s picture

You are awesome Dave! - That patch did the trick. - Now I just have to go through it, and try to understand it :) - Thanks for your great effort!

Status: Needs review » Needs work

The last submitted patch, 5: 2198697-field-default-value-presave.patch, failed testing.

dave reid’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

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