Jump to content
DarsVaeda

[solved] frontend edit on repeater fields

Recommended Posts

Hi,

how do I enable frontend editing for repeater fields?

It partially works with 

foreach($page->repeater_field as $item) {
>?
<li><edit list_item><?= $item->list_item ?></edit></li>
<?php
}

But if I click a frontend item the text disappears. I'm not sure if this is a bug or because it does not know which text it should reference.
I tried with:

foreach($page->repeater_field as $item) {
>?
<li><edit list_item_repeater<?= $item->id ?>><?= $item->list_item ?></edit></li>
<?php
}

But that results in an exception.

Share this post


Link to post
Share on other sites

Hi,

could this be related to permissions?

We currently encounter problems with frontend editing of repeater fields, too: in our case it's simply not possible to frontend-edit repeater fields for users with a specific role, although these users are able to edit the respective repeater fields in the regular backend page editor. Superusers are able to frontend-edit repeater fields as expected.

@DarsVaeda this is an example of how we included frontend-editing which works (apart from the problem described above):

<?php 
/** @var RepeaterPageArray $attributes */
foreach ($attributes as $attribute): ?>
  <div class="attribute">
    <?php $image = $attribute->get('image'); ?>
    <div class="attribute__image" edit="<?php echo $attribute->id; ?>.image">
      <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
    </div>
    <div class="attribute__body">
      <?php echo $attribute->edit('body'); ?>
    </div>
  </div>
<?php endforeach; ?>

 

Cheers, Alex

Share this post


Link to post
Share on other sites

Hmm maybe, I need to try but I only have one user currently who is admin.
But now after updating I have the problem that all fields get "deleted" when starting to edit.
I'm not sure if it's my implementation or a bug. Need to dig into this.

Share this post


Link to post
Share on other sites
On 23/01/2017 at 8:14 PM, DarsVaeda said:

I tried with:


foreach($page->repeater_field as $item) {
>?
<li><edit list_item_repeater<?= $item->id ?>><?= $item->list_item ?></edit></li>
<?php
}

 

This is the actual problem. It doesn't work with this implementation.
If I use this:

foreach($page->repeater_field as $item) {
>?
<li><?= $item->edit('list_item') ?></li>
<?php
}

Then frontend editing works.

Thanks @design-a-point!

Share this post


Link to post
Share on other sites

I had the same issues you both mentioned. When logged in with some other role then superuser and try to edit a page, either the text disappeared or I wasn't even able to edit the text, at all.

The problem comes up, if you want to use option B (explained here: https://processwire.com/api/modules/front-end-editing/ ) for front end editing. 
I chose it above the other options, because of its direct "inline access" (compared to the dialog box with option C and D). Because repeater fields are stored as child of the admin page in the backend, other user roles don't have access to them by default, which leads to the afore mentioned issues, I guess. 
My solution was to give those user roles access to the admin page template and its children.

It took me some time to discover it, so if other users have problems with this, as well, maybe it's worth mentioning it in the module docs somewhere?

Share this post


Link to post
Share on other sites
23 minutes ago, Christoph said:

It took me some time to discover it, so if other users have problems with this, as well, maybe it's worth mentioning it in the module docs somewhere?

Did you check the front-end editing documentation when investigating your problem? It specifically advises against using option B for Repeaters:

Quote

But not worthwhile for things like Files/Images, PageTables, Repeaters or any field that you iterate to output or access object properties from. For those you will want to use <edit> tags or edit attributes, per options C and D.

 

  • Like 1

Share this post


Link to post
Share on other sites

Yes, I read it. 

As I had no problems using option B with repeaters as a superuser, I just wanted to make it work for other user roles, too. Option C/D is just no real front end editing any more, at least regarding how my clients understand it. In my current client's context, the front end editing is just needed to correct typos and the likes.

But you are right, as there is a strong advice to not use option B in a repeater context, the docs don't need to provide a solution on how make it work :).

Share this post


Link to post
Share on other sites
1 hour ago, Christoph said:

Option C/D is just no real front end editing any more, at least regarding how my clients understand it.

It is only option D that forces the modal dialog editor. Option C allows inline editing - you just have to place edit tags around the individual fields in your repeater rather than around the whole repeater foreach(). For example:

<?php foreach($page->my_repeater as $item): ?>
    <h3><edit <?= $item->id ?>.title><?= $item->title ?></edit></h3>
<?php endforeach; ?>

You're right that there does seem to be some sort of permissions issue for non-superusers, but rather than change access for the admin template (which non-superusers shouldn't have access to) I suggest you give edit access to your repeater template (select the "Show system templates" option to see repeater templates).

  • Like 3

Share this post


Link to post
Share on other sites
On 08/02/2017 at 10:00 AM, Robin S said:

Did you check the front-end editing documentation when investigating your problem? It specifically advises against using option B for Repeaters:

 

Is that documentation up to date? It doesn't work for me with option C. I get a popup which is not usable. Would need to dig into what is wrong with the html/css.
But option B works for me without any problem (despite the need to set the permission on the system template as you suggested).

Share this post


Link to post
Share on other sites

@DarsVaeda Had the same issue with unusable popup on option C/D. My solution was to change the jQuery version to the one processwire is using.

@Robin S Thanks for your help! Will try your solution with option C later today. You're right, instead of changing the admin template, it is enough to allow access for the repeater_content template

Share this post


Link to post
Share on other sites
9 hours ago, DarsVaeda said:

But option B works for me without any problem

5 hours ago, Christoph said:

where's the difference to option B in this case?

Re-reading the docs, I'm not sure why Ryan advises against using option B with Repeaters or PageTables (for Files/Images fields it makes sense). Maybe he means if you are wanting to make the entire Repeater/PageTable field editable rather than the individual fields inside the items, because it does seem to work fine for the latter case (once the access bug is worked around).

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Peter Knight
      I have 2 repeater types working with the Repeater Matrix.
      To keep things simple, I have
      image gallery a downloads gallery. I can't quite figure out how to echo the contents from within a product_gallery. 
      I can get the code below to display the text 'An image gallery' but not the bunch of images within this.
      I'm not sure though if it's my variable trail or my nested echo statement.
       
      <?php foreach($page->components as $item) { if($item->type == 'product_gallery') { echo " An image gallery..."; foreach($product_gallery->images as $photo) { echo " An image <image src='{$photo->url}'> "; } } else if($item->type == 'downloads') { echo " A download gallery... "; } } ?> Thanks
      P
    • By David Karich
      Thanks to the great Pro module "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 this module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      With this proof of concept I have created a module which adds the feature to copy a repeater item to the clipboard so that you can paste this item to another page with the same repeater field. The module has been developed very rudimentarily so far. It is currently not possible to copy nested items. There is also no check of Min/Max. You can also only copy items that have the same field on different pages. And surely you can solve all this more elegantly with AJAX. But personally I lack the deeper understanding of the repeaters. Also missing on the Javascript side are event triggers for the repeaters, which would make it easier. Like e.g. RepeaterItemInitReady or similar.
      it would be great if @ryan would implement this functionality in the core of RepeaterMatrix. I think he has better ways to implement this. Or what do you think, Ryan?
      Everybody is welcome to work on this module and improve it, if it should not be integrated into the matrix core. Therefore I put it for testing and as download on GitHub: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDublicate
      You can best see the functionality in the screencast: 
       
    • By anderson
      Hi,
      For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page.
      But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected.
      <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error?
      Thanks in advance.

    • By Harmen
      Hi All,
      I've made a previous regarding an issue which I solved but I stumbled across a new issue :). Below again the explanation needed.
      You need a short introduction for this. The company I am working for has approx. 80 products on their website and they all have their own features. The features are imported from an older system which isn't used anymore. Back then, when they changed from the old system to PW, we imported the features into the product pages as a JSON-array so the pages could load a bit faster as exploding a JSON array is a bit faster than loading in a lot of items from a table or a repeater field.
      How the JSON array looks like: 
      {     "2": {  // The ID of the group that contains the actual features         "name": "Model and function", // The name of the group         "features": {             "43": { // Attribute ID                 "name": "Resolution (DPI)", // Attribute name                 "values": {                     "896": "500-1500-2000-3500" // Value ID and value name                 }             }         }     } } Now we are adding a few more products to the catalog with new features and some that are already in use by other products but I am really struggling to assign the right ID's with the correct values in 7 different languages without messing up filters etc. So I decided to develop a module that lets you easily add feature groups, attributes and values as pages and connect these to the product using a repeater. This started by exporting all the current groups, attributes and values and import them as pages in the following structure:
      - Features - Feature Groups - Group 1 - Group 2 - ... - Feature Attributes - Attribute 1 - Attribute 2 - ... - Feature Values - Value 1 - Value 2 - ... Secondly, I created a repeater field that I assigned to the 'Features'-page that can handle the JSON structure explained above by seeing each item of the repeater field as a group. Inside each item you can select the group page and you will find another repeater which contains 2 page selectors to select an attribute and a value. A single repeater field looks like this:

      Next step was to create a module that gets this field as an Inputfield from the Features page. Once the user has finished adding groups, attributes and values he can click on save and I export all the values in the same JSON array structure as earlier but now the ID's of the groups, attributes and values are just the page ID's. This works great to add new features to new products. 
      BUT, sometimes the features of a product change or need to be changed and you don't want to change the JSON array manually.
      So my idea was to do the same thing as adding the features, but now you grab the values from the product first and populate the items of the repeater field.
      $featuresPage = wire("pages")->get("template=features"); $featureGroups = json_decode($product->features, true); foreach ($featureGroups as $featureGroupID => $featureGroup){ $row = $featuresPage->features_repeater->getNewItem(); $groupPage = wire("pages")->get($featureGroupID); $row->feature_group_selector = $groupPage; $row->save(); foreach ($featureGroup["features"] as $featureID => $feature){ $featuresRow = $row->feature_repeater->getNewItem(); $attributePage = wire("pages")->get($featureID); $valuePage = wire("pages")->get(key($feature['values'])); $featuresRow->feature_attribute_selector = $attributePage; $featuresRow->feature_value_selector = $valuePage; $featuresRow->save(); } $row->save(); } $featuresPage->save(); $f = wire('fields')->get('features_repeater'); $inputfield = $f->getInputfield($featuresPage); $form->add($inputfield); $f = $this->modules->get("InputfieldSubmit"); $f->name = 'updateFeatures'; $f->label = 'Update Features'; $f->icon = 'plus'; $f->value = 'Update Features'; $form->add($f); Then the user can change / add features, click save and done!
      It is possible to update the feature attributes and values, but when I change the feature group value, it seems like PW creates a new field behind the scenes and keeps the old value as well. So let's say I have the following feature structure:
      - Feature Group 1 - Feature attribute 1 - Feature value 1 Now I want to update Feature Group 1 to Feature Group 2, below is the desired result and the actual result:
      // === Desired result - Feature Group 2 - Feature attribute 1 - Feature value 1 // === Actual result - Feature Group 2 - Feature attribute 1 - Feature value 1 - Feature Group 1 - Feature attribute 1 - Feature value 1  
      I am using the following function to update the features:
      private function processForm5_UpdateFeatures(InputfieldForm $form){ $form->processInput($this->input->post); if (count($form->getErrors())) return false; $pageID = $this->session->selectedProduct; $product = wire('pages')->get($pageID); $product->of(false); $repeater = $form->get("features_repeater")->value; $languages = wire("languages"); foreach ($languages as $language) { $return_array = []; $i = 0; foreach ($repeater as $repeaterItem) { //$this->message($repeaterItem); $group = $repeaterItem->feature_group_selector; if ($group["id"] == 0) { continue; } else { $group = wire("pages")->get($group["id"]); $feature_group_name = &$return_array[$group->id]['name']; if (!isset($feature_group_name)) $feature_group_name = $group->title->getLanguageValue($language); $features = $repeaterItem->feature_repeater; foreach ($features as $feature) { $this->message($feature); $attribute = $feature->feature_attribute_selector; $value = $feature->feature_value_selector; if ($attribute["id"] == 0 || $value["id"] == 0) { continue; } else { $attribute = wire("pages")->get($attribute["id"]); $value = wire("pages")->get($value["id"]); $return_array[$group->id]['features'][$attribute->id]['name'] = $attribute->title->getLanguageValue($language); $return_array[$group->id]['features'][$attribute->id]['values'][$value->id] = $value->title->getLanguageValue($language); } } } } $product->features->setLanguageValue($language->name, json_encode($return_array, JSON_UNESCAPED_UNICODE)); } $product->save(); $featuresPage = wire("pages")->get("template=features"); $featuresPage->features_repeater->removeAll(); $featuresPage->save(); /* $this->messages("clear all"); $this->errors("clear all");*/ $this->message("Updated all features for {$product->title}"); $this->session->redirect("../"); } Is there a way to avoid the current result and get the desired result? Why is there even a new repeater page created for the new value? Anything that can help me is greatly appreciated!
       
      ~Harmen
    • By SwimToWin
      ProcessWire is setting a "wires" cookie for each guest session.
      Is it possible drop that cookies, so there are no cookies at all for guests?
      That way, I don't need to spam the user with a cookie consent box.
      I don't need cookies for user preferences and marketing purposes.
      (Why are cookies being set by default in the first place?)
×
×
  • Create New...