Jump to content

Best way to import fields & templates


apeisa
 Share

Recommended Posts

I don't know if there is already a way to do this.. but what I need is some way to import fields & templates to new site. I try to explain:

Let's say I create template "news" and it has fields:

  • Title
  • Datetime
  • Summary
  • Body
  • Author

I have created few template-snippets that like "Latest news" and "All news" etc. This is all fine and easy. But after this next client comes up with similar request: "we also want to show news on our site." (I say that 95% of our current clients have news on their site.)

How can I easily give our next client these same fields & templates? Process could be import from other site or anything. Or should I create module "News" that creates needed fields & templates?

And this should be easy enough, since after news we need have some formal templates for Events and maybe Forum etc... This all may sound like it is fighting "against" flexibility of ProcessWire, but I don't think it like that. After client asks "This is allmost what we need, but our News will require also Attachments and Place fields." it is trivial to extend their News-template, but leave others as is. Just some conventional ways of doing things, that we can write help docs and all people on our organization can help clients etc.

Link to comment
Share on other sites

I think we can automate the field creation part relatively easily. My plan was to add an Export/Import option to Admin > Setup > Fields, and it would basically export a JSON profile of all the fields you selected. Then you could paste that profile into another site into the "import" textarea, and it would create all the fields. The same thing could probably be done at the template level too.

The idea you mentioned of a module is also a good one, though I'd probably be inclined to do the export/import described above instead. But here's how you'd go about doing the module if you wanted to. The module would exists to add the fields and templates needed for news. When you click "install" on that module, it would create the fields and templates automatically just with API calls. For example, here's how your module code might do it in it's ___install() method:

<?php

public function ___install() {

    $summary = new Field(); 
    $summary->type = $this->modules->get("FieldtypeTextarea"); 
    $summary->rows = 3; 
    $summary->name = "summary";
    $summary->label = "Summarize this news item in 1 paragraph";
    $summary->save();

    $datetime = new Field();
    $datetime->type = $this->modules->get("FieldtypeDatetime"); 
    $datetime->name = "datetime";
    $datetime->label = "Enter a date";
    $datetime->dateOutputFormat = "m/d/y";
    $datetime->dateInputFormat = "m/d/y";
    $datetime->defaultToday = 1; 
    $datetime->save();

    $fieldgroup = new Fieldgroup();
    $fieldgroup->name = "news";
    $fieldgroup->add($this->fields->get("title")); 
    $fieldgroup->add($summary); 
    $fieldgroup->add($datetime); 
    $fieldgroup->save();

    // copy your default news.php template to /site/templates/, assuming your server will let you... 
    copy($this->config->paths->YourModuleName . "news.php", $this->config->paths->templates . "news.php"); 

    $template = new Template();  
    $template->name = "news";
    $template->fieldgroup = $fieldgroup;
    $template->save();
}

Because we don't have the documentation complete to the level of detail of individual fieldtypes, you'd currently have to look at what was configurable with each field. The simplest way to do this is probably to view the source when editing that field in the Fields editor, and looking at what configuration options are available under the "Fieldtype Settings" and "Inputfield Settings". Or just ask me.

You might have your module add a hook or two, like PageArray::renderNewsItems() or something like that, if it was helpful to do so.

<?php

public function init() {
    $this->addHook("PageArray::renderNewsItems", $this, "renderNewsItems"); 
}

public function rnederNewsItems(HookEvent $event) {
    $pageArray = $event->object;
    $out = "\n<ul class='nav'>";
    foreach($pageArray as $page) {
        $out .= "\n\t<li><a href='{$page->url}'>{$page->title}</a> {$page->datetime} {$page->summary}</li>";
    }
    $out .= "\n</ul>";
    $event->return = $out;
}
  • Like 1
Link to comment
Share on other sites

I think we can automate the field creation part relatively easily. My plan was to add an Export/Import option to Admin > Setup > Fields, and it would basically export a JSON profile of all the fields you selected. Then you could paste that profile into another site into the "import" textarea, and it would create all the fields. The same thing could probably be done at the template level too.

Sounds excellent. Only thing to consider is when there is different fields with same name.

Simple field & template modules: Thanks for your example. Looking that code I realize that I did exact that thing with AdminBar and Sitemap-page ;) I think that is pretty straightforward also. I'll take this route with most common needs (news/blog, event calendar etc) where I need one button installs and all others could go with field export/import.

Link to comment
Share on other sites

Sounds excellent. Only thing to consider is when there is different fields with same name.

We would just skip any field names that were already present and provide a warning indicating that. The nice thing about JSON or XML exports is that they are readable and easy to change, regardless of whether you understand the details of the format... it's ultimately just text. I like this style of export/import because it's easy to modify that text in between export and import. Most people wouldn't ever take advantage of that aspect, but it's nice to know that you can. So if you needed to change some aspect of the field (like the name) before importing, it's a straightforward thing to do.

Link to comment
Share on other sites

  • 1 year later...

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