You are here

function forum_node_presave in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/forum/forum.module \forum_node_presave()
  2. 7 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
Provides discussion forums.

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,
          ];
        }
      }
    }
  }
}