1

I am trying to create a custom button in Drupal 7. The link which the href should be equal to is dependent on the context such as the current UID.

I have created a view which normally has a plain text output as [title].

I want to use that text output as part of a link field such as

http://my-site.com/page/title

so I had written the code for the above link as such:

<a href="http://my-site.com/page/<?php $block = module_invoke('views', 'block_view', 'profile3-block_4'); print render($block[content]); ?>">LINK TEXT</a>

however the output seems as if it is placing part of a views div tag in the link instead. I tried stripping all html from the views block but no success. Anyone got ideas? Could it be an improper use of quotes?

3 Answers 3

0

print render($block[content]); returns a lot of tags. It simply cannot be a valid part of URL string. Also, be sure to avoid module_invoke in tpl files if feasible.

0

You can try like this:

$block = module_invoke('views', 'block_view', 'profile3-block_4'); 
$url = render($block[content]);
Assume your Output will be <div>TITLE</div>
then use **$url =  strip_tags($url);**
<a href="http://my-site.com/page/".$url>LINK TEXT</a>
It will be better to use **l()**
l(t('Link text'), 'http://my-site.com/page/'.$url);

If you are still facing issue request you to provide block html output.

0

If the url is one of the fields returned in the view query then you could access the raw data of the url by calling views_get_view_result($name, $display_id).

If it returns a uri then you may need to pass it though file_create_url() to get a url.

If the row returns a nid then the url might be generated via something like drupal_get_path_alias('node/' . $nid) (adapt as required for taxonomy terms, users, etc).

3
  • THank you . I like your comment best. I will give this a try and get back to you
    – Cybergei
    Commented Aug 18, 2015 at 18:36
  • I assume that $name is the system field name? and $display_id is the block or page delta?
    – Cybergei
    Commented Aug 18, 2015 at 18:38
  • $name is the machine name of the entire view and $display_id is the machine name of the individual display within the view. An easy way to find these is to go to the edit page of the view and look at the url. It should be either "/admin/structure/views/view/VIEW_NAME/edit/DISPLAY_ID" or "admin/structure/views/view/VIEW_NAME", where VIEW_NAME will be replaced with the value you need for $name and DISPLAY_ID will be the replaced with the value you need for $display_id. If it is the shorter version of the path and there is no $display_id then just call views_get_view_result($name). Commented Aug 19, 2015 at 8:33

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.