1

I'm building a job directory where users are allowed to register (selecting a membership type) and create Companies and Jobs.

A membership can be Simple, Business or Premium. The membership type is treated in the similar way as a user role. See the list of roles:

enter image description here

My goal is:

  • if the user selects Simple membership (Authenticated user), he / she is going to be able to create only 1 Company and 1 Job;
  • if the user selects Business membership (Business user), he / she is going to be able to create 2 Companies and 3 Jobs.

How can I do this?

Through the Permissions section I was only able to define if the user can add, edit, delete content and that's not enough.

2 Answers 2

1

Sounds like you should have a look at the Group module, which gives you all the features you are looking for, using the "roles" of the Group module (as explained in "What are the various roles supported by the Group module?"), combined with granting the relevant permissions for each of the groups you'd configure. Refer to some of these questions to get you going with the Group module.

BTW, even though this question is about D8, same is true for D7 ...

0
0

I don't think Permissions OOTB will be able to do this for you.

It should be doable through a small bit of custom code. I think what I would do is have a form_alter on the node edit page. In the form alter, get the current user (\Drupal::currentUser()->id()) and using the UID, query for nodes that have that user as the author. If the user is currently at the limit of these node types for their plan, then you can alter the form to not allow access, simply setting #access = FALSE, or overriding the entire form, perhaps display markup for them to upgrade their membership, or whatever. Also, you'd want to add the same sort of logic in a #validate function, which you could add in the same form alter. This custom validate function would perform a similar check - if the user account attempting to create this node is "not allowed" by your business logic, then you return a form error stating this is the case, and this way you won't allow users to create more than their quotient.

Here's some of the data querying stuff to get you started:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
  $user_id = \Drupal::currentUser()->id();
  $roles = \Drupal::currentUser()->getRoles();
  $query = \Drupal::database()->select('node_field_data', 'n');
  $query->fields('n', ['uid']);
  $query->condition('n.uid', $user_id);
  $result = $query->execute()->fetchAll();
  ksm($result);

}

That ksm() statement will require you to enable the kint module, but if you haven't already been aware of and using this, I think you'll see the value as a debugging tool.

This should be enough to get you started. If you get stuck, post some code samples so we could further assess the situation!

Your Answer

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

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