Zenophebe Posted October 30, 2014 Share Posted October 30, 2014 I've started to put together a system with PW, and I have a problem which has stumped me. I'm in need of a field which can perform logic. I have a 'content' type of Products with all necessary fields, and a list of parcel delivery companies. Depending on the size and weight of the product, certain delivery companies will not deliver the product, and delivery companies offer different insurance amounts with different prices. So, for each product, I'd like to list the delivery companies that will handle the product based on size and weight and then order by their price, showing the company name, the price and whether their insurance amount will cover the cost of the product. I'm thinking that stripping my needs to the lowest common denominator, it'd be some sort of field that I can specify code in to filter the results, and specify how the order of the content is shown. (If PW had content types, I'd also specify what content types the code applied to for example) So, how can I go about solving my problem? Thanks Link to comment Share on other sites More sharing options...
owzim Posted October 30, 2014 Share Posted October 30, 2014 I don't understand why you want to put the output in one field and not rather just put you logic in the themplate file. Anyway, you can define fields via hook properties (code not tested): // add a hook in you init or any other file that is included by template files $pages->addHookProperty("Page::deliveryCompanies", null, function($event) { $page = $event->object; // limit to the template 'product' if ($page->template->name === 'product') { $out = '<ul>'; // find the companies that deliver with a selector (just an example, pseudo code if you will) $companies = $pages->find("company.someField={$page->someOtherField}"); foreach ($companies as $company) { $out. = "<li>{$company->name}</li>"; } $out .= '</ul>'; // return the list $event->return = $out; } }); // echo out the list in your template files echo $page->deliveryCompanies; read more on hooks here: https://processwire.com/api/hooks/ Edit: might have misunderstood the question completely Oo Link to comment Share on other sites More sharing options...
Philipp Posted October 30, 2014 Share Posted October 30, 2014 Shouldn't this be possible with the Page field using a customer selector? Beside choosing a parent, you canenter your own PHP code to select pages that are shown. The only problem is, that the fields aren't saved when you might need to make the right selection. In this case you would need something custom that make the decision with a background AJAX call or something. I'm not sure how flexible this has to be, but you could also create different fields for each delivery method and only display them (Required-if option...). ___ I focus more on the background PW admin, owzim has a solution for the frontend. 1 Link to comment Share on other sites More sharing options...
Zenophebe Posted October 30, 2014 Author Share Posted October 30, 2014 I don't consider using the template file because the template file is for presentation, and has nothing to do with the logic. If I wanted to query using the API, what delivery companies could deliver for a product and I used logic inside the template, I wouldn't be able to do this. Link to comment Share on other sites More sharing options...
owzim Posted October 30, 2014 Share Posted October 30, 2014 Just to summarize what I got from you question: On your PageEdit page in admin you have a couple of fields, and you want to show a list that automatically updates, when you change values of the fields? Is that list only for visual assistance or do also want to select one of the companies that are contained in the resulting list? Either way, as far as I know, that's not possible out of the box. I don't know of any Inputfield that handles such a thing, but it's definitely possible do code one. Link to comment Share on other sites More sharing options...
Zenophebe Posted October 30, 2014 Author Share Posted October 30, 2014 Yes, that's correct! Hence my question about a field that would allow such logic. Link to comment Share on other sites More sharing options...
Philipp Posted October 30, 2014 Share Posted October 30, 2014 While it is called t emplate file, I think it can contain some kind of logic. Some people here are using a MVC approach, where the template.php fetches and processes data, then compiles it into a template and renders the view. But I kind of like the idea of a instant/live changing of possible input selections rather than pre-filtering them after page load. 1 Link to comment Share on other sites More sharing options...
Joss Posted October 30, 2014 Share Posted October 30, 2014 Is this selection for use in the backend, in the admin, or is it used in the front end? If it is the front end, then the place for the logic is in the template file - that is what it is there for. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 31, 2014 Share Posted October 31, 2014 I think he's talking about the backend. The problem with the backend is, that ProcessWire does not autosave stuff, so your fields are only evaluated once you hit save. Your options would be either to implement some kind of autosave, than you can use PageFields which can show different pages (parcels or other stuff) depending on your other fields.The other option would be to implement your logic in a pure javascript/jquery fashion. If you want to try this, have a look at the AdminCustomFiles module. Link to comment Share on other sites More sharing options...
Joss Posted October 31, 2014 Share Posted October 31, 2014 There is an autosave module kicking around that Soma did. Never tried it in combination with dependancies - http://processwire.com/api/selectors/inputfield-dependencies/ 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