Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
As a result of 2854083 and 2854080 we have a use case where we would like to avoid the header row being output.
Proposed resolution
Provide an option in the constructor in the same way that the delimiter is set.
Remaining tasks
Proposed patch to follow shortly.
User interface changes
None.
API changes
None, fully backwards compatible; simply provides a new option that another module could choose to use.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#7 | csv_serliazation-2854087-skip-headers.patch | 1.65 KB | Steven Jones |
#4 | interdiff-2854087-2-4.txt | 1.12 KB | scott_euser |
#4 | option_for_csv_encoder-2854087-4.patch | 2.78 KB | scott_euser |
#2 | option_for_csv_encoder-2854087-2.patch | 1.73 KB | scott_euser |
Comments
Comment #2
scott_euser CreditAttribution: scott_euser as a volunteer and at Fat Beehive commentedComment #3
Berdirno empty line between multiple @params.
Also, this is a service, so how would you set this through the constructor? Should we add a setter method? This is something you want to control per call, as you might call it multiple times per request, so maybe actually an argument to encode()? I see it supports $context and also has setSettings(), so it should probably be integrated there.
This will also help #2789531: Support for batch operations
Comment #4
scott_euser CreditAttribution: scott_euser as a volunteer and at Fat Beehive commentedWas wondering what to do with that; my editor is throwing all sorts of coding standards warnings. Thought I might keep it consistent with their current style then submit a second issue to fix all the coding standards at once? For instance, pre patch it looks like this already:
Makes sense, I looked at the setSettings, but it seems to be very tied to views and it feels dirty to do something like
Proposed solution attached.
Comment #5
scott_euser CreditAttribution: scott_euser as a volunteer and at Fat Beehive commentedEssentially I think the method needs to be public if we want to use it, or potentially break it into separate getters and setters as you suggest and leave that one for the views plugin only as it is now.
Comment #6
grasmash CreditAttribution: grasmash at Acquia commentedWould you be willing to re-roll this for 8.x-2.x?
Comment #7
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commentedHere's an updated patch against 8.x-1.4.
I've changed it to use a public setter and called that from the protected setSettings. I've also remove the constructor argument, though it does feel like this should be an argument to the encode method, but I guess the interface is fairly inflexible there.
Comment #8
grasmash CreditAttribution: grasmash at Acquia commentedComment #9
KingdutchIs there a reason that the patch chooses for a new function? I think this makes it hard to use through the Serializer API right? If context were to be used then that may be easier as that variable is passed on verbatim.
Could an example be added of how the new setting would be used through batcher Serializer API calls?
There was also some work and discussion done in #2999806: Option for CSV Encoder to add header or not to. which may be of interest.
Comment #10
scott_euser CreditAttribution: scott_euser as a volunteer and commentedBerdir's comment https://www.drupal.org/project/csv_serialization/issues/2854087#comment-... explains why it shouldn't be in the constructor.
Use case example: https://www.drupal.org/node/2854083 - would not want the headers to be output in in batch.
Hope that helps clarify!
Comment #11
KingdutchRe #10
I understand why you wouldn't want it in the constructor and I agree with the sentiment.
My question was why the patch under review introduces a new function rather than using the
$context
parameter of theencode
function as described by Berdir in that same comment.It also makes your use case easier because you could just add a ternary expression in the context instead of calling a separate function.
Comment #13
grasmash CreditAttribution: grasmash at Acquia commentedPlease take a look at https://www.drupal.org/node/2567011/commits and let we know if it addresses this issue. I followed Kingdutch's line of thinking and removed the separate setter method.
Comment #15
DamienMcKennaThe code in the module doesn't provide any means to control whether the header is output, the only solution is to extend the class and add an option in the constructor.
Comment #16
DamienMcKennaThe idea of using the $context argument to encode() wouldn't currently work as the function() doesn't modify the current object attributes before $this->outputHeader is checked. So as it works, there isn't any way to control the header unless a) you extend the class, b) you pass in $context['views_style_plugin']->options['csv_settings']. Can this please go back to "needs work"?