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?

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

4
  • 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.
    – apaderno
    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.
    – apaderno
    Jul 2 '16 at 11:35
5
  1. Make Fields the format used by the view

enter image description here

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

  1. In Relationship, add Content author

    • Checkmark ☑ Require relationship
  2. In Fields, add Name

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

enter image description here

You could put it as 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

15
  • 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
  • 2
    No need to delete this! The approach is still technically valid, it could help someone out. I'm sorry if you feel like I've pressured you to get rid of this, that wasn't my intention. I just wanted to clarify the points, I spent about 2 hours looking into this last night so I'm very familiar with how this part of Views works now
    – Clive
    Jul 1 '16 at 10:00

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.