I have 3 taxonomy vocabularies:
- Fruits
- Vegetables
- 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 theFruits
Taxonomy Vocabularyvegetables_category
which is a non-required, multi-value Entity Checkboxes element referencing theVegetables
Taxonomy Vocabularydrinks_category
which is a non-required, multi-value Entity Checkboxes element referencing theDrinks
Taxonomy Vocabulary
Now on webform submit, I want to:
- Create a new node of type
article
. - Set the values of
field_article_all_items
equal to all selected values fromfruits_category
,vegetable_category
anddrinks_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
?