Skip to main content
remove tag from title
Source Link
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133

I am using Drupal 8 and tryI'm to validate the creation of a new content node, of a custom content type. It seems I cannot find the right hook.

I tried using hook_node_presave, however, the only way to stop node from creating is to throw an exception. This is rather user unfriendly.

 function custom_module_node_presave($node) {

    if($node->getType()=='custom_content_type'){
            if(some_business_rules_apply()){
                    drupal_set_message('You cannot add a new node.','error');
                    # cancel saving 
                    throw new \Exception(t('You cannot add a new node.'));
            }else{
                    # do something more
            }
    }
}

I tried to use hook_node_validate but it seems like it is not called. Do I miss something? Maybe, I use the wrong name for the hook function? Is there some kind of example for hook_node_validate?

function custom_type_node_validate(\Drupal\Core\Entity\EntityInterface $node, $form, &$form_state){
    \Drupal::logger('My logger')->info('Node validate');
}

}

I am using Drupal 8 and try to validate the creation of a new content node, of a custom content type. It seems I cannot find the right hook.

I tried using hook_node_presave, however, the only way to stop node from creating is to throw an exception. This is rather user unfriendly.

 function custom_module_node_presave($node) {

    if($node->getType()=='custom_content_type'){
            if(some_business_rules_apply()){
                    drupal_set_message('You cannot add a new node.','error');
                    # cancel saving 
                    throw new \Exception(t('You cannot add a new node.'));
            }else{
                    # do something more
            }
    }
}

I tried to use hook_node_validate but it seems like it is not called. Do I miss something? Maybe, I use the wrong name for the hook function? Is there some kind of example for hook_node_validate?

function custom_type_node_validate(\Drupal\Core\Entity\EntityInterface $node, $form, &$form_state){
    \Drupal::logger('My logger')->info('Node validate');

}

I'm to validate the creation of a new content node, of a custom content type. It seems I cannot find the right hook.

I tried using hook_node_presave, however, the only way to stop node from creating is to throw an exception. This is rather user unfriendly.

 function custom_module_node_presave($node) {

    if($node->getType()=='custom_content_type'){
            if(some_business_rules_apply()){
                    drupal_set_message('You cannot add a new node.','error');
                    # cancel saving 
                    throw new \Exception(t('You cannot add a new node.'));
            }else{
                    # do something more
            }
    }
}

I tried to use hook_node_validate but it seems like it is not called. Do I miss something? Maybe, I use the wrong name for the hook function? Is there some kind of example for hook_node_validate?

function custom_type_node_validate(\Drupal\Core\Entity\EntityInterface $node, $form, &$form_state){
    \Drupal::logger('My logger')->info('Node validate');
}

8 - Validate a custom type node before submitting

Source Link

8 - Validate a custom type node before submitting

I am using Drupal 8 and try to validate the creation of a new content node, of a custom content type. It seems I cannot find the right hook.

I tried using hook_node_presave, however, the only way to stop node from creating is to throw an exception. This is rather user unfriendly.

 function custom_module_node_presave($node) {

    if($node->getType()=='custom_content_type'){
            if(some_business_rules_apply()){
                    drupal_set_message('You cannot add a new node.','error');
                    # cancel saving 
                    throw new \Exception(t('You cannot add a new node.'));
            }else{
                    # do something more
            }
    }
}

I tried to use hook_node_validate but it seems like it is not called. Do I miss something? Maybe, I use the wrong name for the hook function? Is there some kind of example for hook_node_validate?

function custom_type_node_validate(\Drupal\Core\Entity\EntityInterface $node, $form, &$form_state){
    \Drupal::logger('My logger')->info('Node validate');

}