TYPO3 - Get the current language in an external php file - typo3

I am a beginner in TYPO3 :) and I want to get the current language in an external php file.
How can I do that?
Thanks a lot.

If you've got an instance of the TSFE, you can access the sys_language_uid via $GLOBALS['TSFE']->sys_language_uid

For the V9, $GLOBALS['TSFE']->sys_language_uid is deprecated, it recommanded to use the Language Aspect.
Example :
$languageAspect = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class)->getAspect('language');
$sys_language_uid = $languageAspect->getId();

TYPO3 9+
$context = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class);
// The requested language of the current page as integer (uid)
$currentLanguageUid = $context->getPropertyFromAspect('language', 'id');

It is always best way to get Current language:
$GLOBALS['TSFE']->sys_language_uid
or
$GLOBALS['TSFE']->sys_language_content
based on that you get current language id and you can give condition for that.
Get Current Language in Typo3 10.x version.
$context = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class);
$langId = $context->getPropertyFromAspect('language', 'id');

Normally L is always be used as a language parameter in typo3. $_GET['L']

In case you need detailed language attributes
$request = $GLOBALS['TYPO3_REQUEST'];
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(
$request->getAttribute('language')
);
Further get attributes like i.e. Path of language
$path = $request->getAttribute('language')->getBase()->getPath();
Reference : https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/SiteHandling/AccessingSiteConfiguration.html

Related

Typo3 RealURL: show page id

I want to use RealUrl extension in my typo3 site.
What I want to do:
change the usual url
"http://mysite.it/pag/index.php?id=1"
into
"http://mysite.it/pag/1"
(I wanto to hide "index.php?id=")
Is it possible to do it using RealUrl? Can someone help me? (conf example)
Thank you
In this way you probably do not need realurl.
Any url of type https://mydomain.tld/?{pageid} will be mapped by TYPO3 core to index.php?id={pageid} so you need just a small htaccess rewrite to insert the questionmark.
It is just that links are not generated in this short form. But that could be done by a lastplace replacement (do a stdWrap.replacement on the PAGEobject):
page = PAGE
page {
10 = ...
:
stdWrap.replacement {
1.search = index.php?id=
1.replace =
}
}
Your request reminds to the core-extension simulate static, but that might be obsolete and the pattern differs from your request. It was https://mydomain.tld/{pagetitle}.{pageid}[.{pagetype}].html. For the default-pagetype 0 the pagetype does not need to render.

Create a link in backend to a frontend page

I would link to create a link to a frontend page inside a backend module.
Using:
<f:link.page pageUid="40" >Link</f:link.page>
Doesn't work. It generates a link to the current backend module.
Any solution ?
Since TYPO3 v9 you can use the following lines of code:
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Routing\PageRouter;
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$site = $siteFinder->getSiteByPageId($pageId);
$router = $site->getRouter();
$frontendUrl = (string) $router->generateUri($pageId);
Thanks to Benni Mack # TYPO3Camp Mitteldeutschland
This is a very long story but the gist is: it is not possible to create FE links in BE/CLI context without a lot of workarounds and dummy objects/values.
If possible you should use solutions like pagepath which generate URLs for an eID script which performs the actual URL generation on demand.

Get website name using TypoScript?

Is there a way to get the website name using TypoScript?
In my TypoScript template I am using
data = page : name
to get the name of the current page I am on e.g. About.
I need to get the webiste name as well, so I have tried
data = site : name
but this is not correct and I can't find the correct data name for the website name in the documentation.
NOTE: by website name I mean the Sitename entered in the template settings.
I found this solution on http://www.sk-typo3.de/Seitentitel-aendern.58.0.html from Waldgeist. It reads from the TSFE without a DB query.
lib.siteTitle = TEXT
lib.siteTitle{
data = TSFE:tmpl|sitetitle
attribute = property
}
With line below you need to know used uid of the TypoScript template entry from table sys_template, where uid=1 in example below:
data = DB:sys_template:1:title
Or maybe you want use the sitename as set for your installation:
data = GLOBAL:TYPO3_CONF_VARS|SYS|sitename

SilverStripe 3.1 - Wrong $BaseDir after porting to XAMPP

after porting my fully working SS3.1 Page from my Webserver http://mydomain.de to my locally installed XAMPP http://intranet/silverstripe I've got the problem that the Base Directory is now wrong in SS. It's the old one from the webserver / but it need to be /silverstripe
I already changed it in the .htaccess so that mod_rewrite works but $BaseDir returns / and if I try to use the SS Sitesearch than I get redirected to home/SearchForm?Search= instead of silverstripe/home/SearchForm?Search= after submitting the form.
Can someone please help me to fix this problem.
Thank you in advance
EDIT:
I just call $SiteSearch in my Template.
But the function is modified in my Page.php to search also through a dataobject.
public function results($data, $form){
$results = $form->getResults();
$query = htmlspecialchars($data['Search'], ENT_QUOTES,'UTF-8');
$objects = ListingObject::get()->where("MATCH (Title, Link, Company, Category) AGAINST ('$query' IN BOOLEAN MODE)");
$results->merge($objects);
$data['Results'] = $results;
$data['Title'] = _t('SearchForm.SearchResults', 'Search Results');
$data['Query'] = $query;
return $this->customise($data)->renderWith(array('Page_results','Page'));
}
without this code it also doesn't work
SOLUTION:
I'm sry. I found the Problem. I didn't call $SiteSearch, because when I created the Page, I had to edit the form, so I hardcoded it... because of that, the submitted url is wrong now. I'm so sorry!
$BaseDir should be $BaseHref in your template.
You might have to set an alternate base url in the SS config. Add this to your config.yml:
Director:
alternate_base_url: '/silverstripe'

Typo3 eID, how to access config

I've just created an eID in Typo3. I can't figure out how to access the config data for my plugin from the Typo3 instance.
I've tried the code from the link but it doesn't want to work. I keep getting an exception "No TypoScript template found! " on the call " $TSFE->getConfigArray(); "
http://lists.typo3.org/pipermail/typo3-dev/2006-December/021392.html
Any ideas?
Thanks.
In eID mode, only a small part of the regular TYPO3 frontend is loaded. Unfortunately TypoScript is not loaded. To still access the TypoScript configuration you need manually load the components it needs to do so. Unfortunately this can be a bit of a pain in the butt. So in some cases it might be easier to just load a page containing a single plugin that doesn't contain anything else (without headers etc.).
If you do want to load the TypoScript templates yourself, you can try something like the following:
require_once(PATH_tslib.'class.tslib_fe.php');
require_once(PATH_t3lib.'class.t3lib_userauth.php' );
require_once(PATH_tslib.'class.tslib_feuserauth.php');
require_once(PATH_t3lib.'class.t3lib_cs.php');
require_once(PATH_tslib.'class.tslib_content.php') ;
require_once(PATH_t3lib.'class.t3lib_tstemplate.php');
require_once(PATH_t3lib.'class.t3lib_page.php');
$TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
$id = isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:0;
$GLOBALS['TSFE'] = new $TSFEclassName($TYPO3_CONF_VARS, $id, '0', 1, '','','','');
$GLOBALS['TSFE']->connectToMySQL();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->fetch_the_id();
$GLOBALS['TSFE']->getPageAndRootline();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->tmpl->getFileName_backPath = PATH_site;
$GLOBALS['TSFE']->forceTemplateParsing = 1;
$GLOBALS['TSFE']->getConfigArray();
$cObj = t3lib_div::makeInstance('tslib_cObj');
This initializes the TSFE and cObj but is also used to load and parse the TypoScript templates. You might need to make some modifications to (probably kick some things out)
The code came from one of the comments on the following blog post: http://sebastiaandejonge.com/blog/articles/2010/september/21/bringing-ajax-to-your-frontend-plugins/
Good luck!