6

I am creating a view that will be visible under /user/%/blog, and I am trying to override the view's title with the username (e.g. kiamlaluno's blog), but the description of the field doesn't tell me what I can use to override the title.

screenshot

I only get I can use Twig syntax, but that doesn't help much.

I used Content: Authored by as contextual filter. I also added author as relationship, but I didn't see any change.

How can I override the title with the name of the user authoring the nodes being shown from the view?

4

You can switch the node's uid argument plugin to Drupal\user\Plugin\views\argument\Uid, and get the same behaviour from that field instead. It would take a small amount of code in a custom module.

function MYMODULE_views_data_alter(&$data) {
  $data['node_field_data']['uid']['argument']['id'] = 'user_uid';
}

Alternatively, if passing the author's username in the view URL is not a problem for you, since you have the author relationship set up, you can use the User: User ID field for your filter instead of the one you're currently using. Because of the way its handler treats requests for titles, using {{ arguments.uid }} in the "Override title" field will actually provide you with the username(s) instead of the ID(s).

  • If I set User: Name as contextual filter, the view expects I use it for /user/kiamlaluno/blog, while Drupal uses /user/1 for the user profiles. – kiamlaluno Jun 30 '16 at 20:45
  • Hmmm, understood. So what you need is a contextual filter that works off the user ID, but makes the associated username available in the arguments. Sounds reasonable. I'll have a dig in the code – Clive Jun 30 '16 at 21:00
  • @kiamlaluno Took a while but got something, updated – Clive Jun 30 '16 at 22:33
  • 1
    And {{ raw_arguments.uid }} gives me the user ID, if I really need it. – kiamlaluno Jul 2 '16 at 11:35
5
  1. Make Fields your format

    enter image description here

    Alternatively: if you are using the rendered entity row plugin check the Force using fields in the Format | Settings.

  2. In RELATIONSHIP, Add Content author

    • Checkmark ☑ Require relationship
  3. In FIELDS, add Name

    • Checkmark ☑ Exclude from display
    • Uncheck ☐ Link to the user
  4. For TITLE put {{ name }}'s blog

    enter image description here

    OR you could put it in your CONTEXTUAL FILTER; even though {{ name }} is not listed as a replacement pattern, it works (as long as you do step 1, 2, and 3).

    enter image description here

Result

enter image description here

BONUS

If you want to capitalize the first letter of the user name use {{ name|capitalize }}'s blog.

You can also add a Rendered Entity as a field.

enter image description here

enter image description here

  • 1
    @Clive unintuitive if you're used to solving everything by code not unintuitive at all if you're used to coming up with non coding solutions. – No Sssweat Jul 1 '16 at 9:14
  • 1
    @Nope, read my edited answer. – No Sssweat Jul 1 '16 at 9:23
  • 1
    @Clive already have a "Rendered Entity" I do not since I am using Fields as my format... not content, when you use fields there is no rendered entity unless you add it as a field. – No Sssweat Jul 1 '16 at 9:31
  • 1
    But why would you intentionally restrict yourself to that when the core devs have already given you a proper way to do it? The user_uid argument plugin doesn't convert the title to username by accident, the OP's use case is the reason it exists. Don't use a screwdriver to hammer a nail. Especially when you have s a hammer in the other hand :) – Clive Jul 1 '16 at 9:31
  • 1
    @Clive I approached it from a non User ID way, IMO user/1 looks ugly, I like using path auto so it's user/admin, hence your answer is better if people want to use UID, but if they don't, you're answer is no good for them. – No Sssweat Jul 1 '16 at 9:47

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.