You are here

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

Same name and namespace in other branches
  1. 8 onlyone.module \onlyone_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

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

Code

function onlyone_form_node_form_alter(&$form, &$form_state, $form_id) {

  // Getting the configured content types to have only one node.
  $onlyone_content_types = variable_get('onlyone_node_types');

  // Getting the name of the node type.
  $content_type = $form['type']['#value'];

  // Getting the node.
  $node = $form_state['node'];

  // Verifying if the new node should by onlyone and if we are trying to create
  // a new node.
  if (isset($onlyone_content_types) && in_array($content_type, $onlyone_content_types, TRUE) && (!isset($node->nid) || isset($node->is_new))) {

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

    // If we have one node, then redirect to the edit page.
    $nid = _onlyone_exists_nodes_content_type($content_type);
    if ($nid) {
      drupal_goto('node/' . $nid . '/edit');
    }
  }
}