function forum_node_presave

forum_node_presave(EntityInterface $node)

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 149
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)] = array('target_id' => $old_tid);
        }
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!forum!forum.module/function/forum_node_presave/8.1.x