Feb 28 2018
Feb 28

Stijn Berkers - Producer

+31 (0)20 - 261 14 99

Currently we are busy building a realtime chat platform called Lus. In Lus we connected Drupal to NodeJS for a blazing fast system, with realtime communication (chats, tasks & file sharing).

Within Lus, people can cooperate in ‘channels’, comparable to the WhatsApp groups. Team communication takes place in these channels. A channels works best if you organize it around a certain topic, like ‘sales’.

As soon as a new channel is being started, existing team members can be added in the easiest way imaginable: with the help of a ‘auto-complete field’: as soon as you start typing, suggestions will immediately pop up, in this case for names of team members:

This auto-complete field has been custom developed by us, in part because our Drupal installation uses custom database charts which aren’t available in Drupal 8 core. How did we do it:

1. The form element

To get started we need the Drupal auto complete form element, allowing users on the front end to pick the desired team members. We define this as follows:

Because we use #autocomplete_route_name element, Drupal knows that such a form element has to be ignored on the front end.

2. Custom route

As you can see in the form element, a reference is made to the route, from which data has to be obtained. We’ll add these in the .routing.yml file:

This routing.yml file is already in our module’s root.

3. Controller with custom query

In the route we just created we refer to a custom controller AutocompleteController, method handleAutocomplete. This one can be found in the map moduleroot/src/Controller:

This method ensures that the right data is collected from the data base and will be given back correctly formatted as well.

Wrap it up

As you can see, suggestions are being given for code corrections. In an optimization run we’ll get onto it. For now, at least the data is coming though correctly and we can create new channels with existing team members by means of a auto-complete field.

Credits header foto: ricardo Gomez Angel

Feb 12 2018
Feb 12

Stijn Berkers - Producer

+31 (0)20 - 261 14 99

Currently we are busy constructing the production of a realtime messaging platform in Drupal and NodeJS, look at it as a ‘WhatsApp for Business’. This Drupal system works like a web app; logging in is mandatory. How do you make sure that logged out visitors must log in to Drupal 8 before they are allowed to continue?

Drupal has many out-of-the-box functionalities, as well as a powerful API, but because it has so many functions many tracks are standardly available for anonymous visitors. We’d want to make all paths unreachable, until you log in.

That means that visitors always will be redirected to the login screen as long as they aren’t logged in. You wouldn’t want an anonymous user reaching internal news on the homepage.

Redirect URL in Drupal 8

  • Basically, we want all url’s / paths be made unavailable for non-logged in visitors, except explicitly specified pages like:
  • Login (/user)
  • Forgot password (/user/password)
  • Login link (user/reset/login)

in Drupal 7 you could use the module Logintoboggan for that purpose. You could also easily work around it in hook_init() or hook_boot() in a custom Drupal 7 module.

Quest

This was quite a puzzle, and we soon found some examples as well as exceptions. Everytime it didn’t work how we wanted it to. This example was the most useful.

Implementation in Drupal 8

Eventually, we got it working with the help of following code in a custom Drupal 8 module:

services.yml

put this file in your module root, and format yourmodulename.services.yml:

RedirectAnonymousSubscriber.php

Put the file RedirectAnonymousSubscriber.php in folder /src/EventSubscriber/ and do your custom thing:

Deze code haakt in op de EventSubscriber van Symfony, het framework waar Drupal 8 op gebouwd is.

Wrap up

Alright, that’s it. I hope the information as described will help you to always redirect visitors to the login page. Questions or feedback? Let me know!

We zoeken altijd naar optimale inzet van Drupal contrib modules. Maar in sommige gevallen voldoen ze niet aan benodigde eisen, wensen, stabiliteit en/of veiligheid. In dat geval ontwikkelen we de code op maat; eventueel met behulp van snippets uit andere modules.

Wanneer we dan maatwerk moeten ontwikkelen proberen we het altijd generiek maken, zodat er eventueel een contrib module van geproduceerd kan worden en vrijgegeven op Drupal.org. Dit doen we altijd in overleg met opdrachtgever.

Zo’n voorbeeld is de zojuist vrijgegeven module ‘Conditional Redirect’: een aantal modules kwamen in de buurt, maar voldeden net niet.

Drupal geheel afschermen middels login

Het gaat om volgende functionaliteit: stel dat je een systeem in Drupal produceert wat bedoeld is voor intern gebruik zoals een social intranet of project management app.

Dit systeem moet wel wereldwijd beschikbaar zijn, maar alleen toegankelijk nadat mensen ingelogd zijn. In dit geval wil je graag dat álle bezoekers die niet zijn ingelogd naar de login page worden door gelinkt.

Als extra wens: bepaalde pagina’s moeten wél publiekelijk beschikbaar zijn en dus als uitzondering hierin ingesteld worden.

Bovenstaande is gerealiseerd in de vrijgegeven module: na installatie hiervan zullen alle uitgelogde (anonieme) bezoekers naar de login page geredirect worden. Tevens zijn hierin uitzonderingen te configureren:

  • Content types;
  • Specifieke pagina’s;
  • Specifieke menu links.

Download de module hier

Wrap up

Alright, that’s it. Vragen of opmerkingen over deze module? Let me know, of schiet een issue in op de project page op Drupal.org

Photo by Dmitri Popov on Unsplash

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web