Jump to content

Using "Page" fieldtype for select dropdown


Robin S
 Share

Recommended Posts

I'm new to ProcessWire and trying to get my head around the recommended method for creating <select> controls in the page editor.

A couple of questions...

1. I'm following the advice here and I think I have everything set up properly for my "bibiography_type" select control.

However, when I attempt to output the "bibiography_type" field in my template I get the page ID of the select option rather than the page title or the "select_value" text. Im using:

echo "<p>{$page->bibliography_type}</p>";

...which outputs "1024". I tried...

echo "<p>{$page->bibliography_type->title}</p>";

and...

echo "<p>{$page->bibliography_type->select_value}</p>";

...but these don't output anything.

How do I get the "title" or "select_value" of the selected option "page"?

2. I understand how the ProcessWire method of creating select controls allows for a lot of flexibility (the ability to edit select options in the future, etc). But I expect this means the data that is stored in the database for the select control is something like the page ID of the selected option rather than the text for the selected option. If I am importing new pages from a CSV, do I need to translate the text for my "Bibliography Type" field into a corresponding page ID? So for instance if the "Bibliography Type" for page I am importing is "Book", do I need to replace this with "1024"? Are there any other requirements for importing pages containing data that will go into a select control in ProcessWire?

Link to comment
Share on other sites

Hi @Cerulean, and welcome!

I had the same trouble recently. The number string is good, it means you're nearly successful! 

I don't know very much (especially compared to others on this forum) but I will answer you best i know how...

Start here on another thread to follow how I converted the number string to the correct display. The solution that worked for me was to call the pages before trying to output them. 

Here is some untested code for you to try:

$bib = $pages->find("parent=/enter/path/to/your/bibliography/type/pages/here");
// calling the bibliography type pages
// the path will be short, something like /tools/bibliography_type <- find it in your page tree

echo "<p>{$bib->title}</p>";
 

More experienced users, please correct my suggestion if it is bad!

  • Like 1
Link to comment
Share on other sites

Thanks for the replies.

Issue 1 is now solved - I tried playing around with some of the options for the field I'm using as my select and setting "Deference in API as" to "Single page (Page) or empty page (NullPage) when none selected" allowed me to use $page->bibliography_type->title

This setting isn't mentioned in the Wiki article (whoops, it is :-[ ), and I think it would be useful to have some documentation for the Page fieldtype in the API seeing as the ProcessWire method for creating common controls like select and select multiple is a bit unorthodox (although powerful).

Any advice for issue 2? I'm going to need to import a large number of pages from CSV (using the ImportPagesCSV module I expect) and if that's going to be problematic to do in conjuction with using the Page fieldtype I had better rethink how I set up the fields.

  • Like 1
Link to comment
Share on other sites

You could use the wonderful Page Select Field Creator. In that module there is a textarea that takes CSV text.

I think you may have misunderstood what I mean. That module would be useful for creating the pages for the select field options, but what I'm asking about is importing pages of content where some of the content fields I want to be editable in the admin using select dropdowns.

Take my bibliography_type field, which will have select options like "Book", "Journal Article", "Conference Paper", etc. When that field is set up in ProcessWire using the Page fieldtype those options will be mapped to page IDs (I think) and it is those page IDs that get saved to the database (I think). In my CSV of pages the "Bibliography Type" column will contain the values as text, but I suspect PW is not going to want them in that format. Do I need to find the page IDs for each select option and replace the text in my CSV with the page ID equivalents? Or can I just import my Bibliography Type values as text?

Link to comment
Share on other sites

Hi,

Could you show us a small extract of the data you are trying to import with the CSV so that we might better understand the structure? When I want to import data from a CSV, I usually create a page with a special template (e.g. "file-import" template), which has a "File" field, and when viewing the page, I process the file.

In your case, you would want to create a template called "bibliography-type" with simply a title field (no need to create a php template file for this). Somewhere in your page structure, you can create a section called "Bibliography types" as follows:

Home

-- Bibliography Types

---- Book

---- Journal Article

---- Conference Paper

---- Etc.

You would then create a Page field called "bibliography_type", which is restricted to the "bibliography-type" template (you can set this in the "Input" tab, in the "Selectable Pages" section when creating the field). Finally, you would assign this field to another template (e.g. "document", so that you can assign a bibliography type to the document. When creating a document in the administration, you would then be able to select a bibliography type for your document (just like any other field value).

If you want to show the bibliography type in the front-end when viewing a document, you can use the code mentioned previously:

echo "<p>{$page->bibliography_type->title}</p>";

To give you more information on how to import the data, I would need to better understand the structure of the CSV.

Link to comment
Share on other sites

Hi Cerulean. Welcome to PW and the forums. Natively, the ImportPagesCSV module does not support importing Page Fields. However, with some minor changes to the code, you can make it do just that. See this post for instructions:  https://processwire.com/talk/topic/383-module-import-pages-from-csv-file/?p=21476.

Please note that any changes you make to modules will not survive an upgrade of that module. However, there are various ways of getting around that, normally duplicating the module code and renaming it. Recently, in the dev version of PW, there's a way to have two modules of the same kind co-exist. 

  • Like 2
Link to comment
Share on other sites

Thanks for the replies.

Using phpMyAdmin I checked what is saved to the database for my Page field and it is indeed a page ID number. So that answers my question about how to prepare my CSV for import: text values for fields that will be managed with a select dropdown in PW (using the Page fieldtype) will need be translated into their page ID equivalents. Naturally this means that all select options need to be set up in advance so their page IDs can be found.

Natively, the ImportPagesCSV module does not support importing Page Fields. However, with some minor changes to the code, you can make it do just that.

Modifying the ImportPagesCSV module sounds pretty straightforward and that's probably the way I'll go. The bulk import of pages will only happen during the initial site development so the issue of the module changes not surving a module update won't be a big problem.

When I want to import data from a CSV, I usually create a page with a special template (e.g. "file-import" template), which has a "File" field, and when viewing the page, I process the file.

That sounds like you use the PW API for CSV importing rather than a module like ImportPagesCSV. I'll start with the module approach but if I have problems with that I can look at using the API for the CSV import.

Thanks again for your help.

Link to comment
Share on other sites

Modifying the ImportPagesCSV module sounds pretty straightforward and that's probably the way I'll go. The bulk import of pages will only happen during the initial site development so the issue of the module changes not surving a module update won't be a big problem.

Looks like Ryan is already onto this - maybe you should hold off for a day or two: https://github.com/ryancramerdesign/ProcessWire/commit/bd16deaf8f9e690fc47e91766e479550042eea5d

Maybe also keep an eye on CSV import module (http://modules.processwire.com/modules/import-pages-csv/) to see when that gets updated, or maybe it will work already with those core mods in that recent commit - not sure!

  • Like 1
Link to comment
Share on other sites

I hope you will not manually be checking and copying the page IDs of your select options!

You know, I probably would have because my site only needs a single select dropdown containing half a dozen options. :rolleyes:

But good point - I should get in the habit of using the power of the API whenever I can.

Link to comment
Share on other sites

One last attempt to suggest using the api, then I'm gone :)
 
Instead of changing your source data, you can let the API create the pages for you when needed. So for a (simplified) table like this:

Bibliography Type      Document Title
-----------------      ----------------------
Book                   Interesting Book 1
Book                   Interesting Book 2
Magazine               Fascinating Magazine 1
....

you could use the following code, which automatically creates the pages for you

function getBibliographyType($name) {
    $template = 'bibliography-type';
    $parent = $pages->get('/bibliography-types/');

    static $bibliographyTypes = array();
    
    if (isset($bibliographyTypes[$name])) {
        return $bibliographyTypes[$name];
    } else {
        // Create the bibliography-type page
        $p = new Page();
        $p->template = $template;
        $p->parent = $parent;
        $p->name = wire('sanitizer')->pageName($name);
        $p->title = wire('sanitizer')->text($name);
        $p->save();
        
        // Save bibliography type id for future calls, avoiding calls to the database
        $bibliographyTypes[$name] = $p;
        
        return $p;
    }
}

$f = $page->import_file->filename;
$csvDelimiter = ',';
$csvEnclosure = '"';

$templateName = 'Document';
$parent = $pages->get('/documents/');
while (($data = fgetcsv($f, 0, $csvDelimiter, $csvEnclosure)) !== false) {
    $bibliographyType = getBibliographyType($data[0]);
    $title = $data[1];
    
    $p = new Page();
    $p->template = $templateName;
    $p->parent = $parent;
    $p->name = $sanitizer->pageName($title);
    $p->title = $sanitizer->text($title);
    $p->bibliography_type = $bibliographyType;
    $p->save();
}
  • Like 4
  • Thanks 1
Link to comment
Share on other sites

you could use the following code, which automatically creates the pages for you

Nice! The dev branch commit that adrian linked to above will make importing Page fields easier because it will allow the title string to be used instead of the page ID, but your code for creating new select options is a real bonus. Thanks!

  • Like 1
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...