You are here

function node_limit_node_prepare in Node Limit 8

Same name and namespace in other branches
  1. 7 node_limit.module \node_limit_node_prepare()

Implements hook_node_prepare().

This is where we'll determine if the user may create new nodes or not. We'll use hook_node_prepare, which is sent before the edit/add form is constructed.

File

old/node_limit.module, line 188

Code

function node_limit_node_prepare($node) {
  if (empty($node->nid) && _node_limit_violates_limit($node)) {

    // We have a violation
    // and this is a new node.
    $nodetype = node_type_get_type($node);
    \Drupal::messenger()
      ->addError(t("You can't create more content of type !type", array(
      '!type' => check_plain($nodetype->name),
    )));

    // Avoid redirection loop if there is just one content type.
    $count = 0;
    foreach (node_type_get_types() as $type) {
      if (node_limit_access($type->name)) {
        $count++;
      }
    }
    if ($count > 1) {
      drupal_goto('node/add');
    }
    else {
      drupal_goto('');
    }
  }
}