Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Piña Kanpokaldean
Facebook sharing links to a random image of my site
April 14, 2012 5:11 AM
Answer

Piña Kanpokaldean

Rank: Junior Member

Posts: 79

Join Date: April 14, 2012

Recent Posts

Hi,

I have an issue with facebook sharing. The sharing button is made this way:

1<iframe src="http://www.facebook.com/plugins/like.php?href=http://emphas.is/web/guest/discoverprojects?projectID=<%=projectVO.getProjectId() %>&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" width="47" frameborder="0" style="border:none; overflow:hidden; width:50px; height:24px;" allowTransparency="true"></iframe>


While this does work, it seems to pick a random image from the site as a thumbnail in facebook. I have been checking online, and it would seem I need to add a meta tag in the head to specify which image should be taken, like so:

1   <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>


I know I can put this on the head within the theme. However, I need to specify which image I want according to the project, which is known only on the portlet. How can I modify the header to specify this property from the portlet? Or is there any other way of doing this?

As additional difficulty, I am pretty new at liferay, Facebook is a big unknown and have just been put in charge of a textbook legacy code application, so I'm quite lost here. I've seen in some other page of my application they define meta properties right on the portlet, and then close a </head> tag that was never opened within the portlet. I'll now check if that works, but I find it horrifying and assume there must be a more elegant way of dealing with this problem.

fyi, using liferay 6.0.26 bundled with tomcat.

Thanks for your help.

Edit: I confirmed the legacy properties in the portlet do not work. Why they were there is anybody's guess.
Jelmer Kuperus
RE: Facebook sharing links to a random image of my site
April 14, 2012 5:53 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

jsr286 portlets can add stuff to the header. See this post http://www.liferay.com/community/forums/-/message_boards/message/13229925
Piña Kanpokaldean
RE: Facebook sharing links to a random image of my site
April 14, 2012 8:51 AM
Answer

Piña Kanpokaldean

Rank: Junior Member

Posts: 79

Join Date: April 14, 2012

Recent Posts

Thanks, that's exactly the kind of elegant solution I was looking for. Checking it out now, but I haven't been able to get it working so far.

I added the snippet to portlet.xml as the last thing of the portlet (right before </portlet>, and that's straight forward, so I don't think the error is there. However, I simplified my code on the portlet to the point of commenting it out and copy pasting the example on the page you provided, and it just doesn't work. It doesn't give me an error as clue either, it just runs without any trouble and then, when I check the head of the resulting page with firebug, the tags simply aren't there.

Could I be placing the xml on the wrong place? Or are the generated tags not supposed to be viewable that way?

This is my full code, in case it helps. I have debugged through it and it all goes through without errors, and I've seen the values within the response. But they don't show up.

 1    @Override
 2    protected void doHeaders(RenderRequest request, RenderResponse response) {
 3        HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
 4        String projectID = httpReq.getParameter("projectID");
 5        String pagename = httpReq.getParameter("pagename");
 6        //TODO: get this dynamically
 7        String portalURL = "http://www.emphas.is";
 8       
 9        if ((projectID != null)) {
10            if (pagename == null)
11            {
12                ProjectVO project = DBUtil.getSingleProjectData(projectID).get(0);
13                if (project.getEbook()==1) {
14                    pagename = "bookproject";
15                } else {
16                    pagename = "discoverprojects";
17                }
18                addMetaTag(response, "og:title", project.getTitle());
19                addMetaTag(response, "og:image", portalURL + project.getProjectCover());
20                addMetaTag(response, "og:url", portalURL+"/web/guest/"+pagename+"?projectID="+project.getProjectId());
21                addMetaTag(response, "og:video", project.getVideoUrl());
22                addMetaTag(response, "og:video:height", "200");
23                addMetaTag(response, "og:video:width", "300");
24                addMetaTag(response, "og:video:type", "application/x-shockwave-flash");
25            }
26        }
27    }
28
29    private void addMetaTag(RenderResponse response, String name, String content) {
30        Element element = response.createElement("meta");
31        element.setAttribute("name", name);
32        element.setAttribute("content", content);
33        response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, element);
34    }
Jelmer Kuperus
RE: Facebook sharing links to a random image of my site
April 14, 2012 8:58 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

The guy in the thread i linked ran into what appeared to be a bug for which i suggested a workaround.
Did you check if this is the same thing ?
Piña Kanpokaldean
RE: Facebook sharing links to a random image of my site
April 14, 2012 9:08 AM
Answer

Piña Kanpokaldean

Rank: Junior Member

Posts: 79

Join Date: April 14, 2012

Recent Posts

Yes, I tried commenting out all my stuff and leaving just his code. Same result, it goes through without complaining, but then I can't see the new head elements through bugzilla.
Jelmer Kuperus
RE: Facebook sharing links to a random image of my site
April 14, 2012 12:20 PM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

Bugzilla is a bugtracker emoticon , do you mean firebug?

Use good old fashion view source instead (also add ?js_fast_load=0&strip=0 to your url to make the html more readable)

Firebug works on the processed DOM, so after firebug has "fixed" all invalid html, so if you are running into the same bug the other guy was running into, you would probably see nothing in firebug but would see something in view source
Piña Kanpokaldean
RE: Facebook sharing links to a random image of my site
April 16, 2012 10:52 AM
Answer

Piña Kanpokaldean

Rank: Junior Member

Posts: 79

Join Date: April 14, 2012

Recent Posts

I did mean Firebug, but I'm pretty confident my meta tags don't appear in bugzilla no matter how I configure my local server, so it's not like I was completely mistaken emoticon

Unfortunately, it doesn't show up in view source either. The horrible part of this is, it may actually be a perfectly correct code but because this is a legacy application, something could have a weird configuration or be overriding what I do. After having seen IP and pw for db connections hardcoded on each jsp file, I'm ready to believe anything about my system.

I think I'lll give it another try outside my local machine, then if it doesn't work I'll check if I can get by with javascript. The cleaning up can wait till I stabilize things. If that doesn't work either, I'll have to wait and ask our 'liferay expert' about it; but since he's the one who put header tags on the portlet, I'm not hoping for much. I've heard it was working at some point, so he may know some crap hack I can use for the time being.

Edit: I had a chance to talk with the previous team, and they came up with some solution for the facebook problem where the link itself passes the info, like so:

 1    <a title="Share this article/post/whatever on Facebook"
 2    href="http://www.facebook.com/sharer.php?
 3    s=100
 4    &p[url]=<%=portalUrl%>/web/guest/discoverprojects?projectID=<%=projectVO.getProjectId() %>
 5   
 6    &p[title]=<%=projectVO.getTitle()%>
 7    &p[summary]=<%=projectVO.getProject_description()%> 
 8    &p[image]=<%=portalUrl + projectVO.getProjectCover() %>
 9    target="_blank">
10     <img src="/html/images/dis-pro-icon3.jpg"
11       />
12</a>


I still can't tags to the header, which bothers me, but that's not directly related to the facebook so I'll close this thread.