I need to include a PHP file (containing some database-checks 'n' stuff) into a very bad programmed TYPO3 website. They used TemplaVoilà for templating purposes. This is the definition in the master TypoScript template:
[...]
page = PAGE
page {
[...]
10 = USER
10.userFunc = tx_templavoila_pi1->main_page
[...]
}
[...]
Within the used TemplaVoilà template they mapped the main content div (where I'd like to insert my PHP script) with the attribute „field_content“. Don't know if this helps to answer my question.
I tried nearly everything I know to somehow overwrite/fill the „Main Content Area“ through TypoScript, as it is completly empty () on the page I created for my PHP file.
Is it possible to fill a specific mapped tag with my PHP file through TypoScript?
(TYPO3 Version 4.5.32)
I figured out a solution for my problem: With the hint in the answer by Urs (using _INT) I altered an already included DS object to be extended through additional sub-objects (as the markup allows my code to be placed there with some modifications to the stylesheet):
lib.social.20 = PHP_SCRIPT_INT
lib.social.20.file = fileadmin/path_to_my_file
Now it works like a charm althrough it's a bit hacky...
What about
lib.myscript= USER
lib.myscript {
userFunc =user_myScript->main
}
and in the DS
<TypoScript><![CDATA[
10 < lib.myscript
]]></TypoScript>
I'm no TemplaVoilà expert, I just pulled the second snippet from http://typo3-beispiel.net/index.php?id=10
PS: if its's USER, it will be cached, if it's USER_INT, it will prevent static file caching. So you may be better off running those checks in the background via AJAX?
Related
Well, I am developing a plugin a and I need to display some stuff from my plugin when TYPO3 page load.
Is there some function how to hook action, like in WordPress when page loads than plugin will execute some controller method? Then this controller will produce some output a HTML, which I would like to dispaly in frontend page. Specially I would like display custom script in the head. So the script should be like this <head>...<script>my content</script>...</head>
Ok, what you probably want to do is to develop a so-called TYPO3 extension - that's what plugins/add-ons are called in TYPO3 (which is the term you will likely find google results for).
To get started fast you can try the TYPO3 extension builder (https://docs.typo3.org/typo3cms/extensions/extension_builder/) - which can generate a skeleton extension for you.
For more information you can also have a look at https://docs.typo3.org/typo3cms/CoreApiReference/latest/ExtensionArchitecture/Index.html which explains the concepts in far more detail.
Additional information is available in https://docs.typo3.org/typo3cms/ExtbaseFluidBook/Index.html
in TYPO3 there is something named plugins, but you should differ to the meaning in other context.
first TYPO3 is a CMS which content is structured in a hierarchical tree of pages. These pages are the basis for navigation. and each page contains individual contentelmenents (CE).
As Susi already told: add ons to TYPO3 are in general 'extensions' which could extend(!) the functinality of TYPO3 in different ways. one way is the definition of (TYPO3-)Plugins. These are special ContentElements which enable to show special information.
While normal CEs have all the information what to show in the record (e.g. Text & Image), plugins can be more flexible.
typical examples are: show a list of records OR one record in detail.
These Plugins can be controlled with typoscript or the plugin-CE could have additional fields to hold information what to display.
For detailed information how a plugin is defined consult the links given by Susi.
And be aware: for security reasons it is not possible to just execute a plain PHP file to echo any output. You need to register your plugin using the API, build your output as string and return the generated HTML as string to the calling function. For beginners the ExtensionBuilder will help you to generate a well formed extension which uses the API to register and output your data.
OK guys, thanks for your answers, but it was not very concrete. I found this solution, is not the best one, but it works! If anybody has better please share.
At first, you have to make a file for the class which will be called from the hook at location /your-plugin-name/Classes/class.tx_contenthook.php. Filename have to have this pattern class.tx_yourname.php Inside we will have a code with one method which will be called by the hook.
class tx_contenthook {
function displayContent(&$params, &$that){
//content of page from param
$content = $params['pObj']->content;
//your content
$inject = '4747474747';
// inject content on
$content = str_replace('</body>', $inject. '</body>', $content);
// save
$params['pObj']->content = $content;
}
}
And next, we have to call it on the hook. So Let's go to /your-plugin-name/ext_localconf.php and add there these two lines, which makes a magic and handles also caching.
// hook is called after caching
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output'][] = 'EXT:' . $_EXTKEY . '/Classes/class.tx_contenthook.php:&tx_contenthook->displayContent';
// hook is called before caching
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all'][] = 'EXT:'. $_EXTKEY .'/Classes/class.tx_contenthook.php:&tx_contenthook->displayContent';
I hope this will help those who struggling with typo3.
NOTE: I already use wp_dequeue_script or wp_deregister_script but not successfull
Here is the scenario, i make a image slider plugin that use jquery-cycle2 and it work successfully.
There is a user who used a wp theme and in theme there is a jquery-cycle1, now when he install my plugin the jquery-cycle1 and jquery-cycle2 conflicts, when user delete the jquery-cycle1 file all things work fine and perfectly but i don't want to delete file by user.
Now i am trying that when user install my plugin the jquery-cycle1 in theme close or deregister or stop its effect.
I get file from theme successfully
if((file_exists($theme_file_path."/jquery.cycle.all.js"))){
echo "yes";
}
but i have no idea to close jquery-cycle1 file or stop its effect.
Last Option: I have last solution that delete the file from theme but its my last option.
Please any suggestions, help me.
You will have to place an incompatibility notice on your theme.
It is not possible to attempt to detect the existence of script from server side. You are able to detect queued scripts via the word press methods, however, this assumes that the user has not simply linked the file with a <script></script> tag. The file_exists method assume the file is stored on the server itself - it could be linked from a CDN or another server.
Also, whatever methods you use to detect and remove jQuery-Cycle; You are going to break any feature on the site that uses the existing plugin.
Thus, any solution you able to devise would either be extremely complicated, or would not be generalised enough to account for all these possibilities.
You may be able to use the following to prevent loading your script
if (jQuery().cycle) {
// Script already loaded
console.log("Error: Another version of jQuery-Cycle is already loaded!");
} else {
// Load your script
}
but this cannot unload what is already loaded.
There is a simple hack you can do on you end. In the Cycle2 source replace all $.fn.cycle with $.fn.cycle2 and ).cycle( to ).cycle2(.
I am using the source from here http://malsup.github.io/jquery.cycle2.js
After that You can access it like
$("#id").cycle2({option})
Here is a demo http://jsfiddle.net/33g6z79h/
Here i assume that you are not using cycle events http://jquery.malsup.com/cycle2/api/#events
and cycle extra transitions.
If you are using it you can make a fiddle for your cycle2 implementation and i would be glad to help :)
I have a complex php application which I want to include in a typo3 page. I allready found something like this:
page.headerData.20 = PHP_SCRIPT_EXT
page.headerData.20.file = fileadmin/phpScript.inc
...but this inserts the file to template, I just want to show it on one single page instead of content. The application is currently included as an iFrame (same domain) but that's not the best way because the window doesn't resize.
In also think abouth to convert the whole app to typo3 plugin, but I'm very new to typo3 so I don't now how to start. Is there a guide for converting plain php to typo3 plugin?
Hi you can use a simple user extension.
Some where in Typoscript Setup:
includeLibs.mystuff = path_to_file/my_user_class.php
lib.mystuff = USER_INT
lib.mystuff {
userFunc =user_myclassname->main
var_to_pass_at_class=test
}
--
Now you can replace the subpart with dynamic content (depends on your TYPO3 configuration)
+- page.10.subparts.CONTENT<lib.mystuff
--
File: my_user_class.php
<?php
class user_myclassname {
function main($content,$conf){
global $TSFE;
return $conf['var_to_pass_at_class']; //returns test
}
}
Is it possible to include a typoscript file via php?
Normally I would include typoscript with this:
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/1.ts">
But I want to do this just with php and not typoscript. Is that possible?
My Purpose: I want to dynamically load typoscript in my page
This can be achieved by invoking accordant functions at an early stage, e.g. in calling or delegating it in ext_localconf.php. For example, the bookstrap package is loading TypoScript in PHP like this:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig(
'<INCLUDE_TYPOSCRIPT: source="FILE:EXT:' . $_EXTKEY
. '/Configuration/PageTS/Mod/Wizards/newContentElement.txt">'
);
Please consider, that TypoScript is cached before the actual front-end rendering starts. This means, that you should not modify TypoScript if you're plugin class or controller logic has been called already.
May be you need to return a value from the php function and use typoscript conditions for choosing the typoscript file.
You might try the following (if I get you right):
$typoscriptFile .= file_get_contents($someFile);
$parser = t3lib_div::makeInstance('t3lib_TSparser');
$parser->parse($typoscriptFile);
$tsArray = $parser->setup;
I really don't know how well that will play with anything related to global typoscript though.
If you wanted a complete correct parse, you might be able to pull something like this off if you populated a fresh t3lib_TStemplate instance from $GLOBALS['TSFE']->tmpl and than ran the code above. Might work, never tried.
Edit: I thought about a possible solution, but I made another question as it is very specific: see AJAX proxy with PHP, is it possible?
A couple of times I've encountered this problem...
I create sites that have a certain degree of modularity. So, it is possible that there are "components" (think of a rough CMS) which carry their own PHP code, CSS, and JavaScript, all dynamically included. Think about a structure like:
{siteroot}/component/datagrid/datagrid.php
{siteroot}/component/datagrid/js/datagrid.js
{siteroot}/component/datagrid/css/datagrid.css
{siteroot}/component/datagrid/ajax/getsomedata.php
Now, the question is: for JavaScript files, and expecially AJAX calls, how do I make them context-aware with the URLs?
For example, if in datagrid.js I want to call siteroot/component/datagrid/ajax/getsomedata.php with AJAX I should write (with JQuery):
$("#ajax").load("siteroot/component/datagrid/ajax/getsomedata.php");
First problem: siteroot changes on different installations. I've managed that by including a general
var codeBase = <? echo json_encode(Config::$siteRoot); ?>
with PHP on every page, from a Config file that can be easily edited for every installation, so I can do with whatever JavaScript something like:
$("#ajax").load(codeBase + "/component/Datagrid/ajax/getsomedata.php");
What do you think of this approach?
Second problem: but I have PHP functions that return to me also the components folder, or the folder of other components. It would be nice to make the whole URL dynamic. This would account also for changes in the structure of the component if I want.
The only solution I've found is to use a .js.php dynamic Javascript. This is very unelegant, and I have to include all the framework in the JavaScript file, like:
<?php
include "../../libs/framework.php"; // get my functions...
$myUrl = Config::$siteRoot . Framework::getComponentAjaxDir("datagrid") . "/getsomedata.php";
?>
$("#ajax").load(<?=json_encode($myUrl)?>);
Another side effect is that I have to know exactly the include the path for framework.php... I don't want this so hard-codedin my ".js.php" file.
Any smart solutions about that?
As nobody answered in a suitable way, I answer to myself to provide a solution I've found out that can be useful.
The key to my solution is simple:
I create an AJAX proxy at a fixed location in my site structure, so I can use codeBase to reference the proxy from JavaScript
I call this proxy with two parameters: plugin and action, which identify a) the plugin folder in which the "real" ajax is and b) the ajax file to use, along with the other params:
$("#...").load( codeBase + "/main/ajax.php?plugin=Datagrid&action=gettable&otherparams"...)
In ajax.php I sanitize the parameters, and use plugin and action to obtain the "real" ajax file:
{serverRoot}/components/{plugin}/ajax/{action}.php
Then i simply include that file in ajax.php
To be honest your problems are realistic options and aren't that bad practice in general quite frankly.
But let's explore this a little further.
What would be the best approach is for you to have 1 main config.php file which you can then specify modules, i.e. your datagrid etc.
You could store all modules in an array variable like so:
$_SITE_PATH = "/var/www/html/";
$_HTTP_PATH = "http://example.com/";
$_MODULES_PATH = $_SITE_PATH."modules/"
$_MODULES = array(
"datagrid"=>$_MODULES_PATH."datagrid/init.php",
"something_else"=>$_MODULES_PATH."something_else/init.php"
);
Each module would have it's own directory with instantiation init.php so that it would load all it required to get going.
This way you could code as you liked and when you needed something (preferably in the header state) do something like this.
global $_MODULES;
require_once($_MODULES["datagrid"]);
Everything will be easily available as and when required without any variable path issues later down the line.