function forum_node_validate

Implements hook_node_validate().

Checks in particular that the node is assigned only a "leaf" term in the forum taxonomy.

File

drupal/core/modules/forum/forum.module, line 293
Provides discussion forums.

Code

function forum_node_validate(EntityInterface $node, $form) {
  if (_forum_node_check_node_type($node)) {
    $langcode = $form['taxonomy_forums']['#language'];

    // vocabulary is selected, not a "container" term.
    if (!empty($node->taxonomy_forums[$langcode])) {

      // Extract the node's proper topic ID.
      $containers = config('forum.settings')
        ->get('containers');
      foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {

        // If no term was selected (e.g. when no terms exist yet), remove the
        // item.
        if (empty($item['tid'])) {
          unset($node->taxonomy_forums[$langcode][$delta]);
          continue;
        }
        $term = taxonomy_term_load($item['tid']);
        if (!$term) {
          form_set_error('taxonomy_forums', t('Select a forum.'));
          continue;
        }
        $used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid', 0, 1, array(
          ':tid' => $term
            ->id(),
          ':vid' => $term
            ->bundle(),
        ))
          ->fetchField();
        if ($used && in_array($term
          ->id(), $containers)) {
          form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array(
            '%forum' => $term
              ->label(),
          )));
        }
      }
    }
  }
}