Add configuration allowing administrative lists of books to be sorted alphabetically
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #2969362. --> Reported by: [ivavictoria](https://www.drupal.org/user/3061533) Related to !208 !207 !11 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>Across the site, Books are listed in the order they were created, with no other sort options. (This is about the books themselves, not pages <em>within</em> a book.) I'd like to present more sort options for books.</p> <p>This affects the list of books on /admin/structure/books, the node creation Book dropdowns, and the Book Navigation block when it appears on all pages.</p> <p>Here is a compilation of screenshots from a fresh installation of 8.6.x, with ten books. I've included the books' weight in their titles. You can see they are listed in the order they were created (click to see full size):<br> <a href="https://www.drupal.org/files/issues/2018-05-03/Screenshot-original-sort.png"><img src="https://www.drupal.org/files/issues/2018-05-03/Screenshot-original-sort.png" width="100" alt="Examples of how the book lists are currently sorted."></a></p> <p>This is fine on small sites, but for large sites, it's terribly unwieldy. Trying to locate a specific book is hard when the list is long, because it's not sorted in a meaningful way.</p> <p><strong>Steps to Reproduce:</strong></p> <ul> <li>Enable the book module.</li> <li>Create two or more books: Content &gt; Add Content &gt; Book Page &gt; 'Outline' tab &gt; Create a new Book, select a random weight value as well.</li> <li>Visit /admin/structure/books to see that your books are sorted by creation date.</li> <li>Create a sub-page in one of your books: Content &gt; Add Content &gt; Book Page &gt; 'Outline' tab &gt; Select one of your books. Notice that your books are sorted by creation date in the dropdown.</li> <li>Structure &gt; Block layout &gt; Add a Book navigation block to your sidebar, using the default settings. In the resulting block, your books are once again sorted by creation date.</li> <li>TO TEST MY PATCH: Apply the patch, then go to /admin/structure/books/settings and select a sort order (alpha or weight). Observe that books in all the previous locations have been rearranged to match your selected sort.</li> </ul> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>On my large site, I'd like to sort the books alphabetically by their titles.</p> <p>In <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/book/-/work_items/2828681" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/book/-/work_items/2828681</a></span>, @mtnguru wanted to sort the Book Navigation block by book weight.</p> <p>The patch I'm proposing expands upon that, by allowing both weight and alpha sort, in <em>all</em> lists of books (not just the Book Navigation block).</p> <p>I've added the ability to sort by either alpha or weight; weight is the default sort.<br> I've added a new Book config setting that remembers which sort the site builder wants. My initial patch includes the new config, the updated settings form, and the new sort methods.</p> <p>I chose to implement the sorts in BookManager.php's getAllBooks() function, since that is the source of all of these unsorted lists. I'm unfamiliar with the Book module's code, so if there is a better place for this, I'm all ears!</p> <p>Here are screenshots of the end result (click to see full size):</p> <p>New settings option on /admin/structure/book/settings:<br> <a href="https://www.drupal.org/files/issues/2018-05-03/Screenshot-settings.png"><img src="https://www.drupal.org/files/issues/2018-05-03/Screenshot-settings.png" width="550" alt="New sort selection radio buttons for Books."></a></p> <p>Based on the selected setting, I get either an alpha sort or a weight sort across all lists:<br> <a href="https://www.drupal.org/files/issues/2018-05-03/Screenshot-alpha-sort.png"><img src="https://www.drupal.org/files/issues/2018-05-03/Screenshot-alpha-sort.png" width="100" alt="Examples of the books being sorted alphabetically."></a><br> <a href="https://www.drupal.org/files/issues/2018-05-03/Screenshot-weight-sort.png"><img src="https://www.drupal.org/files/issues/2018-05-03/Screenshot-weight-sort.png" width="100" alt="Examples of the books being sorted by weight."></a><a href="https://www.drupal.org/files/issues/2018-05-03/Screenshot-original-sort.png"></a></p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <ul> <li>Reviews needed <ul> <li><del>Are there other places that Books get listed that I've missed?</del></li> <li><del>Any changes to the verbiage presented to users on the settings page? It probably needs clarification.</del></li> </ul> </li> <li><del>Needs upgrade path</del></li> <li><del>Tests need to be written.</del> <ul> <li><del>Someone wrote a test in the previous issue (<span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/book/-/work_items/2828681" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/book/-/work_items/2828681</a></span>) that might be a good start.</del></li> </ul> </li> </ul> <h3 id="summary-data-model-changes">Data model changes</h3> <p>I added a new Book configuration option. Shouldn't interfere with any previous config, though.</p>
issue