I think that this will be necessary to achieve the goals set in the parent issue 2735561. It will make those patches much easier. I think it is better to separate this into another task so that patches are smaller and easier to read and collaborate on.

Here is the proposed file structure:

  • src/
    • PrintBuilder.php
    • PrintBuilderInterface.php
    • PrintEngineException.php
    • Annotation/
      • {{ export_type }}Engine.php
    • Entity/
      • {{ export_type }}Engine.php
      • {{ export_type }}EngineInterface.php
    • Event/
      • {{ export_type }}CssAlterEvent.php
      • {{ export_type }}EventBase.php
      • {{ export_type }}Events.php
      • {{ export_type }}HtmlAlterEvent.php
      • Presend{{ export_type }}Event.php
      • Presend{{ export_type }}MultipleEvent.php
    • EventSubscriber/
      • {{ export_type }}PostRenderSubscriber.php
    • Form/
      • SettingsForm.php
    • Plugin/
      • {{ export_type }}EngineBase.php
      • {{ export_type }}EngineInterface.php
      • {{ export_type }}PluginManager.php
      • Action/
        • {{ export_type }}Download
      • {{ entity_type }}Engine/
        • {{ export_library }}.php
      • Renderer/
        • ContentEntityRenderer.php
        • RendererBase.php
        • RendererFactory.php
        • RendererFactoryInterface.php
        • RendererInterface.php

I have to run now, but please let me know if you have suggestions, comments, or questions. I'll keep working on that issue with the way I've changes the export libraries in config.

TODO

  1. Fix the tests, add any new tests
  2. Add an upgrade path
  3. Create a 8.x-2.x branch to commit this to

Comments

jordanpagewhite created an issue. See original summary.

jordanpagewhite’s picture

StatusFileSize
new19.41 KB

This is a WIP patch of the changes that are needed to modularize the current 8.x-1.x branch in a way that adding additional Engines* is simple. I won't bother to write a lengthy explanation here because I know it isn't great yet. I'll upload another patch, possibly tomorrow, and write an appropriate explanation of the changes I am proposing to the architecture of the project.

benjy’s picture

Thanks for getting started, would be good to have an issue summary that describes the approach when you're ready.

Couple of comments below.

  1. +++ b/src/EntityPrintPrintBuilder.php
    @@ -0,0 +1,118 @@
    +class EntityPrintPrintBuilder implements PrintBuilderInterface {
    

    I'm happy to simply called this EntityPrintBuilder or even PrintBuilder is nicer again?

  2. +++ b/src/EntityPrintPrintBuilder.php
    @@ -0,0 +1,118 @@
    +  public function getEntityRenderedAsPrint(EntityInterface $entity, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) {
    

    Same here we should take the opportunity to clean-up some naming, how about printEntity or even just print()?

jordanpagewhite’s picture

Issue summary: View changes

I've updated the issue summary with the proposed changes to the file structure. I will have to check my patch and make sure that it actually matches the suggested file structure. I was working on this patch and the Word Export patch simultaneously, and I don't think that I was careful enough in my commits and rebasing to match my intention. I'll fix it and upload the corrected patch though.

jordanpagewhite’s picture

Issue summary: View changes
benjy’s picture

Do you think we need different plugin types for every export_type, i was wondering if we could get away with a generic print plugin?

jordanpagewhite’s picture

Hmm. I don't know. Let me give it a shot and see what happens.

jordanpagewhite’s picture

Issue summary: View changes
StatusFileSize
new95.66 KB
new91.12 KB

Okay, so I gave it another shot and tried to use a generic print plugin as you suggested. That is what I was hoping I could do with my first patch, but I think I misunderstood some of the components. After spending quite a bit more time reading through the existing code, I think I am headed in a better direction with this patch. This should be more sustainable and modular. That being said, there are some issues with this patch in its current state. The /entityprint/node/1/debug works, but anything that utilizes an export library does not work.

Currently, I have tried to change the config schema like so:

entity_print.settings:
  type: config_object
  label: 'Entity Print settings.'
  mapping:
    default_css:
      type: boolean
      label: 'Enable the default CSS.'
    force_download:
      type: boolean
      label: 'Force download of Print'
    print_engines:
      type: mapping
      label: 'The plugin id of the Print engine to use.'
      mapping:
        pdf_engine:
          type: string
          label: 'The plugin id of the PDF engine to use.'

Also, the updated file structure is:

  • src/
    • PrintBuilder.php
    • PrintBuilderInterface.php
    • PrintEngineException.php
    • Annotation/
      • PrintEngine.php
    • Controller/
      • EntityPrintController.php
    • Entity/
      • PrintEngine.php
      • PrintEngineInterface.php
    • Event/
      • PrintCssAlterEvent.php
      • PrintEventBase.php
      • PrintEvents.php
      • PrintHtmlAlterEvent.php
      • PresendPrintEvent.php
      • PresendPrintMultipleEvent.php
    • EventSubscriber/
      • PostRenderSubscriber.php
    • Form/
      • SettingsForm.php
    • Plugin/
      • PrintEngineBase.php
      • PrintEngineInterface.php
      • EntityPrintPluginManager.php
      • Action/
        • {{ export_type }}Download
      • EntityPrint/
        • {{ entity_type }}Engine/
          • {{ export_library }}.php
      • Renderer/
        • ContentEntityRenderer.php
        • RendererBase.php
        • RendererFactory.php
        • RendererFactoryInterface.php
        • RendererInterface.php
benjy’s picture

in your global ~/.gitconfig file can you put:

[diff]
  renames = copies

This should help Git recognise when files have only been renamed and reduce the size of the diff.

Also, you can ignore the schema errors for now if you like, add protected $strictConfigSchema = FALSE; in the test files, and we can focus on getting everything else green.

jordanpagewhite’s picture

StatusFileSize
new95.74 KB
new6.9 KB

Gotcha. I made that change to my global gitignore. Thanks for the suggestion. They are easier to read now.

You can see the changes I made in this latest patch in the interdiff. The route-based export and Action plugin export are both working properly now. I think this provides a very extensible base to build upon with other export types. I will have to work on the tests though. I haven't updated them yet.

benjy’s picture

+++ b/src/Controller/EntityPrintController.php
+++ b/src/Controller/EntityPrintController.php
@@ -80,7 +80,7 @@ class EntityPrintController extends ControllerBase {

@@ -80,7 +80,7 @@ class EntityPrintController extends ControllerBase {
-      $print_engine = $this->pluginManager->createInstance($config->get('print_engines'));
+      $print_engine = $this->pluginManager->createInstance($config->get('print_engines.pdf_engine'));

Is the controller defaulting to PDF? I'm wondering if we need a new URL structure as well? Maybe /export_type/entity_type/entity_id, thoughts?

Let me know when you're ready for a thorough review and i'll go over it properly, probably won't do that until I return on the 4th though.

jordanpagewhite’s picture

Yeah, the controller is currently defaulting to PDF. I think we will need a new URL structure. I like /export_type/entity_type/entity_id. That is the same URL structure I was using in my patch to add Export to Word via PHPWord. That should be in this patch though. I will update the routing file, check all of the tests, and then I think it will be ready to review.

Thanks again for your help and feedback.

jordanpagewhite’s picture

Would it be simpler to change the URL structure to

/entityprint/{{ export_type }}/{{ entity_type }}/{{ entity_id }}

and the debug route could either be

/entityprint/debug/{{ entity_type }}/{{ entity_id }} OR
/entityprint/html/{{ entity_type }}/{{ entity_id }} OR
/entityprint/print/{{ entity_type }}/{{ entity_id }}

?

benjy’s picture

Maybe we should put the type on the end, works quite well.

/{{ entity_type }}/{{ entity_id }}/{{ export_type }} and
/{{ entity_type }}/{{ entity_id }}/{{ export_type }}/debug
jordanpagewhite’s picture

That's what I thought too, but do we need a /debug for each export_type?

jordanpagewhite’s picture

Status: Active » Needs review
StatusFileSize
new102 KB
new11.84 KB

I made some more changes based upon my latest work on 2733781. I didn't do much with the tests because we're going to have to change that up a bit anyway to test the different export types. I will try to tackle that soon though.

Status: Needs review » Needs work

The last submitted patch, 16: 2735875-16.patch, failed testing.

benjy’s picture

Thanks for working on this, looks like it's shaping up well. Couple of comments below, could you tell me which files I should focus my code review on? Just so it's easier because obviously there is a lot of noise with all the renaming.

  1. +++ b/src/Annotation/PrintEngine.php
    @@ -0,0 +1,40 @@
    +  public $export_type;
    

    Should this be engine_type? It seems export and engine refer to the same thing here. I do actually wonder if export is a better word than engine in general?

  2. +++ b/src/Controller/EntityPrintController.php
    @@ -57,47 +57,50 @@ class EntityPrintController extends ControllerBase {
    +   *   The response object on error otherwise the Print is sent.
    

    "Print" doesn't make sense here, we'll need it to be something like "exported document"

jordanpagewhite’s picture

  1. Yeah, it could be engine_type. I'm just trying to track if it's a pdf export or word_doc export.
  2. Yeah, you are correct. That comment doesn't make any sense. I will have to fix that. Good catch.

Yeah, I'll try to summarize what I've done. Hopefully that will at least give you a good start and you can ask other questions if you have them.

  • Changed the config to allow for multiple *_engines in the future. Right now we just have print_engines.pdf_engine.
  • Changed the Action to pdf_download_action to allow for other *_download_actions going forward.
  • I could use some help with entity_print_entity_view_alter in .module
    • If you can figure out a good way to get PDF and Word Doc and ePub from the config here, I think that would be helpful going forward. Maybe just adding another field in the config.
  • I changed up the SettingsForm a bit to allow for future export types
  • We should find a better way to abstract the file extension in generateFilename() in PrintBuilder. I think I came up with some lame solution in one of the other issues, but we come up with a better solution.

I feel like there was probably other changes, but nothing major that I noticed skimming through the patch. A lot of changes to filenames and methods.

benjy’s picture

Patch doesn't apply for me currently, can you re-roll?

<stdin>:319: trailing whitespace.
   * The filetype to be exported to.
error: patch failed: entity_print.routing.yml:1
error: entity_print.routing.yml: patch does not apply
error: patch failed: src/Controller/EntityPrintController.php:2
error: src/Controller/EntityPrintController.php: patch does not apply
jordanpagewhite’s picture

Status: Needs work » Needs review
StatusFileSize
new101.92 KB

Sorry. I missed some trailing whitespace. I applied this patch on a new branch and it worked. Let me know if there are any other issues. Thanks.

Status: Needs review » Needs work

The last submitted patch, 21: 2735875-21.patch, failed testing.

benjy’s picture

The patch still fails to apply, have you done a pull recently and rebased your work?

benjy’s picture

Issue summary: View changes
jordanpagewhite’s picture

Status: Needs work » Needs review
StatusFileSize
new101.95 KB
new13.41 KB

Okay, I rebased and I think that all of the merge conflicts were resolved appropriately

Status: Needs review » Needs work

The last submitted patch, 25: 2735875-25.patch, failed testing.

benjy’s picture

05:24:17 Fatal error: Class 'Drupal\entity_print\Plugin\PdfEngineBase' not found in /var/www/html/modules/entity_print/tests/modules/entity_print_test/src/Plugin/EntityPrint/PdfEngine/TestPdfEngine.php on line 20

Something went wrong with the merge?

jordanpagewhite’s picture

Status: Needs work » Needs review
StatusFileSize
new109.39 KB
new8.09 KB
new109.39 KB
new8.09 KB

Some of the tests weren't updated.

EDIT: Yeah, I guess something went wrong with that directory in the merge. Here's a patch to try to resolve that.

The last submitted patch, 28: 2735875-27.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 28: 2735875-27.patch, failed testing.

jordanpagewhite’s picture

I'm going to have to work on the tests more tomorrow. I'm tired.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new85.76 KB
new8.25 KB

Fixed a couple of tests to help this along.

+++ b/entity_print.module
@@ -48,17 +49,22 @@ function entity_print_entity_extra_field_info_alter(&$info) {
 function entity_print_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
-  if ($display->getComponent('entity_print_view')) {
-    $route_params = [
-      'entity_type' => $entity->getEntityTypeId(),

I reckon we could do this in a follow-up, just keep with only the View PDF link for now.

Status: Needs review » Needs work

The last submitted patch, 32: 2735875-32.patch, failed testing.

jordanpagewhite’s picture

Status: Needs work » Needs review
StatusFileSize
new121.33 KB
new5.38 KB

Updated routes on the tests

Status: Needs review » Needs work

The last submitted patch, 34: 2735875-34.patch, failed testing.

jordanpagewhite’s picture

Status: Needs work » Needs review
StatusFileSize
new123.76 KB
new5.87 KB

just working on the tests

Status: Needs review » Needs work

The last submitted patch, 36: 2735875-36.patch, failed testing.

jordanpagewhite’s picture

I'm not sure that I understand the issues with the tests right now. I might have to tinker with it later if nobody else has a clue.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new93.87 KB
new4.24 KB

This fixes the admin tests with a couple of @TODO's, to make the settings page work with multiple print engines in follow-up issues?

We still need an upgrade path I think although we could do that in follow-ups as well, I'm going to commit this to a 2.x branch, so I don't have any problems with not getting everything perfect here.

Status: Needs review » Needs work

The last submitted patch, 39: 2735875-39.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new93.63 KB
new489 bytes

This passes locally, just fixed a hardcoded return in dompdf.

Status: Needs review » Needs work

The last submitted patch, 41: 2735875-41.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new93.26 KB
new1.13 KB
benjy’s picture

I think this is pretty close, couple more comments.

  1. +++ b/src/Controller/EntityPrintController.php
    @@ -57,47 +53,50 @@ class EntityPrintController extends ControllerBase {
    -   * Output an entity as a PDF.
    +   * Output an entity as a Print.
    

    Print an entity to the selected format.

  2. +++ b/src/Entity/PrintEngine.php
    @@ -2,7 +2,7 @@
     /**
      * @file
    - * Contains \Drupal\entity_print\Entity\PdfEngine.
    + * Contains \Drupal\entity_print\Entity\PrintEngine.
      */
    

    Lets just delete this comment. We don't need them in class files.

  3. +++ b/src/Event/PrintEventBase.php
    @@ -0,0 +1,35 @@
    +abstract class PrintEventBase extends Event {
    

    We should add a follow-up issue to add the export type to all events. Or maybe add it as a method to the plugin itself? Either way, need a way to access that context.

We just need to create follow-ups for:

  1. Update the settings form for all export types
  2. Test/fix the "View" links for each export type
  3. Add and tests for an upgrade path from 1.x to 2.x
  4. Ensure export type context is available for all events.
jordanpagewhite’s picture

StatusFileSize
new123.88 KB
new808 bytes

I took care of your suggestions in #1 and #2. I agree with the follow-up issues. It would probably be easier if these other issues were broken into separate issues too. I can add the follow-up issues if you'd like. I'm pretty sure I could get started on 'Update the settings form for all export types' and 'Test/fix the "View" links for each export type'. Those seem pretty doable. I have never worked on an upgrade path like this. I would be very interested to see how that looks though and contribute however I can.

benjy’s picture

OK great, do you want to create the issues and then I'll work the upgrade path and you can make start on the others. I'll commit this to a 2x branch shortly.

benjy’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
benjy’s picture

Status: Needs review » Fixed

Committed to 2.x, thanks for the work here.

jordanpagewhite’s picture

Yay!

benjy’s picture

benjy’s picture

Assigned: jordanpagewhite » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.