Render TypoScript Objects in a CommandController - typo3

tldr: How do I render a TypoScript COA Object with a GIFBUILDER image from inside a CommandController?
I'm currently developing an eCommerce platform for which I need to periodically import an excel file that holds the product catalog. After a product is created from a row of data in the excel file, a directory is searched for product images related to that item, and they are linked as FileReferences to the product. I've written an ImportCommandController that takes care of that.
This all works like a charm, with the only glaring problem being the performance of the image manipulation. The first call to a product list pages takes a good 30 seconds, the first request of a single view. I periodically need to recreate the whole catalog from scratch, and the source product images are huge files, I have no influence on that.
The product images of that catalog are being generated by a TypoScript that takes care of fitting these images inside a square white background, returning the IMG_RESOURCE url. I call that TypoScript from inside a Fluid template with the cObject ViewHelper.
I've been trying to call this bit of TypoScript from the ImportCommandController->importAction, so that the import cronjob would take care of creating these scaled images beforehand, with the same filename hash, so they're already processed when they're called by the single view. But I can't manage to do that.
The TypoScript in question ist this:
plugin.tx_productfinder_products {
// Productfinder-Produktbilder
// Bilder quadratisch auf weissen Hintergrund einpassen
productimage = COA
productimage {
// Daten der FileReference im Regsiter ablegen
10 = FILES
10 {
references.data = current
renderObj = COA
renderObj {
10 = LOAD_REGISTER
10 {
param = TEXT
param.data = file:current.uid
}
}
}
20 = IMG_RESOURCE
20 {
file = GIFBUILDER
file {
XY = 960,960
format = jpg
quality = 95
backColor = #ffffff
20 = IMAGE
20 {
offset = 960-[20.w]/2,960-[20.h]/2
file {
import.data = current
treatIdAsReference = 1
maxW = 960
maxH = 960
}
}
// // Text aus Daten der FileReference als Wasserzeichen ins Bild rendern
// 30 = TEXT
// 30 {
// //text.data = register:param
// text.data = current
// fontColor= #dddddd
// fontSize = 12
// offset = 20,[20.h]-20
// align = left
// antiAlias = 1
// }
}
}
}
}
And I call these from inside the Fluid template like so:
<img class="img-responsive" src="{f:cObject(typoscriptObjectPath:'plugin.tx_productfinder_products.productpreviewimage', data:'{image.uid}')}">
What have I tried so far? Pretty much everything.
I've first tried to call the ContentObjectRenderer directly,
/** #var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
$contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
$contentObject->setCurrentVal($image->getUid());
$content = $contentObject->cObjGetSingle($this->settings['productimagetest'], $this->settings['productimagetest.']);
resulting in these weird errors.
Oops, an error occurred: PHP Warning: imagejpeg(typo3temp/GB/csm_8000424600_cbbd127be3_9cb1d3c8cc.jpg): failed to open stream: No such file or directory in /html/typo3/typo3_src-7.6.16/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php line 2912
It seems that the TYPO3 Configuration regarding the Grapics Processing isn't initialized in the same way as it is for the Frontend.
Next, I tried instanciate a StandaloneFluidView to render the whole SingleItem template per item, but I cant figure that out because the Request and Context are missing and so the partials being referenced in the template aren't being rendered.
Then I tired to just create the FrontendUrls per single item and request them from the CommandController,
/** #var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
$contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
$test = $contentObject->typolink_URL(array(
'parameter' => 671,
'additionalParams' =>
'&tx_productfinder_products[product]='.$product->getUid().
'&tx_productfinder_products[action]=show'.
'&tx_productfinder_products[controller]=Product',
'returnLast' => 'url'
));
$this->outputLine(print_r($test,true));
but the URLs generated in this way are missing the cHash.
Can anybody offer help or a different approach to this?

Since these images are generated just once for the frontend output and are then available anyway, I don't see the advantage of generating them beforehand. The white square could be easily generated with CSS, so to me this does not even look like a use case for the GIFBUILDER.
That being said there's still something you could do: Since you are in the PHP context already, why don't you instanciate the GIFBUILDER directly or use even pure IM/GM commands instead of going for an IMG_RESOURCE which is actually meant to be output in the frontend?

I know this is an old question, but I had the same issue and had to invest some time to debug it, so maybe this this answer is useful for someone else in the future.
The CLI uses a different working directory than the frontend, therefore the relative path to typo3temp/ directory used in the GifBuilder class cannot be resolved, which results in the above mentioned warning.
To fix the relative path resolution, you have to change your working directory to the frontend one, this can be achieved by:
class AcmeCommand extends Command
{
protected static $cwdBackup;
protected function execute(InputInterface $input, OutputInterface $output)
{
if (Environment::isCli()) {
static::$cwdBackup = getcwd();
chdir(Environment::getPublicPath());
}
//
if (Environment::isCli()) {
chdir(static::$cwdBackup);
}
}
}

Related

How to include Header.html partial in TYPO3 ver 9

I want to change the Layout of my Headers in TYPO3. There is a post about this but this but I cant get it to work and that post is 2 years old. With TYPO3 most stuff is outdated quite quickly. This is the post.
Additionally I looked at this article. I know it is in German, maybe it helps anyways.
So I copied the Header.html from the TYPO3 system files, put it in a directory under fileadmin and tried to link to that directory.
fileadmin/.../Partials/Header/Header.html
In the Template setup i added the partialRootPath.
page = PAGE
page {
shortcutIcon = fileadmin/sitedesign/Resources/Private/Templates/Vave/img/Favicon.ico
10 = FLUIDTEMPLATE
10.file = fileadmin/sitedesign/Resources/Private/Templates/Vave/Contact/index.html
10.partialRootPath {
20 = fileadmin/Resources/Private/Partials
}
includeCSS {
contactFile1 = fileadmin/sitedesign/Resources/Private/Templates/Vave/Contact/css/Contact.css
}
}
In Header.html I created an additional case to check if it was working.
<f:case value="7">
<p class="{positionClass}">
<f:link.typolink parameter="{link}">{header}</f:link.typolink>
</p>
</f:case>
I then added that new case in the Page Resources to the TSConfig.
TCEFORM.tt_content.header_layout {
addItems.7 = Name1
}
Changing the content of the Header.html file in the TYPO3 system files works the way I expected it to, so I am quite sure that I understand the basic functionality. The TSConfig part works as well, because I can select "Name1" in the Header Layout Type field.
But no matter what part in the fileadmin Header.html version I change, nothing happens. I checked the path to that Partials folder and the spelling of everything a million times, so I do not think that is the issue either.
Of course I could just change the system file Header.html but that seems wrong on a lot of levels.
Thank you for any help.
Don't mix different usages of fluid!
You want to change the header partial of your content elements.
But you add the new partial to the fluid of page rendering.
if you use FSC (Fluid Styled Content) your additional partial path should go here:
lib.contentElement {
partialRootPaths {
10 = fileadmin/Resources/Private/Partials
}
}
breaking change: lib.contentElementinstead of lib.fluidContent
Additional advices:
be carefull with the names: aside from partialRootPaths there sometimes exist partialRootPath (without s in the end), which is not an object array. That enables you to set only one path (not the usual path list with priority) and which has priority over settings in partialRootPaths if both exist.
separate the different fluid usages!
Give them different paths. There are multiple ways. I prefer:
each extension gets it's own three folders in a folder named for the extension.
And also separate the page rendering and CEs (Content Elements). Your own CEs might be considered as part of the extension 'FSC'.
use a site extension.
All configuration goes into that extension: typoscript, templates, viewhelpers, TCA, ...
That is the basic configuration for that site, but also the additional configuration/ adaption for the used extensions.
May this code will help you!!
page = PAGE
page {
shortcutIcon = fileadmin/sitedesign/Resources/Private/Templates/Vave/img/Favicon.ico
10 = FLUIDTEMPLATE
10 {
templateName = TEXT
templateName {
cObject = TEXT
cObject {
data = levelfield:-2,backend_layout_next_level,slide
override.field = backend_layout
required = 1
case = uppercamelcase
split {
token = pagets__
cObjNum = 1
1.current = 1
}
}
ifEmpty = Innenseite
}
#templateName=TEXT
# templateName.value=index
layoutRootPaths {
20 = your layoutRootPaths
}
partialRootPaths {
20 = your partialRootPath
}
templateRootPaths {
20 = your templateRootPath
}
}
includeCSS {
contactFile1 = fileadmin/sitedesign/Resources/Private/Templates/Vave/Contact/css/Contact.css
}
}
Make sure header included properly in main template

Include javascript (or css) as inline in HEAD with Typoscript in TYPO3 7.6

I have a little javascript file that I need to run before my site loads (it contains some modernizr code and some more).
Usually I add it using includeJS but for performance issues I need it inline. As workaround I am including it using headerData in this way:
headerData {
10 = TEXT
10.value (
<script>
// Here goes my javascript code
<script>
)
}
It works but it is ugly and difficult to update. Is there a way to say something like
headerData {
10 = TEXT
10.value < include(../path/to/my/file.js)
}
I don't see how this is an performance improvement over page.includeJs but you can just render a template here:
headerData {
10 = FLUIDTEMPLATE
10.file = EXT:my_ext/Resources/Private/Templates/MyTemplate
}

How to display latest or all posts (pages) of a parent page, in a single page in TYPO3 CMS

I have a TYPO3 TypoScript html template and want to show in a single page (called: "competitions"), a list of all competitions with their image and title and this should be links for each of them ('competition'). I have thought to make a page ('competitions') and subpages ('single cmpetition'). I'm not using Fluid actually. How can i get and display those in main page? Or is another way to achieve this ?
depending on your data structure (is the relevant info you want to show stored in the pages record or do you want access tt_content records inside that pages) it is more or less easy.
you always can access the informations with typoscript, but those constructs may get complicated. that means: developing the TS may take some time and the following rendering might also get complex and might take some time.
the easiest way: all information in pages records:
temp.competitions = CONTENT
temp.competitions {
table = pages
select {
pidInList = this
orderBy = sorting
}
renderObj = COA
renderObj {
wrap = <div class="teaser">|</div>
10 = TEXT
10.field = title
10.wrap = <h3>|</h3>
20 = TEXT
20.field = abstract
20.wrap = <div class="abstract">|</div>
30 = IMAGE
:
}
}

Extract single Object from Object list in typoscript

Ok, the headline is bit confusing, but i didnt know, how to describe my problem in a short version. Here we go: I get a list of image-urls from the resources of one page, it works like this:
lib.slider = FILES
lib.slider {
references {
data = levelmedia:-1, slide
}
renderObj = TEXT
renderObj {
data = file:current:publicUrl
wrap = {image :'|'}
}
}
Now i want to make an image-container from each of the images. I'm not very good in typoscript so i have no idea, how to do that. I had the idea of making an array out of it and using the f:for-Viewhelper but I dont know how to get an array from this or how to access the images from html.
As an alternative to your own findings, you can modify the renderObj to be an IMAGE cObject, like the following - that way you can define dimensions of your images as well.
lib.slider = FILES
lib.slider {
...
renderObj = IMAGE
renderObj {
file {
import.data = file:current:originalUid // file:current:uid
maxW = 150
}
}
}
See the FILES documentation and IMAGE documentation for further examples.
Ok, I found a solution myself, ich just changed the wrapper to <img src="|" /> and got what i wanted

TYPO3: Trying to add link to images

On our site, other admins add images via the "Resources" tab of the main page. These images are displayed as Banners in a Slider on the main page. However, now they want the ability to add links to specific images.
My first thought on this (after receiving some help on making a loop for images to be added to the page) was to perhaps let them be able to add the link to either the "Title" or "Caption" spot I saw there. And later, on the slider "create" function, pull the said data from the image and make <a> wrap around the image before the slider finished building. I've already tested the slider plugin with this functionality, and that would work fine, however, I can't seem to pull anything from the "Title" or "Caption" and add it to the image in any way.
My other thought would be, is there a way to extend the back end to give them an actualy spot to paste links on images so that I may pull that and wrap the image via the typoscript, or can i pull from caption and wrap image in <a> "if" the link is available.
In other words, does typoscript have a type of "if" statement? What I ahve so far, thanks to maholtz is as follows:
#BANNER IMAGES LOOP BEGIN
page.10.marks.topimage = TEXT
page.10.marks.topimage {
# retrieve data
data = levelmedia: -1, "slide"
override.field = media
# we have some filenames in a list, let us split the list
# and create images one by one
# if there are five images selected, the CARRAY "1" will be executed
# five times where current is loaded with only one filename
split {
# the images are separated via ","
token = ,
# you can do funny stuff with options split, f.e. if you want to give first
# and last image a different class... but thats another topic;)
# we just say, render every splitted object via CARRAY "1"
cObjNum = 1
1 {
# just render the single image,
# now there should be one filename in current only
10 = IMAGE
10 {
file.import.wrap = fileadmin/user_upload/|
file.import.current = 1
border = 0
file.height = 670
file.width = 1800
altText = Banner
titleText = Banner
# attempt to add link to image if available
caption.1.typolink.parameter.field = image_link
caption.1.typolink.parameter.listNum.stdWrap.data = register:IMAGE_NUM_CURRENT
}
}
}
wrap = <div id="slides">|</div>
}
#BANNER IMAGES LOOP END
I was thinking perhaps I could do something like:
10 {
file.import.wrap = fileadmin/user_upload/|
file.import.current = 1
border = 0
file.height = 670
file.width = 1800
altText = Banner
titleText = Banner
# attempt to add link to image if available
caption.1.typolink.parameter.field = ???
caption.1.typolink.parameter.listNum.stdWrap.data = register:IMAGE_NUM_CURRENT
}
But as you can see, I'm stumped on how that might even work right. Can anyone help point me the right way.
As before mentioned, perhaps I could do ONE of two things:
Pull link from "Title" or "Caption" and add it to the IMAGE Date on output so that I can use that client side to wrap the image in appropriate a tag, |OR|
Pull link from there and use typoscript to wrap the image in a tags
When accessing the ressources via levelmedia = slide you're not directly accessing the FAL table. Therefore you have to load it again to access the fields you want. We solved exactly the problem you have with the following code. Insert it inside your 1 after 10 = IMAGE.
typolink{
parameter{
cObject = RECORDS
cObject{
source.current = 1
tables = sys_file_reference
conf.sys_file_reference = TEXT
conf.sys_file_reference.field = #title or description
}
}
}