0

This would be most simple functional question in Drupal 7. Now below mentioned things are mandatory to do and cant change the functionality. 1) I have created a custom block in module. 2) I have included form like below:-

function usercontent_block_view($delta='') 
{

   $block = array();    
   switch($delta)
   {
     case 'user_front_page' :        
       $block['content'] = drupal_get_form('genre_all_login');                       
     break;
   }
   return $block; 
}

3) Now I have some other content in block but which put me in trouble is shown below:-

function genre_all_login($form) {
  $form['new_user'] = array(
  '#type' => 'item',
  '#markup' => l('Add New User','http://www.google.com'),            
    );
    return $form; 
  }

4) So it includes a link which will move me to Google once clicked. 5) Now I want to validate certain content inside the block before user navigates to Google.So if the condition doesn't match then I will stop the user by showing an error message and hence user cannot traverse to Google.

Please tell me if it is possible or should I use different technique to handle this scenario.

1 Answer 1

0

You can achieve this using a client side validation. Like below,

  1. Add an ID attribute to the external link

    $form['new_user'] = array(
      '#type' => 'item',
      '#markup' => l('Add New User','http://www.google.com', array('attributes' => array('id' => 'external-link'))),            
    );
    
  2. Add a JavaScript code to validate and preventi the click.

    jQuery(document).ready(function() {
      jQuery('#external-link').click(function(event) {
        // Perform your validation. If the validation fails prevent events default behavior.
        validated = false;
        if (!validated) {
          event.preventDefault(); // You can also use return false instead of prevent default.
        }
      });
    });
    

Hope this helps :)

2
  • Thanks. In my scenario I want to do PHP validation. Does Drupal provides anything out of box for PHP validation for links? Jul 7, 2013 at 7:01
  • I will try to achieve functionality using this. Thanks for response Jul 8, 2013 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.