processwire / processwire-issues

ProcessWire issue reports.
45 stars 2 forks source link

Required repeater item fields with Required field action issue #639

Closed PWaddict closed 1 year ago

PWaddict commented 6 years ago

Short description of the issue

Having "Required field action" set to "Unpublish the page" on a specific template and also having a repeater field on that template with at least 1 empty required repeater item field the page doesn't unpublish.

Optional: Suggestion for a possible fix

I'm currently using the following hook as a temp fix to auto unpublish the page when there are empty required repeater items on a specific repeater and template.

I've also changed "Repeater item visibility in editor" to "Items always open" so the site editor will easily see which repeater items needs to edit. Of course it would be better to only display repeater items as opened the ones with the missing required values but I don't know how to do that.

$wire->addHookAfter("Pages::save", function(HookEvent $event) {

  $page = $event->arguments[0];
  if($page->template->name != 'my-template') return;

  $admin_page_name = wire("pages")->get(2)->name;
  $repeater_id = wire("fields")->get('my_repeater_field')->id;

  $empty_items = wire("pages")->find("template=repeater_my_repeater_field, my_text_field='', parent=/{$admin_page_name}/repeaters/for-field-{$repeater_id}/for-page-{$page->id}/, include=all");

  if(count($empty_items)) {

    if ($page->is(Page::statusUnpublished)) return;
    $this->session->error($this->_("Page unpublished because missing required value."));
    $page->of(false);
    $page->addStatus(Page::statusUnpublished);
    $page->save();
  }

});

Setup/Environment

ryancramerdesign commented 6 years ago

Thanks, I have pushed a fix for this issue.

PWaddict commented 6 years ago

@ryancramerdesign I like what you did where the repeater items with missing values are opened but there is still one problem that needs to be fixed: If a page is already published and the editor add a new repeater item with missing required values when the page is saved it doesn't get unpublished.

PWaddict commented 6 years ago

@ryancramerdesign I just test it again and it seems to be working properly cause now the page is getting unpublished as it should. I will do more testings and report back.

PWaddict commented 6 years ago

@ryancramerdesign I found why it doesn't work sometimes. The page doesn't get unpublished if the repeater item is "New". Check the screenshot.

repeater_issue

netcarver commented 5 years ago

@ryancramerdesign Bumping this for your consideration.

matjazpotocnik commented 2 years ago

@PWaddict, is this still an issue? There have been some changes in repeaters, so please check with the latest dev.

PWaddict commented 2 years ago

@matjazpotocnik @ryancramerdesign

The issue still exists in 3.0.199. The page doesn't get unpublished if the repeater item is "New". Here is the screenshot: repeater_issue

ryancramerdesign commented 1 year ago

@PWaddict @matjazpotocnik @netcarver The reason for this is that the repeater item with label "New" is not a published repeater item, and thus not contributing to the live page's data. A repeater item won't publish until its own requirements are met. So there would be no reason to unpublish the owning page for a missing requirement on an unpublished repeater item, as the front-end page would be the same either way.

But if a repeater item is already published, and you go and make its title blank, then that's going to affect the live page data, and thus your unpublish rule would apply for the owning page at that point.

PWaddict commented 1 year ago

@ryancramerdesign maybe the best solution for this "new" repeater item is to auto delete if it's not used at all?

ryancramerdesign commented 1 year ago

@PWaddict I'd prefer not to auto-delete anything since these are technically pages in the system. Plus I'm not sure we can really tell what the intention of the user is, probably best to leave it to them to decide.