0

I have 3 taxonomy vocabularies:

  1. Fruits
  2. Vegetables
  3. Drinks

On the Article content type, I have set a multi-value entity reference field field_article_all_items referencing all of Fruits, Vegetables and Drinks taxonomy vocabularies.

Using the Webform module, I have created a webform with 3 elements:

  • fruits_category which is a non-required, multi-value Entity Checkboxes element referencing the Fruits Taxonomy Vocabulary
  • vegetables_category which is a non-required, multi-value Entity Checkboxes element referencing the Vegetables Taxonomy Vocabulary
  • drinks_category which is a non-required, multi-value Entity Checkboxes element referencing the Drinks Taxonomy Vocabulary

Now on webform submit, I want to:

  1. Create a new node of type article.
  2. Set the values of field_article_all_items equal to all selected values from fruits_category, vegetable_category and drinks_category.

In my customhandler.php file, I have tried:

// Get an array of the values from the submission.
    $values = $webform_submission->getData();

// Get 'Fruits Category' Value.
    $FruitsCategory = $values['fruits_category']; // Multi-value tid

// Get 'Vegetables Category' Value.
    $VegetablesCategory = $values['vegetable_category']; // Multi-value tid

// Get 'Drinks Category' Value.
    $DrinksCategory = $values['drinks_category']; // Multi-value tid

// Put all values in an array().
    $AllItems = array($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' => 'article',        
        'title' => Test,
        'langcode' => 'en',
        'uid' => '1',
        'status' => '1',
        'created' => $current_time,
        'field_article_all_items' => $AllItems,       
        ]);
// Save the created node.
    $article->save();

the above code created a new node but with field_article_all_items field empty.

Also I have tried:

// Get an array of the values from the submission.
    $values = $webform_submission->getData();

// Get 'Fruits Category' Value.
    $FruitsCategory = $values['fruits_category']; // Multi-value tid

// Get 'Vegetables Category' Value.
    $VegetablesCategory = $values['vegetable_category']; // Multi-value tid

// Get 'Drinks Category' Value.
    $DrinksCategory = $values['drinks_category']; // Multi-value tid

// Put all values in an array().
    $AllItems = array($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' => 'article',        
        'title' => Test,
        'langcode' => 'en',
        'uid' => '1',
        'status' => '1',
        'created' => $current_time,
        //'field_article_all_items' => $AllItems,       
        ]);
// Save the created node.
    $article->save();

foreach ($AllItems as $Item) {
  $article->field_article_all_items[] = $Item;
}

// Save the created node again.
    $article->save();

// Also I have tried the below method.
//   $article->field_article_all_items[] = ['target_id' => $AllItems]

  //Save the created node again.
//   $article->save();

But also this code created a new node but with field_article_all_items field empty.

How to set the values of field_article_all_items equal to fruits_category + vegetable_category + drinks_category ?

0
0

Educated guess: (The key was to merge the arrays by using array_merge())

// Put all values in an array().
$AllItems = array_merge($FruitsCategory, $DrinksCategory, $DrinksCategory); // use array_merge

foreach ($AllItems as $Item) {
  $article->field_article_all_items[] = ['target_id' => $Item];
}

$article->save();

Also it can be used within the node creation:

// Put all values in an array().
    $AllItems = array_merge($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' => 'article',        
        'title' => Test,
        'langcode' => 'en',
        'uid' => '1',
        'status' => '1',
        'created' => $current_time,
        'field_article_all_items' => $AllItems,  // use array_merge
        ]);
// Save the created node.
    $article->save();

Your Answer

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