Jump to content

How to programatically attach a new field with the template script file?


sam13579
 Share

Recommended Posts

8 hours ago, pwired said:

Have you seen the processwire documentation ?

https://processwire.com/api/ref/field/

1) Setup a field of type textarea
2) Echo or include the textarea field where you want it

echo $Yourpage->yourfield;
include $Yourpage->yourfield;

I was looking for a way to make template with fields programatically. But I think this is not happening. What you are suggesting is adding fields that are already made inside database. But thanks.

Link to comment
Share on other sites

I was addressing your first question

This is a different question

Quote

I was looking for a way to make template with fields programatically.

Have you read the Processwire Documentation ?
Your answers are here:

https://processwire.com/docs/start/variables/templates/

---------------------------------------------------------------------
Creating a new template with api

<?php namespace ProcessWire;
$fieldgroup = new Fieldgroup();
$fieldgroup->name = "something";
$fieldgroup->add("title"); // add some fields
$fieldgroup->add("body");
$fieldgroup->add("summary");
$fieldgroup->save();

$template = new Template();
$template->name = "something"; // must be same name as the fieldgroup
$template->fieldgroup = $fieldgroup;
$template->save();

---------------------------------------------------------------------
Adding fields to a template with api

<?php namespace ProcessWire;
$template = $templates->get("some_template");
$template->fields->add("body"); // add some fields
$template->fields->add("summary");
$template->fields->add("images");
$template->fields->save(); 

---------------------------------------------------------------------

Forum
https://processwire.com/talk/topic/4921-how-to-create-template-file-using-api-under-a-module/
https://processwire.com/talk/topic/352-creating-pages-via-api/

Examples
https://processwire-recipes.com/recipes/add-fields-to-template/

Beginner mistakes
https://processwire.com/talk/topic/19415-trying-to-create-field-using-api/

 

 

 

 

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, pwired said:

I was addressing your first question

This is a different question

Have you read the Processwire Documentation ?
Your answers are here:

https://processwire.com/docs/start/variables/templates/

---------------------------------------------------------------------
Creating a new template with api

<?php namespace ProcessWire;
$fieldgroup = new Fieldgroup();
$fieldgroup->name = "something";
$fieldgroup->add("title"); // add some fields
$fieldgroup->add("body");
$fieldgroup->add("summary");
$fieldgroup->save();

$template = new Template();
$template->name = "something"; // must be same name as the fieldgroup
$template->fieldgroup = $fieldgroup;
$template->save();

---------------------------------------------------------------------
Adding fields to a template with api

<?php namespace ProcessWire;
$template = $templates->get("some_template");
$template->fields->add("body"); // add some fields
$template->fields->add("summary");
$template->fields->add("images");
$template->fields->save(); 

---------------------------------------------------------------------

Forum
https://processwire.com/talk/topic/4921-how-to-create-template-file-using-api-under-a-module/
https://processwire.com/talk/topic/352-creating-pages-via-api/

Examples
https://processwire-recipes.com/recipes/add-fields-to-template/

Beginner mistakes
https://processwire.com/talk/topic/19415-trying-to-create-field-using-api/

 

 

 

 

 

That saved my day. Thanks.

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