Get base url in custom not.html.twig in Drupal 8 - drupal-8

I made a custom node to show content type, say: node--blog.html.twig. Now I want to call base URL, but unable to do so. I tried code like below, but nothing worked:
Read More
Apply
Apply
How can I get base URL? I think there must be some variable like Drupal 7.

You can add the following to your .theme file:
function YOUR_THEME_preprocess_node(&$variables) {
$variables['someVarName'] = $GLOBALS['base_url'];
}
And then access it in your node template like this:
{{ someVarName }}

just in your template in YOUR_THEME.theme, add the following snippet.
function YOUR_THEME_NAME_preprocess_node(&$variables) {
$variables['base_path'] =base_path();
}

Related

Can I make a WooCommerce shortcode from variable template part?

I want to create a shortcode that will show the template parts mytheme/woocommerce/single-product/add-to-cart/variable.php on my Woocommerce product page where I want. My target is to set my page fully from WP Barkery on Long Description and not call this template part from templates files.
I made :
function shortcode_add_to_cart_variations(){
wc_get_template_part( 'single-product/add-to-cart/variable' );
}
add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');
The template part is called but the variations it's not shown because $available_variations = NULL : https://github.com/woocommerce/woocommerce/blob/3.5.0/templates/single-product/add-to-cart/variable.php#L26
How can I make my shortcode and show variations where I want on my product page (for example in my Revolution Slider)?
I found myself the answer.
I only need to use the core woocommerce function woocommerce_variable_add_to_cart()
Detail here: http://hookr.io/functions/woocommerce_variable_add_to_cart/
Add this in my functions.php (child theme) :
function shortcode_add_to_cart_variations(){
woocommerce_variable_add_to_cart();
}
add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');

Access to description attribute of an Property template construct parameter

I want to use a parameterized template construct to access the attributes of a Property object to generate some output in a word document. When I use the template with some basic types (e.g. Integer or String) it works fine but when I've tried to use a Property object, I can only access the "name" attribute but not e.g. the description attribute. Using the unit test recursiveEObject-template as a starting point, my template looks like this:
{ m:template mytest(element : ecore::ENamedElement) }
{ m: element.description }
{ m:endtemplate }
And I get the following error message:
{ m:element. <---Feature description not found in EClass ENamedElement description }
I assume that the type of my parameter is just wrong and I need to change it or cast it to some derived Property child class of ENamedElement but I did not find the correct class. Any suggestions?
Kind regards
Christian
Yes you need to change the type of the parameter element to the type of the object that is holding the description feature. You will also need to import the EPackage containing the EClass.
For instance if I want to use the description of a Capella object:
{ m:template mytest(element : capellacore::CapellaElement) }
{ m: element.description }
{ m:endtemplate }
then import the following nsURI (may change according to the version of Capella):
http://www.polarsys.org/capella/core/core/1.4.0
using the template property wizard

golang template.Execute and struct embedding

I have a little website project written go where you can store links, and I ran into a problem :
The website has many different pages which show different information so you need to pass template.Execute a different kind of a struct. But every page also needs info like username and tags, which are displayed in sidebar. I tried to make something like this instead of just making completely new struct type for each page.
http://play.golang.org/p/VNfD6i8p_N
type Page interface {
Name() string
}
type GeneralPage struct {
PageName string
}
func (s GeneralPage) Name() string {
return s.PageName
}
type PageRoot struct {
Page
Tags []string
IsLoggedIn bool
Username string
}
type ListPage struct {
Page
Links []Link
IsTagPage bool
Tag string
}
type GalleryPage struct {
Page
Image Link
Next int
Previous int
}
But I get an error when I execute the template: "fp.tmpl" at <.Links>: can't evaluate field Links in type main.Page
The part of the template where the error occurs:
{{with .Page}}
{{range .Links}}
<tr>
<td>{{if .IsImage}}<img src="{{.Url}}" />{{end}}</td>
<td>{{.Name}}</td>
<td>{{.Url}}</td>
<td>{{.TagsString}}</td>
</tr>
{{end}}
{{end}}
And {{.Name}} doesn't work. (It's the function embedded from GeneralPage)
You're embeding the Page interface, but what you need is GeneralPage.
Maybe you can use a map[string]interface{} to store your data (and then check if not-nil in your template), it easier.
But you can share the main layout and just change the detail (like a master page).
Look at http://golang.org/pkg/text/template/#example_Template_share

tt_address: add categorys of address to the template

Is it somehow possible to add the sub-group of a cetrain group the address is assigned to the html output?
In the template I have ###MAINGROUP### and ###GROUPLIST###. I can't use maingroup, cause it's not the case that the group I need is always the maingroup. And with the grouplist I can't say which group is the sub-group of the one group.
Anyone have an idea how I could do it?
And in addition to that I also need the value of a self created field in the tt_address table.
Edit:
I try it like #lorenz say. What I have so far:
ext_localconf.php:
<?php
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_address']['extraItemMarkerHook'][]
='EXT:txnextaddresssort/class.tx_next_address_sort_addmarkers.php:tx_next_address_sort_addmarkers';
class.tx_next_address_sort_addmarkers.php:
<?php
class tx_next_address_sort_addmarkers {
function extraItemMarkerProcessor(&$markerArray, &$address, &$lConf,
&$pObj) {
$lcObj = t3lib_div::makeInstance('tslib_cObj');
$lcObj->data = $address;
$markerArray['###SORTBEREICH###'] =
$lcObj->stdWrap($address['tx_nextaddresssort_sort_bereich'],
$lConf['tx_nextaddresssort_sort_bereich.']);
}
}
Extentionkey: next_address_sort
All I get is a blank screen, but no errors in apache log
No, there is no possibility to do that.
Yet you can write a custom extension that integrates the extraItemMarkerProcessorhook in tt_address. In ext_localconf.php, add:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_address']['extraItemMarkerHook'][] ='EXT:myextension/class.tx_myextension_filename.php:tx_myextension_classname';
Then add a file class.tx_myextension_filename.php to your extension.:
class tx_myextension_classname {
public function extraItemMarkerProcessor(&$markerArray, &$address, &$lConf, &$pObj) {
$lcObj = t3lib_div::makeInstance('tslib_cObj');
$lcObj->data = $address;
$markerArray['###MYFIELD###'] = $lcObj->stdWrap($address['myfieldlikeindatabase'], $lConf['myfieldlikeindatabase.']);
return $markerArray;
}
}
This would be an example for getting a field that is in the tt_address table and adding it to the markers so they can be used in a template. It is also stdWrap enabled.
Now, instead of getting a field, you should replace $address['myfieldlikeindatabase'] with a variable that contains the information you need. To receive the data, you can use the TYPO3 database API functions ($GLOBALS['TYPO3_DB']).

Why is php_template_preprocess_page function not called in Drupal 6x?

From another forum I found the following example:
"I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
"
This seems like the logical way to do it and I did this, but the phptemplate_preprocess_page function is never called ... any suggestions?
I figured it out for myself from a Drupal Support Theme Development page:
"Maybe this helps
leahcim.2707 - May 29, 2008 - 05:40
I was trying to get the same thing done and for me this works, but I'm not sure if it is the correct way as I'm still new to Drupal:
in "template.php" I added the following function:
function phptemplate_preprocess_page(&$vars)
{
$css = $vars['css'];
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
$vars['styles'] = drupal_get_css($css);
}
I think after adding the function you need to go to /admin/build/themes so that Drupal recognises the function."
The part in bold is what did the trick ... you have to re-save the configuration so it recognizes that you've added a new function to the template.