Jump to content
theoretic

Problem: saving repeater item from one page to another

Recommended Posts

Thanks everyone for a perfect product!

Got a problem during development of a website which has two different templates sharing the same "suboffers_prices" repeater field: the "offer" template and the "order" template. The idea is to take a single repeater item from offer page and to save it as a repeater item to a newly-created order page.

Getting a single repeater is not complicated:

$suboffers_prices = $pages->get("uid={$input->post->suboffers_prices_uid}");

But saving that suboffers_prices to a new order page appeas to be not that easy:

    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->save(); //doesn't save the repeater

...another try...

    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->suboffers_prices->save(); //in fact saves nothing
...
    $newOrder->save(); //doesn't save the repeater

...and another...

    $newOrder->suboffers_prices->getNew();
    //$newOrder->suboffers_prices->of(false); //not callable in this context, PW crashes with error
    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->suboffers_prices->save(); //again saves nothing
...
    $newOrder->save(); //again no repeater saved

Tried some other approaches, still no result. Would like to get any advice on the best practice for saving a single repeater item to another page. Thanks in advance!

Share this post


Link to post
Share on other sites

Not certain, but I think you may need to iterate through each of the repeater items subfields, adding them to the new repeater item.

Also, take a look at this: 

In a hurry, so not sure if you are perhaps following that correctly or not.

 

 

Share this post


Link to post
Share on other sites

OK, have some hours spent to resolve the problem. Conclusions:

  1. It's possible to clone a repeater item using API,..
  2. ...but it's a delicate procedure which can be performed in the only correct way. Trying to use alternative approaches may even corrupt your database (thanks again to the PW creators for making a very simple and straightforward DB structure which can easily be healed if corrupted!)
  3. My way to clone the repeater item from one page to another:
     
    //Creating a new page which will hold the repeater clone
    $newPage= new Page();
    $newPage->of(false);
    $newPage->template = 'my_template_with_repeater_field';
    //Setting some more fields skipped
    ...
    //Getting the original repeater item
    $repeaterItem = $pages->get("some_selector=$some_value");
    
    //Saving the page before cloning the repeater -- it's very important!
    $newPage->save();
    
    //OK let's clone that repeater item now!
    $repeaterItemClone = $newPage->repeater_field->getNew(); //the repeater clone is already created and saved to the database
    //Cloning the repeater fields
    $repeaterItemClone->field1 = $repeaterItem->field1;
    $repeaterItemClone->field2 = $repeaterItem->field2;
    //Cloning some more fields skipped
    ...
    //Saving the modified clone
    $repeaterItemClone->save();

     

  4. Maybe it could be useful to create a PW module for cloning repeater items.

Hope this will be useful to someone ;)

 

Share this post


Link to post
Share on other sites
4 hours ago, theoretic said:

//Cloning the repeater fields $repeaterItemClone->field1 = $repeaterItem->field1; $repeaterItemClone->field2 = $repeaterItem->field2; //Cloning some more fields skipped

So rather than specifying each field, this is why I suggested looping through them.

 

4 hours ago, theoretic said:

Maybe it could be useful to create a PW module for cloning repeater items.

This seems like a perfect idea for an action for the AdminActions module (https://processwire.com/talk/topic/14921-admin-actions/) action. Are you interested in putting one together, or would you like me to?

  • Like 1

Share this post


Link to post
Share on other sites

Thanks adrian! Another great idea from You! It seems You know Your module much better than me, so You'll be faster to add one more action. However i can try to do it in a couple of days.

Share this post


Link to post
Share on other sites

@theoretic - I have added it as a new action in my module. However I am not sure it will really help with your current need - it provides a GUI for copying/overwriting repeater items from one page to another. Hopefully it might be useful for you in other circumstances though.

Share this post


Link to post
Share on other sites

@theoretic - I just posted an update to the new action - it now allows you to define a selector to limit to the matching repeater items, so you could theoretically now use the action via the API, but probably no point now that you have your code working.

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 David Karich
      ProcessWire InputfieldRepeaterMatrixDuplicate
      Thanks to the great ProModule "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use a content module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      This module extends the commercial ProModule "RepeaterMatrix" by the function to duplicate repeater items from one page to another page. The condition is that the target field is the same matrix field from which the item is duplicated. This module is currently understood as proof of concept. There are a few limitations that need to be considered. The intention of the module is that this functionality is integrated into the core of RepeaterMatrix and does not require an extra module.
      Check out the screencast
      What the module can do
      Duplicate multible repeater items from one page to another No matter how complex the item is Full support for file and image fields Multilingual support Support of Min and Max settings Live synchronization of clipboard between multiple browser tabs. Copy an item and simply switch the browser tab to the target page and you will immediately see the past button Support of multiple RepeaterMatrix fields on one page Configurable which roles and fields are excluded Configurable dialogs for copy and paste Duplicated items are automatically pasted to the end of the target field and set to hidden status so that changes are not directly published Automatic clipboard update when other items are picked Automatically removes old clipboard data if it is not pasted within 6 hours Delete clipboard itself by clicking the selected item again Benefit: unbelievably fast workflow and content replication What the module can't do
      Before an item can be duplicated in its current version, the source page must be saved. This means that if you make changes to an item and copy this, the old saved state will be duplicated Dynamic loading is currently not possible. Means no AJAX. When pasting, the target page is saved completely No support for nested repeater items. Currently only first level items can be duplicated. Means a repeater field in a repeater field cannot be duplicated. Workaround: simply duplicate the parent item Dynamic reloading and adding of repeater items cannot be registered. Several interfaces and events from the core are missing. The initialization occurs only once after the page load event Attention, please note!
      Nested repeaters cannot be supported technically. Therefore a check is made to prevent this. However, a nested repeater can only be detected if the field name ends for example with "_repeater1234". For example, if your MatrixRepeater field is named like this: "content_repeater" or "content_repeater123", this field is identified as nested and the module does not load. In version 2.0.1 the identification has been changed so that a field ending with the name repeater is only detected as nested if at least a two-digit number sequence follows. But to avoid this problem completely, make sure that your repeater matrix field does NOT end with the name "repeater".
      Changelog
       
      2.0.1
      Bug fix: Thanks to @ngrmm I could discover a bug which causes that the module cannot be loaded if the MatrixRepeater field ends with the name "repeater". The code was adjusted and information about the problem was provided 2.0.0
      Feature: Copy multiple items at once! The fundament for copying multiple items was created by @Autofahrn - THX! Feature: Optionally you can disable the copy and/or paste dialog Bug fix: A fix suggestion when additional and normal repeater fields are present was contributed by @joshua - THX! 1.0.4
      Bug fix: Various bug fixes and improvements in live synchronization Bug fix: Items are no longer inserted when the normal save button is clicked. Only when the past button is explicitly clicked Feature: Support of multiple repeater fields in one page Feature: Support of repeater Min/Max settings Feature: Configurable roles and fields Enhancement: Improved clipboard management Enhancement: Documentation improvement Enhancement: Corrected few typos #1 1.0.3
      Feature: Live synchronization Enhancement: Load the module only in the backend Enhancement: Documentation improvement 1.0.2
      Bug fix: Various bug fixes and improvements in JS functions Enhancement: Documentation improvement Enhancement: Corrected few typos 1.0.1
      Bug fix: Various bug fixes and improvements in the duplication process 1.0.0
      Initial release Support this module
      If this module is useful for you, I am very thankful for your small donation: Donate 5,- Euro (via PayPal – or an amount of your choice. Thank you!)
      Download this module (Version 2.0.1)
      > Github: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate
      > PW module directory: https://modules.processwire.com/modules/inputfield-repeater-matrix-duplicate/
      > Old stable version (1.0.4): https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate/releases/tag/1.0.4
    • By arnd
      Hi,
      I have a repeater with some images in it.
      As Superuser I can work with the Actions (rotate...). But my Users with lower Rights can't use this Actions.
      The same User can use the Actions on images outsite a Repeter-Field. So I think it's not a Problem of Rights-Management but from Images in Repeater Fields.
      ProcessWire 3.0.164.  Bug? Or can I manage this in the settings?
      Thanks in advance
    • By Robin S
      This is a module I made as an experiment a while ago and never got around to releasing publicly. At the time it was prompted by discussions around using Repeater fields for "page builder" purposes, where the depth feature could possibly be used for elements that would be nested inside other elements. I thought it would be useful to enforce some depth rules and translate the depth data into a multi-dimensional array structure.
      I'm not using this module anywhere myself but maybe it's useful to somebody.
      Repeater Depth Helper
      This module does two things relating to Repeater fields that have the "Item depth" option enabled:
      It enforces some depth rules for Repeater fields on save. Those rules are:
      The first item must have a depth of zero. Each item depth must not be more than one greater than previous item depth. It provides a RepeaterPageArray::getDepthStructure helper method that returns a nested depth structure for a Repeater field value.
      Helper method
      The module adds a RepeaterPageArray::getDepthStructure method that returns a multi-dimensional array where the key is the page ID and the value is an array of nested "child" items, or null if there are no nested children.
      Example

      The module doesn't make any assumptions about how you might want to use the depth structure array, but here is a way you might use it to output a nested unordered list.
      // Output a nested unordered list from a depth structure array function outputNestedList($depth_structure, $repeater_items) { $out = "<ul>"; foreach($depth_structure as $page_id => $nested_children) { $out .= "<li>" . $repeater_items->get("id=$page_id")->title; // Go recursive if there are nested children if(is_array($nested_children)) $out .= outputNestedList($nested_children, $repeater_items); $out .= "</li>"; } $out .= "</ul>"; return $out; } $repeater_items = $page->my_repeater; $depth_structure = $repeater_items->getDepthStructure(); echo outputNestedList($depth_structure, $repeater_items);
       
      https://github.com/Toutouwai/RepeaterDepthHelper
      https://modules.processwire.com/modules/repeater-depth-helper/
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By neonwired
      I have a front end form for creating new pages, repeater and repeater matrix field don't seem to save any data. I was considering handling the data manually but can't seem to get anything useful from the post data, are there any methods i can use?
×
×
  • Create New...