heldercervantes Posted January 13, 2017 Share Posted January 13, 2017 Hi forum, I'm implementing a site where a files field needs to be language-sensitive. I don't mean descriptions, but the files themselves need to be different according to language. I could have a field for each language, but that would mean having to add new fields if we add new languages later on. So my attempt was to use the description as a filter. If there is one, show the file, if not ignore it. So a file that has a description only for english would only show when viewing english. But the description defaults to the main language. Is there a way to get the actual description on a given language without defaulting when it's empty? Or some other approach that's minimally elegant for this problem. Thanks, H Link to comment Share on other sites More sharing options...
adrian Posted January 13, 2017 Share Posted January 13, 2017 This is the approach I use for ML file fields: https://processwire.com/api/multi-language-support/multi-language-fields/#language-alternate-field-values Link to comment Share on other sites More sharing options...
heldercervantes Posted January 13, 2017 Author Share Posted January 13, 2017 Thx adrian, but this doesn't seem to work for file fields. Even with outputformatting set to false I always get the default language. Link to comment Share on other sites More sharing options...
adrian Posted January 13, 2017 Share Posted January 13, 2017 Just now, heldercervantes said: Thx adrian, but this doesn't seem to work for file fields. Even with outputformatting set to false I always get the default language. Works great for file fields for me - I use it for different language versions of the same PDF report. I don't think you want outputformatting off though - you'd want it on to get the localized version of the file and its description. Maybe someone with more ML experience might have an idea why it's not working for you? Link to comment Share on other sites More sharing options...
heldercervantes Posted January 13, 2017 Author Share Posted January 13, 2017 I think I misinterpreted you. Your approach is to add a file field for each language? Link to comment Share on other sites More sharing options...
adrian Posted January 13, 2017 Share Posted January 13, 2017 Just now, heldercervantes said: I think I misinterpreted you. Your approach is to add a file field for each language? Correct - that is how those language alternate fields work. Just add the name of the language as a suffix to the base field name and it should all be taken care of automatically. Maybe not the cleanest solution because you do need a new field for each language, but other than that it works seamlessly in my experience. Link to comment Share on other sites More sharing options...
heldercervantes Posted January 14, 2017 Author Share Posted January 14, 2017 Found a pretty cool solution for this. Repeater + Page + Files. So I set up a repeater called langFiles. Inside that repeater I added a Page selector called lang and the files field. The page selector is set to be a single select, by template = language. So in the admin I get a repeater, add an item, select the language and upload the files. Add another one for the second language, and so on. Then on the frontend: // Somehow $page->langFiles->find('lang='.$user->language) doesn't work foreach ($page->langFiles as $lf) { if ($lf->lang == $user->language) { $filesRepeaterBlock = $lf; break; } } if ($filesRepeaterBlock->files->count) { echo '<ul>'; foreach ($filesRepeaterBlock->files as $file) { echo '<li><a href="'.$file->url.'" target="_blank">'.$file->name.'</a></li>'; } echo '</ul>'; } And there it is. Add a new language tomorrow, and it adapts. And it's not too weird for the admin. 2 Link to comment Share on other sites More sharing options...
adrian Posted January 14, 2017 Share Posted January 14, 2017 Nice one! 25 minutes ago, heldercervantes said: // Somehow $page->langFiles->find('lang='.$user->language) doesn't work Try this: $page->langFiles->find('lang.name='.$user->language) Link to comment Share on other sites More sharing options...
heldercervantes Posted January 14, 2017 Author Share Posted January 14, 2017 Already did, no cigar. If I get the first langFiles->lang, it returns 1010, same as outputting $user->language. Comparing those in an IF statement works, but the find returns nothing. I tried: find('lang.name='.$user->language->name); find('lang='.$user->language->id); find('lang.id='.$user->language->id); Nothing works. I even tried adding a title field and fake a string and still get nothing. Something's preventing that from working. But that foreach solves the problem and in this small site performance won't be an issue so I give up streamlining more. Link to comment Share on other sites More sharing options...
adrian Posted January 14, 2017 Share Posted January 14, 2017 Yes of course - sorry, you can't actually "find" a specific repeater item. However one thing you might prefer is rather than using a Page field for the language, just use the built in "language" field. Link to comment Share on other sites More sharing options...
Robin S Posted January 15, 2017 Share Posted January 15, 2017 8 hours ago, adrian said: Yes of course - sorry, you can't actually "find" a specific repeater item. Maybe this issue is specific to language pages when used in a Page field, because I can successfully find repeater items with a Page field matching a given id, name, title, etc. Link to comment Share on other sites More sharing options...
adrian Posted January 15, 2017 Share Posted January 15, 2017 2 minutes ago, Robin S said: Maybe this issue is specific to language pages when used in a Page field, because I can successfully find repeater items with a Page field matching a given id, name, title, etc. Can you find the actual repeater item, or just the page that has a repeater item that matches the find? Link to comment Share on other sites More sharing options...
Robin S Posted January 15, 2017 Share Posted January 15, 2017 15 minutes ago, adrian said: Can you find the actual repeater item, or just the page that has a repeater item that matches the find? The actual repeater item. I also installed Language Support and set up a test Page field with template=language and that works too. $results = $page->test_repeater->find("test_page.name=german"); echo $results->each("<p>{name}</p>"); // echoes the repeater item name, e.g. 1484443861-212-1 Edit: fixed the above 1 Link to comment Share on other sites More sharing options...
adrian Posted January 15, 2017 Share Posted January 15, 2017 Just trying this here: d($page->repeatertest->find("language=".$user->language)->each("author_name")); In this case I am using the system "language" field, rather than a page field and I am reporting the author_name which is another field within the repeater. It actually seems ok here 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now