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 150
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);
        }
      }
    }
  }
}
doc_Drupal
2016-10-29 09:17:19
Comments
Leave a Comment

Please login to continue.