Jump to content

ProcessWire ProFields: Table


ryan

Recommended Posts

On 5/20/2014 at 11:05 AM, apeisa said:

Nope, not yet. I am pretty sure there will be documentation before 2.5 is released.

Did this ever happen? I can't find any PageTable documentation for all my searching! It seems odd, the docs are so great, generally.

(I'm specifically looking for how to create a page with several pagetable field values programmatically).

  • Like 2
Link to comment
Share on other sites

  • 5 months later...

You know, I was looking into this as well for the opposite reason in that I have always used RepeaterMatrix to build up customizable content stacks but I found old posts talking about how PageTable was more efficient for this than Repeaters and so I've been sleuthing and unfortunately the road ends here? Maybe someone who has been here longer can chime in?

Link to comment
Share on other sites

On 9/19/2022 at 10:09 PM, artfulrobot said:

(I'm specifically looking for how to create a page with several pagetable field values programmatically).

Behind the scenes entries in a PageTable field are just regular pages in a regular PageArray. Lets say you have a field "pagetable" and it is configured to use template "blog-post" for items.

You can create a new page with new items in the pagetable field like this:

// create page that holds pagetable items
$p = new Page();
$p->template = 'basic-page';
$p->parent = '/foo/';
$p->title = "Bar";
$p->save();
// get parent id for pagetable items from field config
$parentID = $p->getField('pagetable')->parent_id;
// create item page for field pagetable
$item = new Page();
$item->template = 'blog-post';
$item->parent = $parentID;
$item->title = "Item Foo";
$item->save();
// add item page to pagetable field
$p->pagetable->add($item);
$p->save('pagetable');

Since the value of $p->pagetable is a PageArray, you can find and manipulate the pagetable items with methods from Pages, PageArray and WireArray.

To remove a specific item:

$item = $p->pagetable->get('title="Item Foo"');
$p->of(false);
$p->pagetable->remove($item);
$p->save('pagetable');

 You can also use WireArray manipulation methods insertBefore(), insertAfter(), append(), prepend() to add items in a specific order.

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...

I'm having trouble displaying a pagetable field in the same order as it appears on the page edit screen.
The child pages are hidden so I use $page->getUnformatted("field") to get the array of pages but a foreach loop outputs them in completely random order. When I try sort("sort") it then appears in the order of the child pages in the page tree so that "sort" refers to the order from the Pages table.

I never had trouble with this in earlier versions, this random order appeared in the last two websites I'm developing.

Is there a way to access the internal sort of the actual pagetable field as per screenshot?

282645152_Screenshot2023-03-22at17_39_33.png.0eaaefb07e45d08533a37b783962a39e.png

Link to comment
Share on other sites

  • 1 year later...

In file FieldtypeTable.module  on line: 1355
code should be: 
    $v = explode(',', $v ?? '');     // old code:   $v = explode(',', $v ); 
php +8.1 otherwise gives an error:  
PHP Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in...

Link to comment
Share on other sites

  • 3 months 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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...