lukaskleinschmidt / kirby-sortable

Making subpage managing a breeze
79 stars 6 forks source link

Rename modules & update blueprint in filestructure #51

Closed findthebug closed 5 years ago

findthebug commented 5 years ago

Kirby 2.5.12 Latest Modules & Sortables plugin PHP 7.2

Maybe this two issues are related to each other: You can reproduce this in the kirby starterkit. here a basic test installation:

app.zip

Renaming module slug does not proper update the folder .txt modules: entry When a modules slug title is changed in the panel, the .txt files Module reference is still the "old" slug. i have to go back to the parent page of the module and hit save once to update the .txt in the filesystem. Step 1 to 5 to reproduce.

1 2 3 4 5

Renaming an module title/slug to a number leads to invisible modules in the panel There seems to be a problem with numbers in the module title/slug. When i create a module and change the title/slug to a number the module is then no longer visible in the panel backend.

a b

lukaskleinschmidt commented 5 years ago

The bug with disappearing pages in the panel when having numeric slugs is now fixed. But I would like to note that one should be careful with using slugs starting with numbers in kirby 2 in the first place. You might run into unexpected behavior in some cases (Wrong folder name if new page starts with a number).

The other problem is not something I am able to fix properly. Actually the reason why the modules are stored in the parent .txt file is to keep the order for visible and invisible subpages in the panel and I did never rely on the stored order in the frontend.

If you rely on the stored order and need to make sure that the subpages are always in sync with the field then you would need to implement a hook on your own to do so. Sadly there is no generic way of doing this. Something like the following might get you going:

kirby()->hook('panel.page.move', function ($page, $oldPage) {
  // the used field name
  $field = 'modules';

  // when you are using the sortable (modules) field in conjunction
  // with the modules-plugin it is relatively easy to determine
  // weather the moved page is a module or not
  // if ($page->isModule()) {
  //
  // }
  //
  // otherwise you need to somehow make sure that the moved page
  // is a module with something along this lines perhaps:
  // if (
  //   $page->intendedTemplate() === 'subTemplate' &&
  //   $page->parent()->intendedTemplate() === 'parentTemplate'
  // ) {
  //
  // }

  if ($page->isModule()) {
    $parent = $page->parent();

    // when using the parent option
    if (!$parent->{$field}()->exists()) {
      $parent = $parent->parent();
    }

    $values = $parent->{$field}()->split(',');
    $index = array_search($oldPage->uid(), $values);
    $values[$index] = $page->uid();

    $parent->update([
      $field => implode(', ', $values),
    ]);
  }
});
findthebug commented 5 years ago

Problem with slug is solved in patch 2.4.1. Thanks!