I’mI am trying to setup a custom module which clones the contentcopies values from NODETYPE_Aa node whose content type is A to NODETYPE_Ba node whose content type is B. For the most part, both nodetypes usethe content types have the same fields, but NODETYPE_BB has a few additional onesfields. For example:
NODETYPE_A:
A
- Title (title)
- Body (body)
- Date (field_date_agenda)
NODETYPE_B:
B
- Title (title)
- Body (body)
- Date (field_date_agenda)
- Number of items (field_int_how_many)
- Reference to NodeTypeAA (field_noderef_nta)
Now, with the build in functionality “$cloned_node = $node->createDuplicate()”With $cloned_node = $node->createDuplicate()
I am able to clonecopy all the fields from NODETYPE_A to NODETYPE_BA, as well as the abilitybeing able to change the nodetype from NODETYPE_A to NODETYPE_Bcontent type to B.
Since NODETYPE_A doesn’t
Since A doesn't have the extra fields (field_int_how_many and, field_noderef_nta) attached to it to begin with, createDuplicate() obviously won’tcreateDuplicate()
won't copy them to NODETYPE_B.
Now, what I would like to do, is to programmatically addingadd the other fields (which I will be populatingpopulate with some custom values) to $cloned_node$cloned_node
. When these actions are completed, $cloned_node->save();$cloned_node->save()
will be calledused to finalize the cloning process.
Simply adding the value via $cloned_node->set("field_int_how_many", $value)$cloned_node->set("field_int_how_many", $value)
doesn't seem to to the trick; an php-error gets triggered, withdo the following watchdog entrytrick: A PHP error is thrown.
InvalidArgumentException: Field field_int_how_many is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (rule 587 of /home/username/domains/domainname.com/public_html/devsite/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
InvalidArgumentException: Field field_int_how_many is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 587 of /home/username/domains/domainname.com/public_html/devsite/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
Does anyone hashave an idea how to add those “extra/custom”extra fields to the cloned objectnode?