function forum_node_presave

Same name in other branches
  1. 7.x modules/forum/forum.module \forum_node_presave()
  2. 8.9.x core/modules/forum/forum.module \forum_node_presave()
  3. 10 core/modules/forum/forum.module \forum_node_presave()
  4. 11.x core/modules/forum/forum.module \forum_node_presave()

Implements hook_ENTITY_TYPE_presave() for node entities.

Assigns the forum taxonomy when adding a topic from within a forum.

File

core/modules/forum/forum.module, line 151

Code

function forum_node_presave(EntityInterface $node) {
    if (\Drupal::service('forum_manager')->checkNodeType($node)) {
        // Make sure all fields are set properly:
        $node->icon = !empty($node->icon) ? $node->icon : '';
        if (!$node->taxonomy_forums
            ->isEmpty()) {
            $node->forum_tid = $node->taxonomy_forums->target_id;
            // Only do a shadow copy check if this is not a new node.
            if (!$node->isNew()) {
                $old_tid = \Drupal::service('forum.index_storage')->getOriginalTermId($node);
                if ($old_tid && isset($node->forum_tid) && $node->forum_tid != $old_tid && !empty($node->shadow)) {
                    // A shadow copy needs to be created. Retain new term and add old term.
                    $node->taxonomy_forums[count($node->taxonomy_forums)] = [
                        'target_id' => $old_tid,
                    ];
                }
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.