Functionally, what I want to achieve is to duplicate a node (with a different node-type) when creating/inserting.

To be more precise: I have two content types: news and press-release. What I want to achieve is that when a press-release is made in admin, a news item is automatically created as well.

I looked into D8 hooks (hook_ENTITY_TYPE_insert to be specific), but can't get it to work, the documentation is confusing to me. This is what my .theme file looks like:

 * @file
 * Bootstrap sub-theme.
 * Place your custom PHP code in this file.

 * Implements hook_ENTITY_TYPE_insert().
function hook_persbericht_insert(Drupal\Core\Entity\EntityInterface $entity)
    if (isset($entity->field_duplicate_to_news) && $entity->field_duplicate_to_news[0] === 'on')
        // create a new news-article here

Any help and suggestions are welcome

  • 3
    hook_ENTITY_TYPE_insert() is the right approach. in your case, hook_node_insert(). As always, when asking questions about code, share your existing code and what you tried. See also wizzlern.nl/drupal/drupal-8-entity-cheat-sheet
    – Berdir
    Mar 4, 2016 at 15:06
  • 1
    The hook you implemented has the wrong name. I am sure the short name of the theme is not hook.
    – apaderno
    Mar 10, 2016 at 14:38
  • Hi kiamlaluno, than what should it be? My theme name is delta My content type is called persbericht. Is the comment block configured correctly? Mar 11, 2016 at 7:56
  • Reopening b/c that is the acutal code. However, I suspect we have a duplicate of this.
    – mpdonadio
    Mar 11, 2016 at 13:40
  • OK. So far we tried the following solutions: codeshare.io/6iOzq But to no avail. Who knows what's going on here. Mar 11, 2016 at 14:29

1 Answer 1


Whenever in drupal documentation it says to use "hook_" it means to replace "hook_" with the name of your module or theme. Often on this site, you'll see "mymodule_" used as an example. When you see "ENTITY_TYPE" in a hook description that is usually going to be "node" "taxonomy_term", "user", etc., it is not the bundle, or subtype of an entity. Drupal nodes are labelled as "Content Types", which is synonymous with bundle. Bundle is the term used across many different entities and is a backend term, while they are labelled different things on the front-end for different entity types. For nodes, they are "Content types", for taxonomy, they are "Vocabularies".

You wouldn't want to use the bundle in your hook if it's looking for "ENTITY_TYPE", so "persbericht" ("press release") isn't correct. You'd want to use node.

Themes can only implement a small number of hooks, such as alter hooks. They can't implement CRUD operations. So you'd want to put this in a custom module.

use Drupal\node\Entity\Node;
 * Implements hook_ENTITY_TYPE_insert().
function mymodule_node_insert(Node $node) {
  if ($node->bundle() == 'press_release') {
    $copy = Node::create([
      'title' => $node->getTitle() . ' copy!',
      'type' => 'article',
      'status' => 0,
    foreach ($node as $key => $value) {
      // Copy values from all fields (fields start with "field_").
      if (strpos($key, 'field_') === 0) {
        if (isset($copy->{$key})) {
  • Themes can only implement alter hooks and a very small number of others (maybe just hook_theme and pre/process). You need a module for any others including entity CRUD ops
    – Clive
    Nov 9, 2017 at 17:47
  • Ah, I'll update my answer. I had tested the code locally in a custom module. I have noticed sometimes hooks don't work in a theme. Now I know why, thanks!.
    – oknate
    Nov 9, 2017 at 17:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.