1

I have this problem: I created three content types. Actor, author and musician. An user can modify own actor, author and musician profile, if his role is one of these.

A user can have more than one role, so, he can be actor, author and musician. An actor can be a man or a woman, so his role canbe actor or actres. In user menu I must have links on pages for these roles, so, an actor can have actor/[user:name] and an author can be author/[user:name]. Title of these links should be [user-name] actor or actres depending on the role.

Ok, I can use user-name token, but how can I retrieve the role? Can I use php code on title of link?

  • Can you add a Drupal version tag? – Neograph734 Dec 30 '16 at 20:57
  • I'm sorry, drupal version is 7.53. – Germano Carella Dec 30 '16 at 21:40
  • Below logic still applies, but the hooks take slightly different parameters. Click the links below and then at the top switch to the d7 version. – Neograph734 Dec 30 '16 at 21:50
  • I found custom tokens module, that allows me to define custom tokens. So, I defined a field named actor_genre in my actor_profile content type. Now, I need a way to retrieve this field value that can be actor or actres. So, in custom tokens, I can define php code for getting these info. Now, where I can find this value? perhaps $actor_profile->$field_actor_genre? – Germano Carella Dec 31 '16 at 8:37
  • If it is a role you can use user_has_role() to check it. – Neograph734 Dec 31 '16 at 9:23
0

Since you did not specify a version I am assuming Drupal 8, but the hooks are nearly the same for Drupal 7. (You can navigate to them from the linked documentation pages).

The problem is that a user can have multiple roles and they can be assigned in a random order. This way you can never be sure that the correct role is shown.

You could try to provide Drupal with some custom tokens for these roles. This means you'll have to create a small custom module, but that is really not that hard.

Then you'll have to implement 2 hooks in your .module file:

hook_token_info_alter(). This allows for the tokens to show up in the token browser.

hook_tokens_alter(). The code could look something like this:

function <modulename>_token_info_alter(&$data) {
  // Add description of user tokens.
  $data['tokens']['user']['actor-role'] = array(
    'name' => t("Actor role"),
    'description' => t("Role if the user is an actor."),
  );
  $data['tokens']['user']['author-role'] = array(
    'name' => t("Author role"),
    'description' => t("Role if the user is an actor"),
  );

  // Add as much as you like (need).

}

function <modulename>_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $options = $context['options'];

  if ($context['type'] == 'user' && !empty($context['data']['user'])) {
    $user = $context['data']['user'];

    if (isset($context['tokens']['actor-role'])) {
      // PHP logic to determine the role. Below is an assumption and might not work.
      // Set the value to the actor-role token to 'Actor' or nothing.
      $actor-role = $user->hasRole('actor_role_id') ? t('Actor') : '';
      $actress-role = $user->hasRole('actress_role_id') ? t('Actress') : '';

      // Combine them (one should be empty leaving the other)
      $actor-role-to-show = $actor-role . $actress-role

      $title = $node->field_title->view('default');
      $replacements[$context['tokens']['actor-role']] = $actor-role-to-show;
    }

    // Again add as many as you like.
  }
}

Now you should be able to use [user:author-role] and any other tokens you have defined.

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.