You are here

function onlyone_node_validate in Allow a content type only once (Only One) 7

Implements hook_node_validate().

File

./onlyone.module, line 289
Allows to define if a content type must have more than one node in the site.

Code

function onlyone_node_validate($node, $form, &$form_state) {

  // Getting the configured content types.
  $onlyone_content_types = variable_get('onlyone_node_types');

  // If the content type is configured and the language is locked we need to
  // check if exits a node created in the node language. See:
  // https://www.drupal.org/project/onlyone/issues/2962186
  // https://www.drupal.org/project/onlyone/issues/2969293 .
  if (in_array($node->type, $onlyone_content_types)) {

    // Loading the helper functions file.
    module_load_include('inc', 'onlyone', 'onlyone.helpers');

    // If we have a node in the current language we should not insert a new
    // one.
    $nid = _onlyone_exists_nodes_content_type($node->type, $node->language);

    // If the existing node have the same id that the node that is being saved
    // then is the same node that is being updated.
    if ($nid && $nid != $node->nid) {
      $existing_node = node_load($nid);
      $values = array(
        '%content_type' => $node->type,
        '@href' => url(drupal_get_path_alias('node/' . $existing_node->nid)),
        '!title' => $existing_node->title,
        '%language' => t('Language neutral'),
      );

      // Setting the error in the language field.
      form_set_error('language', t("The content type %content_type is configured to have Only One node per language but the node <a href='@href'>!title</a> exists for the %language language.", $values));
    }
  }
}