-
-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Allow developers to define their own view #1784
base: main
Are you sure you want to change the base?
Conversation
I regret to disappoint you but the option of JSON view you've already with https://github.com/TYPO3-Headless/headless_news |
I know, i have written my thought for it at the bottom of my comment |
Plus you talk about the JsonView. I tried to make it so that every view can be used. Self made or a core one. The JsonView was just an example because it the most used one after the TemplateView |
@@ -35,15 +32,16 @@ public function __construct(NewsController $newsController, string $defaultViewO | |||
} | |||
|
|||
/** | |||
* Get the news controller | |||
* @return NewsController |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required, can be removed
|
||
/** | ||
* @var string | ||
* @param NewsController $newsController |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All not required, pleas remove
@@ -39,14 +39,14 @@ class News extends AbstractEntity | |||
protected $l10nParent = 0; | |||
|
|||
/** | |||
* @var DateTime | |||
* @var DateTime|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am really unsure if that breaks in 10 because extbase can't get the type anymore of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe leave those changes, also imo not related to this feature, correct?
Now days a lot of websites are taking the headless approach, so i figured that the news extension should give the ability to the developers to use the JsonView if needed or any other view for that matter.
I added an event and a getView function which gets the view and appends the developer's wished view to the called action. It can be done for every action or individual actions. An example would be:
Create the listeners
If every action should adopt the developer's wished view:
If only a specific action should adopt the developer's wished view:
Now if we want to use the JsonView, the
$this->view->assignMultiple($event->getAssignedValues());
is not enough. We have to tell the JsonView which variables should be rendered. This can be done like this. I am using the listAction as an example:This will render everything as json and no templates are used. The only problem you will come across, is that the news entries are objects and can not be converted to json properly. In this case you have two options (AFAIK):
$event->getAssignedValues()['news']
. A small example would be the following.This works perfectly, the only con is that you have to manually iterate and select the values to append to the new created array. The same applies for the files (images, pdfs etc). You have to manually generate the links to the target file.
Pros:
Cons:
Last but not least, you can take the headless approach, which in this case, you do not need any of the above. The only disadvantage is that you ll have to create the json output in your .html Templates. The headless extension has actually created an extended version of the news extension to make it headless. https://github.com/TYPO3-Headless/headless_news. But if you got to the Resources and look at the files, everything is converted to json in the .html files. This is very sensitive, because a space or a line not on the right position can cause the json output to be converted to string and can be very tedious.
If anyone can make it better, please do not hesitate to edit the code!