Jump to content

Creating a Simple Select [Please check for accuracy]


Joss
 Share

Recommended Posts

(can someone check this for accuracy and let me know any changes I should make - I am very new to PW)

I wanted to create a simple select drop-down, but there isn't one in Processwire by default. Reading the forums, it became clear this was for very good reasons and that you are expected to use pages.

But I couldn't find clear instructions how to do this. So, having worked out how, I wrote myself this little tutorial:

#####################

Processwire by default does not have a simple select field.

The reason, given by Ryan the developer, is that a plain simple select field is not related to the database, so you have, in effect, some of your data in a flat file - sort of - which is not good practice.

The preferred way of doing select fields is by using pages for the values and the Pages field to list them in your form. If you just want a simple on/off state, use a checkbox, but if you need more options, then this is the way.

Creating Pages for Select Fields

These pages must be published to be accessible by the pages field but are obviously not wanted on the website.

  1. First, create a dummy template (a tempalte with no associated file) called something like "selects." Give it two fields - the normal title field and a text field called select_value
  2. Create a Top Level (child of Home) page called Selects (or something friendly). Give it a blank dummy template. Make this page hidden.
  3. Create a child page and call it something that will work for this group of selects - perhaps call it "Yes No." Again, give it a blank dummy template
  4. Create three children under this - one called Yes, another called Maybe and a third called No. Use the dummy template "selects" that you created earlier.
  5. In the select_value field put a value - in this case the most obvious would be yes, maybe and no! For ease of use, make sure these values have no spaces or anything clever.

These three pages will be your select values.

How to add to a template

  1. Create a field using the Pages field type. Call it something like "yes_no_maybe_selector"
  2. Under the details tab, select the single/null option.
  3. Under the Input tab, select the Yes No page as the parent. This will then display your three children in the select.
  4. Choose either Radio or Select.

Add this new field to your template in the usual way.

How to output into your template.php

The field is called in the usual way, remembering to call the particular value from the select page. So:

$page->yes_no_maybe_selector->select_value;

The chances are that you are going to use this as a condition - if "yes" do something, if maybe do something else and if "no" do something else again.

IMPORTANT: You cannot set a default state - so you will always have another option - Null. So, if you are only wanting to do a two way selection, then you are probably better off just doing a simple checkbox. In this example, therefore, we will effecting have Yes, Maybe, No and Null - that is no, twice! Never mind....

First, lets add it to a variable:

$yesno = $page->yes_no_maybe_selector->select_value;

Now, we can set up our variations.

if ($yesno === "yes") {
echo "Say Yes";
} elseif ($yesno === "maybe") {
echo "Shrug your shoulders";
} elseif ($yesno === "no") {
echo "not a chance";
} else {
echo "nada";
}

Note that the last option is effectively the null option, so something is being outputted, even if it just some blank space which saves potential errors or silly bits of markup (well, in my case that seems to happen!)

And that is it!

Joss

  • Like 8
Link to comment
Share on other sites

You don't even need the two fields on the template. Since you only need one information, the title is enough. So, in your example, the title would hold the the "yes", "maybe" and "no" values.

To output do this instead:

$page->yes_no_maybe_selector->title;

or

$page->yes_no_maybe_selector->name;
  • Like 2
Link to comment
Share on other sites

Hi Diogo

I did it your way on something earlier, but then I realised that a client might want labels on the select that said something more complicated, or even had punctuation in it.

For instance: "Yes, I want it now" and "No, don't send me anything!"

So, this way it allows a difference between the label and the value.

But you are right - on my example above it probably isn't necessary.

It is me just covering my butt for the person who says that their value "My $%!*ing cat's got an @ in his email address {isn;t that nice}?" broke their template!

By the way, the "null" issue is a bit of a nuisance - it would be nice to be able to add either a no-value option "Select one" or default onto one of the page options. At the moment the field is just a blank, which is a bit sad looking :(

Joss

Link to comment
Share on other sites

Thanks for writing this. There's many ways and different use cases, and that's hard to write everything down to not get to lengthy.

Some thoughts. This technique with pages and a page field or without can be used for various things.

You can build such helper pages to build a select on the frontend only and render the options in a foreach. Doing it this way you benefit from being able to add and move options in the admin page tree, and allow the editor to even edit or add to them. And all stays in the same manner as all other pages. I can tell my clients "well there's the pages that make these options and they know how already". Being everything accessible through API so easy, would make it easy to even build some little tool in the backend to manage those options outside the page tree, to go even further.

Also those page references are great for doing categories, tagging, ingredients, .. whatever. And later those pages can be even used in your site and make them accessible and give them a template. There you then show all pages that have a reference to the currently viewed category. I've for example created a whole shop where the navigation are the categories and articles are just linked to some category and display when browsing the category tree. And best of all it stays flexible and can be adapted or expanded in no time with no limits.

One could write down a whole book solely on this subject as it's endless, but guess nobody would buy it. ;) Creativity is what I enjoy in PW.

One thing about selects having a empty option. This is simply to be able to "deselect" it in the admin. So if you use page field select for doing something on the frontend you would use usually check for if not empty to determine if anything at all. So your example could aswell be like this:

if ($yesno) { // anything selected?
 if($yesno == "yes") {
   echo "Say Yes";
 } elseif ($yesno == "maybe") {
  echo "Shrug your shoulders";
 } elseif ($yesno == "no") {
  echo "not a chance";
 }
}

No need to write === here, == is sufficient I think when comparing strings. You do triple === usually on comparing objects.

  • Like 4
Link to comment
Share on other sites

  • 1 year later...

Hi everyone - I'm completely new to processwire, but really loving it from the get-go.

I must say I was a little put off initially that there is no facility for plain old select fields. On reading this post by Joss (thanks!) I realised that what I wanted to do to begin with was probably much better served by using his method. It must be something along the lines of what Soma is describing above, although - to be honest - it's been too long a day for me to fully follow. 

I have a template which features a sidebar and I also have a bunch of blocks of code  presenting bits of content gathered from throughout the site ("latest news", "latest projects", etc.). The code blocks are n the form of .inc files in the templates folder and wanted to be able to select which ones would appear on each page. Joss gave me a great way of implementing just that. I followed Joss's instructions to the letter, except I set my field to be of multi-select page type.

In the select_value field of each of the select option pages, I put the names of my .inc 'slot/block' files, e.g. 'sidebar-news.inc'. I end edup with the following 'structure':

Select Fields [page containing all the select field structures]

|

|---- Sidebar Slots [top of my "sidebar slots select values field structure, a field which I named 'sidebar_slots' in my site template ]

        |

        |---- News Slot [ select_value= 'sidebar-news.inc' ]

        |

        |---- Latest Projects Slot [ select_value= 'sidebar-projects.inc' ]

etc., etc.

In my template (which of course contains the sidebar_slots field), in the column I have reserved for my slots, I use the following code:

<div class="sidebar">
<?php foreach($page->sidebar_slots as $slot)
{
	include("./" . $slot->select_value);
} ?>
</div>

I then go in my page and select which slots I want to be rendered in it.

I can't say I'm thrilled by having my "Select Fields" structure appear on the same tree as my Site Pages. I am building a fairly small site here, which I am going to be managing, but it is easy to see how "pages=select values" can get confusing for some. It almost feels like "pages" is an arbitrary name for what are essentially entities but, semantically I guess you need to start from somewhere more grounded than "entities", in the context of a cms.

On the other hand, I am so grateful for this wonderful piece of software - much, much of my cms wishlist finally come true! - that I already feel guilty for nagging.

Bless you all

Leonidas

Link to comment
Share on other sites

Hi lkossis

I tend to create a page in my tree with a unique template that has no file and call it content-management or something like that.

Then under there I put all the stuff like selects, tags (sometimes) and so on.

I then make that content-management template not accessible to some roles.

That way everything is in one place and only I can mess it up!

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...