I'm trying to set the scope for a translation to 'parent' while using the $t() function, but I'm unable...
I know it is posible to set it up when using the tag syntax...
<i18n-t :keypath="label" scope="parent"> </i18n-t>
Is there any way I can do the same with the $t function?
Related
I am using Wordpress Timber which uses TWIG as the Templating Enginge. I am trying to get my array shifted, so i can then loop over it with TWIG.
array('Anna','Ben','Caroline','Emma','Daniel')
Which should end up as this:
array('Ben','Caroline','Emma','Daniel', 'Anna')
Is there a way with Timber to do this, or must I do it via PHP?
Simply solving it with PHP as DarkBee suggested.
I have defined a function to return an array and need to pass this array as a parameter to a template (node--contenttype.tpl.php).
function mymodule_preprocess_page(&$variables) {{
$variables['tdata'] = $myArray;
}
In node--contenttype.tpl.php
print $tdata;
I tried to call the variable $tdata, but the error shows
Notice: Undefined variable: tdata in include()...
The same error happens if I define a variable in template.php and call from node--contenttype.tpl.php.
Oops... I think I see your problem now.
You should be using hook_preprocess_node(), not hook_preprocess_page() if you want your variables to be available in your node template files.
Make sure you clear cache after you make the change too!
Let us know if that fixed your issue...
I have a drupal template. I want to know what are the names of the active variables using php..
How do I achieve that ?
If you're just looking to get which variables have been defined, you can use PHP's function get_defined_vars:
print_r(get_defined_vars());
http://php.net/manual/en/function.get-defined-vars.php
If your after Drupals globals, here they are...
http://api.drupal.org/api/drupal/globals/7
I'm building a module that manages ad units in the form of blocks that all need to be aware of each other and pass information around.
Thus I need to find a simple hook or other function to get a listing of every block that will be used on the page, so I can be sure to know the entire list of ad units on the page.
I've tried hook_block_list_alter() but this seems to return the ENTIRE list of blocks that exist in my Drupal install, with no designation of which blocks are actually going to be rendered or not on this page.
So, now what do I do?
I think it's got something to do with the order hook_block_list_alter() is called in but I'm not entirely sure. Either way the following code will get you a list of all blocks in the current page context. Be sure not to put this in a hook_block_list_alter() function or you'll get an exception caused by infinite nesting.
global $theme;
$all_regions = system_region_list($theme);
$blocks = array();
foreach (array_keys($all_regions) as $region) {
$blocks += block_list($region);
}
If you only need the blocks in a particular region you can ditch most of the code above and just go with:
$blocks = block_list('region_name');
I ran into the same problem when calling block_list('content'). Looking at the code for this function I found it calls two other functions, _block_load_blocks() and _block_render_blocks(). The problem seems to occur in _block_render_blocks() as the display text is not added to the content object. This is different from the other block objects that pass through the function.
To get around this, instead of calling block_list(), I called _block_load_blocks() directly. This returns an array of blocks grouped by region, bypassing the _block_render_blocks() call.
Now we can check for blocks in the content region without the content text disappearing. Huzzar!
I had a similar requirement when implementing an adserver (DFP). My solution was to define an array as a global variable, and include php code in each ad unit block that added a new element to the array.
Then, once all blocks on the page have been executed, you can simply access the global variable to see which ad blocks were called. Because the code to build the list of blocks being called is part of each block, it doesn't matter whether the blocks are displayed in a region, in a panel, or anywhere else.
In my case, I wanted to use the information to add scripts to the <head> section that reference only the add units from the blocks being placed. My complete solution was as follows:
1) Implement hook init to create a global variable in which to store information about which blocks are being displayed (you need to create a custom module to contain this code):
YOURMODULE_custom_init() {
$GLOBALS['dfp-ads'] = array();
}
2) Enable the core php module
3) Add php code at the end of each ad block to add a row to the array created in step 1
<?php
$GLOBALS['dfp-ads']['AD_OR_BLOCK_NAME_GOES_HERE']="AD SPECIFIC SCRIPT GOES HERE";
?>
4) Implement THEME_preprocess_html in my template.php file to access the global variable, build the script, and add the script to <head> section with a call to drupal_add_html_head
function YOURTHEME_preprocess_html(&$vars) {
$inline_script = LOGIC TO ACCESS $GLOBALS['dfp-ads'] AND BUILD SCRIPT GOES HERE;
$element = array(
'#type' => 'markup',
'#markup' => $inline_script,
);
drupal_add_html_head($element, 'google-dfp');
}
It sounds from your description that you don't need the list of ad blocks to build javascript for the head section, but instead want to use the information to modify the contents of the blocks themselves.
In that case insteaad of THEME_preprocess_html, you could try hook_page_alter(&page)
The api page for that hook states that individual "Blocks may be referenced by their module/delta pair within a region:"
// The login block in the first sidebar region.
$page['sidebar_first']['user_login']['#block'];
Hope that helps someone!
I need to create a new custom formatter(using the module custom formatter) to replace some template code. So, for some fields, When I add a new custom formatter(field type: text) I need to print the title and the body. I tried to use $node->title but it doesn't work...
How can I do this? Probably using elements? And if yes...how?
Thanks in advance,
Regards,
Michele
Field formatters relate to the field that they are used for, it it's impossible to answer your question without know what field you are using (and it's contents).
To debug this, you could use the devel module and a bit of code. If you in your formatter write.
dpm(get_defined_vars());
this will give you a pretty printed list of all variables you have available. That should help you inspect and figure out how you get to what you need.
Custom formatters get's passed $element, if you do a dpm of $element (dpm($element) - If you have the Devel module installed) you will see the entire array, and notive that the $node object is passed as $element['#node'].
So with that said, to get to the node title you would use $element['#node']->title.
Please also not that it does say this on the help text of the custom formatters UI.