0

I am looking to create a custom front page for users that are visiting the site. This is a landing page in a way, which will encourage users to register and has different CSS. Once a user is logged in or registered then they can see the normal website. Trying to do this without any modules.

What I have done so far is change the .htaccess file so it loads the index.html first.

Like so:

DirectoryIndex index.html index.php index.htm

The problem with this method is that users who login in still see the index.html page if they click on any link that points to the Home page. Instead I would like them to see the index.php or the normal Drupal site.

If it possible to do this with preprocess page hook? What code should be used?

I am only trying to present a different front page to the visitor when they hit domain.com. All the other pages such as domain.com/about-us and others should display properly regardless of the user role. In other words, I am NOT trying to hide the whole site for visitors.

6
  • 1
    You're not going to do this without any code. You might be able to create suggestions for anonymous or authenticated for the front page but I am not sure if caching will interfere here. If there is a module that can do this, then you should use that. I feel like Page Manager in D7 used to facilitate this. – Kevin May 16 '18 at 13:59
  • I used the Front module but it is crashing my D8 site. – Progonat May 16 '18 at 14:13
  • 1
    I don't know what that means, it does not look like it is fully ported to D8. drupal.org/project/front/issues/2615248 - but it should be possible to create template suggestions for page--front--anonymous and html--front--anonymous depending on current user - but I think you'd also have to work at ensuring that page is uncached. 4k4/Berdir might have better insight here. This is the Page Manager module: drupal.org/project/page_manager - it might support variants like checking the user role. It did in D7. – Kevin May 16 '18 at 14:26
  • 3
    Very much depends how you want to build your front page. The simplest method I can think of would involve creating a very basic piece of content with just title and strapline then adding blocks to create the two variants, using role restrictions to show / hide them. – Alfred Armstrong May 16 '18 at 14:56
  • 2
    @Kevin caching is no problem here, because the cache context for user permissions is default. Progonat, "Has different CSS": You'll find the class user-logged-in or anonymous in the body tag. – 4k4 May 16 '18 at 16:38
1

If the pages are not too different, you could edit the page--front.html.twig and make it something like:

{% if not logged_in  %}
  // add your markup for non-registered users here
{% else %}
  {{ page.content }}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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