Jump to content

How would I have a field with logic?


Zenophebe
 Share

Recommended Posts

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...