Question

I'm setting up a job directory using Drupal 8, together with a team. It is possible to see it here.

As you can see below, in a screenshot of the site, there's a button called "Add new job":

enter image description here

In a more visual way, this is what I would like to implement:

  1. If the user doesn't have login:

    • User clicks button

    • User goes to create account

  2. Else:

    • User clicks button

    • User goes to a specific / different page / view

Something is triggering me that we can achieve this like that using Rules, setting on action events, but I'm not sure how.

It can be that I am totally wrong and there's a more straightforward way to do this.

For example, if I was to implement this just with PHP, would redirect a button to one page and in that page, using user sessions sessions and if conditions, would define the content showing up.

I'm not an expert in Drupal neither I'm aware of all of its possibilities. So, any help is appreciated.

Was it helpful?

Solution 3

I've noticed when the user is logged in, the template adds a class called user-logged-in in the body.

So, in the custom block of the button "Add New Job", added a script to validate if the class is found within the document an, when clicking the button, redirect to different places whether it exist or not.

The script is the following:

<script type"text/javascript">

function checkPage () {

    if (jQuery('.user-logged-in').length > 0) {

         window.location.replace("https://www.google.pt");

    } else {

          window.location.replace("/user/register");

    }

}

</script>

The complete custom block code is:

<style type="text/css">.button {
    background-color: #56739B;
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}
</style>
<script type"text/javascript">
function checkPage () {
if (jQuery('.user-logged-in').length > 0) {
    window.location.replace("https://www.google.pt");
} else {
    window.location.replace("/user/register");
}
}


</script>
<p><button class="button" href="#" onclick="checkPage()">Add a New Job</button></p>

OTHER TIPS

Point the button to a custom controller:

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;

class ExampleController extends ControllerBase {

  /**
   * Controller for action button.
   */
  public function action() {

    // if user is not logged in, redirect to create a new account
    if ($this->currentUser()->isAnonymous()) {
      return $this->redirect('user.register');
    }

    // else return content or redirect to content
    return ...;
  }

}

My approach would be to follow the way that Drupal handles login versus logout: to have two links, one for logged-in users and one for anonymous users.

If the permissions on the two routes are correct, then only one will be visible at a time. Both can look exactly the same, but the destination will differ depending on whether the user is logged in or not.

You can create these as custom menu items, or by any other mechanism where Drupal checks access to the route (so eg. using twig route functions https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates, but not hard-coding links).

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top