As the title says I want to know how to add a custom operation in the drupal 8 book admin edit form's operations. I need this for my module in which I want to provide Add Sub Page operation for the user.

enter image description here Upon clicking on the operation, I want to execute a function to add a new page to the parent page. I know how to add pages programmatically (Thanks to Lisa regarding help for adding pages in the book) , the only thing that I need is this operation entry in the drop down list.

Can somebody please help?

  • I don't have an answer, but I think you're going to need to implement a hook to alter the form, which is built in Drupal\book\Form\BookAdminEditForm. Take a look at bookAdminTableTree(), the $form[$id]['operations'] code in particular. You'll need to add a $form['id]['operations']['#links']['add-sub-page'] item for each page in the book. It looks like it'll be fairly complicated, so perhaps somebody has a better idea... – user72086 Jul 17 '17 at 13:41
  • Another random idea for an alternative approach: instead of using a hook to alter the form, extend the BookAdminEditForm class with your own custom class. Then implement your own bookAdminTableTree() method, with the modified operations links. Then alter the existing /admin/structure/book/{node} route (book.admin_edit) in book.routing.yml with a route to your new custom form class. See drupal.org/docs/8/api/routing-system/… and the setDefault() method in the Route class: – user72086 Jul 17 '17 at 13:46
  • Route class link didn't fit in my comment above: api.drupal.org/api/drupal/… – user72086 Jul 17 '17 at 13:46
  • Just found this tutorial which seems helpful: jaypan.com/tutorial/… – user72086 Jul 17 '17 at 13:47
  • @Lisa Your alternative approach worked! Please post that as an answer. – Shujaat Ali Khan Jul 17 '17 at 16:07

You can override the bookAdminTableTree() method in the BookAdminEditForm class by extending core/modules/book/src/Form/BookAdminEditForm.php with your own custom form class. You can copy the bookAdminTableTree() method's code and then add an item to the $form[$id]['operations']['#links'] array for 'add-sub-page' (or whatever you want to name the new operation.)

Then alter the existing /admin/structure/book/{node} route (book.admin_edit) in book.routing.yml with a route to your new custom form class. See www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones and the setDefault() method in the Route class: api.drupal.org/api/drupal/vendor%21symfony%21routing%21Route.php/class/Route/8.2.x

This is a helpful tutorial that describes the overall approach: https://www.jaypan.com/tutorial/drupal-8-extending-core-configuration-extending-core-forms-and-overriding-core-routes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.