Skip to main content
edited tags (comment edited Feb 19, 2022 at 13:42)
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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?

I’m trying to setup a custom module which clones the content from NODETYPE_A to NODETYPE_B. For most part, both nodetypes use the same fields, but NODETYPE_B has a few additional ones. For example:

NODETYPE_A:

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)

NODETYPE_B:

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)
  • Number of items (field_int_how_many)
  • Reference to NodeTypeA (field_noderef_nta)

Now, with the build in functionality “$cloned_node = $node->createDuplicate()” I am able to clone all fields from NODETYPE_A to NODETYPE_B, as well as the ability to change the nodetype from NODETYPE_A to NODETYPE_B.

Since NODETYPE_A doesn’t have the extra fields (field_int_how_many and, field_noderef_nta) attached to it to begin with, createDuplicate() obviously won’t copy them to NODETYPE_B.

Now, what I would like to do, is programmatically adding the other fields (which I will be populating with some custom values) to $cloned_node. When these actions are completed, $cloned_node->save(); will be called to finalize the cloning process.

Simply adding the value via $cloned_node->set("field_int_how_many", $value) doesn't seem to to the trick; an php-error gets triggered, with the following watchdog entry:

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).

Does anyone has an idea how to add those “extra/custom” fields to the cloned object?

I am trying to setup a custom module which copies values from a node whose content type is A to a node whose content type is B. For the most part, the content types have the same fields, but B has a few additional fields.

A

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)

B

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)
  • Number of items (field_int_how_many)
  • Reference to A (field_noderef_nta)

With $cloned_node = $node->createDuplicate() I am able to copy all the fields from A, as well as being able to change the content type to B.
Since A doesn't have the extra fields attached to it, createDuplicate() won't copy them.

Now, what I would like to do is to programmatically add the other fields (which I will populate with some custom values) to $cloned_node. When these actions are completed, $cloned_node->save() will be used to finalize the cloning process.

Simply adding the value via $cloned_node->set("field_int_how_many", $value) doesn't seem to do the trick: A PHP error is thrown.

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 have an idea how to add those extra fields to the cloned node?

edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Source Link

How to programmatically append fields to node object when cloning with createDuplicate()

I’m trying to setup a custom module which clones the content from NODETYPE_A to NODETYPE_B. For most part, both nodetypes use the same fields, but NODETYPE_B has a few additional ones. For example:

NODETYPE_A:

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)

NODETYPE_B:

  • Title (title)
  • Body (body)
  • Date (field_date_agenda)
  • Number of items (field_int_how_many)
  • Reference to NodeTypeA (field_noderef_nta)

Now, with the build in functionality “$cloned_node = $node->createDuplicate()” I am able to clone all fields from NODETYPE_A to NODETYPE_B, as well as the ability to change the nodetype from NODETYPE_A to NODETYPE_B.

Since NODETYPE_A doesn’t have the extra fields (field_int_how_many and, field_noderef_nta) attached to it to begin with, createDuplicate() obviously won’t copy them to NODETYPE_B.

Now, what I would like to do, is programmatically adding the other fields (which I will be populating with some custom values) to $cloned_node. When these actions are completed, $cloned_node->save(); will be called to finalize the cloning process.

Simply adding the value via $cloned_node->set("field_int_how_many", $value) doesn't seem to to the trick; an php-error gets triggered, with the following watchdog entry:

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).

Does anyone has an idea how to add those “extra/custom” fields to the cloned object?