Hello, I need to create a link with the class' use-ajax ", but I need inside the link an image, I tried to do with the function l() but prints the link address.

Comments

nevets’s picture

What does your code look like?

Crackall’s picture

I'm creating a block which has some images and in the onclick event of one of these I need an ajax call. This is my code implementing hook_block_view($delta):

case 'mymodule':
$nodes = entity_load('node', FALSE, array('type' => 'some'));
$html = "

";
foreach ($nodes as $node){
$dir = file_create_url($node->field_image['und'][0]['uri']);
$html .= "< a href = 'fbf/$node->nid'> < div id ='div$node->nid'> < img src='$dir'>".'< /div> < /a>';
$cont++;
}
$html .= '< /div>';
$block['content'] = $html;
pushpinderchauhan’s picture

	// Example Code :
        global $base_url;

        print l(
          '<img src="' . render(file_create_url($node->field_image['en'][0]['uri'])) . '"/>', 
          $base_url . $node_url, 
            array(
              'attributes' => array(
                'id' => 'my-id', 
                'class' => array('use-ajax')
              ), 
              // Need to add this otherwise the image tag is
              // printed to the browser, rather than the image the
              // tag represents:
              'html' => TRUE
            )
        );
 case 'mymodule':
$nodes = entity_load('node', FALSE, array('type' => 'some'));
$html = "";
foreach ($nodes as $node){
$dir = file_create_url($node->field_image['und'][0]['uri']);
$html .= l('<img src="' . render($dir) . '"/>', 
          'fbf/'.$node->nid, 
            array(
              'attributes' => array(
                'id' => 'my-id', 
                'class' => array('use-ajax')
              ), 
              // Need to add this otherwise the image tag is
              // printed to the browser, rather than the image the
              // tag represents:
              'html' => TRUE
            )
        );
$cont++;
}
$html .= '< /div>';
$block['content'] = $html;

There might be some syntax mistake in code. Important thing is $text(first parameter of l function ) is not sanitized if 'html' is TRUE.

Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert

Crackall’s picture

Thanks, with that parameter I solved the problem