1

Contextual is set up to display all content from a user for a specific content type.
www.example.com/users/realname/gallery where realname is contextual filter.
Realname is used with token that reflects field from registration: field-choose-realname.

Lets say we have 2 users and 1st writes "One" as realname and 2nd writes "One Two".
Realname contextual filter is setup to use lowercase and change spaces to dash.

Here is what happens:
www.example.com/users/one/gallery - ok

www.example.com/users/one-two/gallery - page not found error
www.example.com/users/one%20two/gallery - ok

For Contextual "when the filter is in the URL or a default is provided" i am using this php code (i did not write it so i have no clue what it does really):

if (!isset($argument)) {
  return FALSE;
}
$query = db_query('SELECT uid, realname FROM {realname} WHERE realname = :realname', array(':realname' => $argument));
$row = $query->fetchAssoc();
if (isset($row['uid'])) {
  return TRUE;
}
return FALSE;

To better explain what i want to do:
Get sidebar menu like on myspace for bands. When you land on bands page you can use sidebar menu to check their foto galleries, band members, video gallery and links must be made with a field that is not default username (it can be realname module or some other text field). Field validation is used so i do not get same name twice, and all links in pathauto are made example.com/bands/realname.
To repeat again it does work if realname contains 1 word.

1 Answer 1

2
+50

Setting the contextual filter to transform spaces to dashes will not make the Realname field in the database contain dashes, so the filter fails. In your url you look for one-two, in the database this is One Two they are not the same and it doesn't work (one-two ≠ One Two).

First make sure pathauto is installed and enabled. It will provide your users with a pretty url. This will make sure every user has a unique path. If you replace spaces and dashes in the URL as you do now, the contextual filter for these users One Two, One-Two, One'Two will all become one-two even if they are if fact different users. So your results will be mixed up. Pathauto will make sure they become unique; one-two, one-two2 and so on.

Do not rely on simple pattern matching, but use the uid from the instead. Luckily we can hide this entirely from the url by using a View's contextual filter.

Update

It seems I have missed a module requirement; Sub-pathauto (Sub-path URL Aliases).

The Drupal Path module matches only full URLs when creating SEO-friendly aliases. This module extends that behavior by also matching known sub-paths and replacing them with their respective alias.

For example, if user/1 is aliased to users/admin, this module rewrites the link to the user contact page user/1/contact to use the aliased URL users/admin/contact instead.

The is required to properly obtain the UID from the url.

Then create your view as follows: View

Use must use the unaliased path for the path. So if your user is at /users/url-safe-realname and your gallery at /users/url-safe-realname/gallery, you enter /user/%/gallery (omit the s in users pathauto adds if using default settings).

For the contextual filter, in the WHEN THE FILTER VALUE IS IN THE URL OR A DEFAULT IS PROVIDED section, check Specify validation criteria, choose user and numeric UIDs.

The result will be a page that has the url-safe realname in the path and shows only content created by the user:

Result

16
  • User id is 153453 or something like that and Realname is profile name which is "One Two". I want nice and clean looking urls thats why i want to use Realname as contextual filter and not uid.
    – Azz Kawa
    Commented May 19, 2016 at 18:58
  • You can keep realname in the url and still use the uid for the contextual filter. Just try it. Commented May 19, 2016 at 18:59
  • I tried it, but got same results. I have updated question, maybe to clear things up.
    – Azz Kawa
    Commented May 20, 2016 at 6:55
  • Updated the answer. Commented May 20, 2016 at 9:56
  • Thanks Neograph. Pathauto is installed, unique link is generated for each user using field field-choose-realname. Realname module uses this field. In views i have setup everything like you and i get ok results if the field-choose-realname has 1 word. If it has 2 words it breaks up becouse of the dash in the link, and if i let it go undashed i get ok results but link looks really ugly. Conclusion is that view contextual is working ok since it get good results for 1 word link, and for the link where it uses %20 (space) instead of dash.
    – Azz Kawa
    Commented May 20, 2016 at 12:16

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.