1

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.

2
  • I would point the button to a custom controller and put the logic there, return either a redirect response or the content.
    – 4uk4
    Jan 21, 2018 at 10:45
  • Can you explain that with some more detail? Jan 22, 2018 at 13:09

3 Answers 3

1

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 ...;
  }

}
1

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).

1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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