0

I am quite novice to Drupal. I am trying to figure out how to create views listing files such that only those files are listed where filename contains currently logged username?

3
  • So you have a username like johnsmith and you have filenames that contain that username, like somethingjohnsmith.jpg, somethingelsejohnsmith.jpg? Commented Sep 8, 2013 at 12:07
  • exactly!!! that's what I have. And what I want is to throw into value slot of the views filter smth like [current-user:name] as suggested by NineAllexis. I have enabled PathAuto, Token, Token Filter and whatnot and still it does not work.
    – ein_io
    Commented Sep 8, 2013 at 15:58
  • See my answer. PathAuto has to do with aliases and Token Filter has to do with Text format filters, as in viewing a node and replacing tokens with some data, so they won't help you here. Commented Sep 8, 2013 at 17:52

2 Answers 2

2

If you could use tokens in views filters you could do something like File: name ... contains any word ... [user:name] and that would end up in the sql as file_managed.filename LIKE '%username%' which would be cool. But you can't do that, to my knowledge.

You might achieve what you want using contextual filters having PHP code in both contextual filter and validator, maybe.

That said, depending on how much files and users you have and so on, it might be easier to tag the files with taxonomy terms. That way user wanting to change their username, eg. when they get married, won't brake your system. Also usernames having spaces, underscores, apostrophies etc. wouldn't be an issue.

Edit:

What do you know, this can be done with contextual filters! I don't know how practical or robust or performant this is but here goes:

1) Add a contextual filter of Global: null.
2) Provide some default value, doesn't matter what. For example Fixed value -> 'blaa'.
3) Specify validation criteria -> PHP code

global $user;

$view->query->where = array(
    1 => array(
        'conditions' => array(
            0 => array(
                'field' => 'file_managed.filename',
                'value' => '%' . $user->name . '%',
                'operator' => 'LIKE',
            )
        ),
        'args' => array(),
        'type' => 'AND',
    )
);
return true;

I figured this out with dpm($view). If you have other filters in the Filter criteria, this code will have to take that into account. Tampering with the $view like this is not really future proof, but theoretically possible.

3
  • Thanks, Kari! I kind of figured a partial solution. You could go to contextual filters and select "use default value" if the search term is not present in the url. Then select the PHP is the type of text to define the value. Then insert the following code into the value field: <?php global $user; print $user->name; ?> Yet it will only allow to select files with complete match to user name. Unfortunately partial matches are not implemented in contextual filters. Id be curious if it was possible to supplement partial matching capability to this algorithm.
    – ein_io
    Commented Sep 8, 2013 at 19:18
  • Great, I continued from there, see my edited answer. Commented Sep 8, 2013 at 21:56
  • You are a genius! It works!!!
    – ein_io
    Commented Sep 9, 2013 at 7:35
0

You can get current logged username by using token [current-user:name] or just [user:name], provided you have Pathauto installed.

3
  • Thanks! So should I just type 'current-user:name' into the Value field of the filter?
    – ein_io
    Commented Sep 8, 2013 at 8:56
  • either that or you could get the data from global $user variables. $user->name if i'm not wrong... Oops... somebody got it already hehe Commented Sep 9, 2013 at 8:33
  • thanks :) NineAllexis. See Kari's comments above, he definitely solved it through PHP code in validator – works even my very rookie hands.
    – ein_io
    Commented Sep 9, 2013 at 11:54

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.