Jump to content
Marc

PageTable page not being assigned to parent page

Recommended Posts

I have a template file that receives some form data that I save to a pageTable field. I use the pageTable to add notes to user profiles, so every user can have child pages containing a note and a date. Here's what I use:

$notes = $input->post->textarea('notes'); // Passed via javascript.
$customerId = $input->post->int('id');
$customer = $users->get($customerId);

$newNote = new Page;
$newNote->template = 'user-notes'; 
$newNote->parent = $customer; 
$newNote->of(false); // turn off output formatting before setting values
$newNote->save();
    
$newNote->title = 'user note';
$newNote->notes_date = date('Y-m-d H:i:s');
$newNote->notes = $notes;
$newNote->save();

// So far so good, but this bit does not work:
$customer->user_notes->add($newNote);
$customer->of(false);
$customer->save('user_notes');
// Just as an example, this part DOES work:
$customer->gender = 2;
$customer->save('gender');

The child page is created but it is not added to the user's 'user_notes' pageTable. If I place this code in my admin module, it works as expected. Any ideas what could be going wrong?

Edit: fixed it by switching these lines around:

$customer->of(false);
$customer->user_notes->add($newNote);

Now it works as expected. Strange it works in the other order on another page, but there you go.

Share this post


Link to post
Share on other sites

Fixed it by switching these lines around:

$customer->of(false);
$customer->user_notes->add($newNote);
 

Now it works as expected. Strange it works in the other order on another page, but there you go.

Share this post


Link to post
Share on other sites

Admin modules sometimes do run with output formatting of by default (depends on when they are executed), which would explain the behaviour. 

Share this post


Link to post
Share on other sites

It's something I'll keep in mind. Output format should be off before modifying a pagearray, check.  :)

Share this post


Link to post
Share on other sites

Just make sure to keep the right thing in mind: Output formatting needs to be of if you're editing any fieldvalue of a page, especially if you're trying to save it afterwards. There's no correlation to pagearrays per se. 

  • Like 1

Share this post


Link to post
Share on other sites

Looks like I still don't quite understand. In a regular template file I added these tests, trying to remove a page from 'user_notes' which is a PageArray field:

$note = $pages->get(1259);
$customer = $note->parent;

$customer->user_notes->remove($note);
$customer->of(false);

$customer->save('user_notes');

^ Result: no error, but PageArray is not updated.

$note = $pages->get(1258);
$customer = $note->parent;

$customer->of(false);
$customer->user_notes->remove($note);

$customer->save('user_notes');

^ Result: "Field 'user_notes' from page 1172 is not saveable because it is a formatted value."

$note = $pages->get(1258);
$customer = $note->parent;

$customer->user_notes->remove($note);
$customer->of(false);
$customer->user_notes->remove($note);

$customer->save('user_notes');

^ Result: PageArray is successfully updated.

Why would I have to use the 'remove' method twice in order for this to work?

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      So I ran into a very strange issue today. I have a template with a pagetable and I went to add an item to it, when I went to select an image (for an image field) the page instantly threw up an error
      "ProcessPageSearchLive: No search specified"
      The page's content also switched to the image attached. This all worked perfectly last week (local mamp box). Has anyone experienced this before, and how did you solve it?
       

    • By jploch
      Hey folks,
      I have a question regarding rendering of template files that are in a subfolder of the template folder.
      How would I for example render these PageTable items, when their template files are inside templates/blocks/:
      <div class="grid"> <?php foreach($page->grid_ext as $item): ?> <div id="pteg_<?= $item->id ?>"> <?php echo $item->render(); ?> </div> <?php endforeach; ?> </div> I tried this, but get an error:
      <?php echo $item->render(wire('config')->paths->templates . '/blocks/'); ?>  
    • By tron1000
      Hello! I use PW 3.0.98 and I have frontend editing enabled for a PageTable Field. Somehow, when I double click the field in the frontend, the iframe in wich the content is displayed is very small (see screenshot). I couldn't find out if thats some CSS conflict or another problem. Any suggestions? Thanks, Andrej
       

    • By Noel Boss
      I use a PageTable field to make edits to children of pages more intuitive…
      To register the hooks, insert the following Snippet inside your init function in your module (or add it to your init.php file):
      /** * Initialize the module. * * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. */ public function init() { // Prefill pageTable field $this->wire()->addHookBefore('InputfieldPageTable::render', $this, 'addChildrenToPageTableFieldsHook'); $this->wire()->addHookBefore('InputfieldPageTableAjax::checkAjax', $this, 'addChildrenToPageTableFieldsHook'); } Then, add this hook method:
      /** * Fill pagetable fields with children before editing…. * * @param HookEvent $event */ public function addChildrenToPageTableFieldsHook(HookEvent $event) { $field = $event->object; // on ajax, the first hook has no fieldname if (!$field->name) { return; } // get the edited backend page $editID = $this->wire('input')->get->int('id'); if (!$editID && $this->wire('process') instanceof WirePageEditor) { $editID = $this->wire('process')->getPage()->id; } $page = wire('pages')->get($editID); // disable output formating – without this, the ajax request will not populate the field $page->of(false); // you could also insert a check to only do this with sepcific field names… // $page->set($field->name, $page->children('template=DesiredTemplate')); // just specific templates $page->set($field->name, $page->children); } Now whenever there is a page-table field on your page, it gets populated with the children
    • By pppws
      Hey there,
      i've set up a page wich uses pageTable and it works like a charm.
      for my home page i get several pages. they are sperated in two types:
      a) page has the module 'featured_img'
      b) page doesn't have the 'featured_img'
       
      my pageTable field is called add_modules. so i was wondering if something like 
      <?php foreach ($featured as $child): ?> <?php if ($child->add_modules->module_featuredimg): ?> // show image <?php else: ?> // show text <?php endif; ?> <?php endforeach; ?> is possible?
       
      thanks for your input!
×
×
  • Create New...