Jump to content

Special form when creating new page


Sipho
 Share

Recommended Posts

 

I have an issue where I want different formats for creating one page. When you are creating a new page I want it to offer different types of fields from when you are editing it.
For example, I have a field called countries which is a multiple page reference field. This is desirable as it is in a very easy to edit format. Currently, I am using selectize.js which makes it possible to search for the pages and add them in a tag fashion. This is how I want it to be when a page is already created. However, when creating a new page it is often easier to just paste a list of all the countries as text. This is because the data is coming from an old website where the countries are written like this:

Quote

Nigeria, Kenya, Australia, Russia, France

I can code something which converts this text into the multiple page reference fields but I am not sure how to go about it. At first I tried making another template which had fields that were in this “quick” format. Then I planned to make it such that when you add one of these quick pages it creates a page in the standard format and deletes itself. This has it’s own problems such as where to place the new page, what title and name to give it, whether to have just one quick page or multiple and when to delete it. It just didn’t seem right. 
Another possibility would be to show special quick fields when creating a new standard page but hide them when the page is saved and show the standard fields. I am not to sure how to achieve this though. 
Does anyone know a better way of going about this? I feel like this is a simple problem which already has an elegant solution.

 

Link to comment
Share on other sites

14 hours ago, Sipho said:

However, when creating a new page it is often easier to just paste a list of all the countries as text.

Who is creating the pages? You (the developer), or a client?

If you are creating the pages then I think it would be better to use the API or a bulk import module (ImportPagesCSV, BatchChildEditor) to bring this data in initially rather than spend time making something special in Page Edit.

API

$countries_string = 'Nigeria, Kenya, Australia, Russia, France';
// Maybe you would instead create an array of country strings to process multiple pages at once
// 'Page One Title' => 'Nigeria, Kenya, Australia, Russia, France',
// 'Page Two Title' => 'Russia, ...'
// ...

$country_titles = explode(', ', $countries_string);
$country_pages = $pages->find([
    'template' => 'country',
    'title' => $country_titles,
]);
// Set $country_pages to the Page Reference field

 

ImportPagesCSV

Use a text editor to replace instances of ', ' with '|' in your countries string. Now the country titles are in a format that can be imported to a Page Reference field by ImportPagesCSV (see the module readme).

You could import all the page content via the module, or just the countries field if you prefer (create a CSV that contains two columns - the page title and the pipe-separated string of country titles).

 

  • Like 2
Link to comment
Share on other sites

9 hours ago, Robin S said:

Who is creating the pages? You (the developer), or a client?

@Robin S The client would be creating the pages. Initially, I will be migrating the pages from the old website and I shall use one of the techniques you mentioned. But it is still often easier to enter the data in a different format from editing it. Another example is names. It is more intuitive to input names in this format:

Quote

John Smith, Amy Pond, Rory Williams, James Bond

But it is easier to manage and search the data using ProcessWire when it is in a format such as a repeater with a first name and last name field. I was going to try and get this special form going like I described and have the API interact with that because it would be easier. But I think I will do it the way you described instead. I am still curious as to how one would go about this though for the client.

Link to comment
Share on other sites

53 minutes ago, Sipho said:

But it is still often easier to enter the data in a different format from editing it.

To me the main distinction is between the bulk importing of data (for which modules like ImportPagesCSV are great) and the occasional addition or editing of data which you do via Page Edit.

But in any case it's not difficult to do what you are asking about. Add a textarea field in your template named "country_import" or something. Then use a hook to Pages::saveReady() to process the field contents and add them to the "countries" Page Reference field. In /site/ready.php:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    // Only for the appropriate template
    if($page->template == 'YOUR-TEMPLATE') {
        // If the import field is not empty
        if($page->country_import) {
            // Get the individual country titles in an array
            $country_titles = explode(', ', $page->textarea_1);
            // Find the country pages with those titles
            $country_pages = $this->pages->find([
                'template' => 'country',
                'title' => $country_titles,
            ]);
            // Add the country pages to the countries field
            $page->countries->add($country_pages);
            // Empty the import field
            $page->country_import = '';
        }
    }
});

You could use the same principle for other fields if you think there's a major benefit to it.

  • Like 3
Link to comment
Share on other sites

55 minutes ago, Sipho said:

I want to only show my special field when the page is being added for the first time but not afterwards.

You can show the field when the countries field is empty using the core inputfield dependencies feature:

countries.count=0

Or you could show it when the page is unpublished (which would apply when the page is first added) with the help of my Custom Inputfield Dependencies module.

  • 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

×
×
  • Create New...