0

im trying to create a community select box to enable visitors to jump to open Liferay communities. However it only seems to work for authenticated users. So how I can list 'open' communities to all users including logged out users?

Here's my current code

#set ($myPlaces = $user.getMyPlaces())
    #if($listTool.size($myPlaces) > 0)

        <select id="communitySelector">

        #foreach ($myPlace IN $myPlaces)
            #if ($myPlace.isCommunity())

                #set ($myPlaceURL = ${myPlace.getFriendlyURL()})
                ## Only link if theres pages
                #if ($myPlace.hasPublicLayouts())
                    ## Prefix web for 'public'
                   #set ($myPlaceURL = "${public_pages_url}${myPlaceURL}")

                   ## Select if current community
                     #set($commSelected = "")
                    #if($themeDisplay.getLayout().getGroup().getName() == $myPlace.getName())
                        #set($commSelected = " selected='selected' ")
                    #end
                    <option $commSelected value="${portal_url}${myPlaceURL.toString()}">$myPlace.getName()</option>


               #end

            #end
        #end
            </select>
    #end
experimenter
  • 768
  • 1
  • 9
  • 30
  • So I figured out MyPlaces is only populated for logged in users :( . Is the only solution to create a user assign them to all available communities and try and list communities for that user id? – experimenter Feb 04 '12 at 02:03

1 Answers1

1

In the long tradition of answering my own Liferay questions here is the code snippet for the solution I came up with for listing Communities to all users including the public/guest logged out users

  ## Grab this service as MyPlaces only available to authenticated users
  #set($groupLocalService =  $serviceLocator.findService("com.liferay.portal.service.GroupLocalService"))

  ## Get all Groups
  #set($groupList = $groupLocalService.getGroups(-1, -1))

  ## Grab all groups that are Communities
  #set($commList = [])
  #foreach($group in $groupList)

    #if($group.isCommunity())

        $commList.add($group)
    #end
  #end

  #foreach($comm in $commList)
    ## Exclude Control Panel which is also validated as Community in LR
    #if($comm.getName()!="Control Panel")
        #if($comm.hasPublicLayouts())
            <a href="$comm.getFriendlyURL()">$comm.getName()</a><br />
         #elseif($comm.hasPrivateLayouts() && $is_signed_in)
            ## Community is private so only print this link if user authenticated
            <a href="$comm.getFriendlyURL()">$comm.getName()</a><br />
         #end
    #end
  #end

EDIT - Here's a similar snippet someone else posted https://stackoverflow.com/a/8457759/417933

Community
  • 1
  • 1
experimenter
  • 768
  • 1
  • 9
  • 30