How to use $link for watchdog messages in drupal? - drupal

I tried using l()
watchdog('my_module', 'My message for /admin/reports/dblog', WATCHDOG_NOTICE,
$link = l(t('A hyperlink'),
'/node/386/group?realname=&uid=&state=All&order=created&sort=desc',
array('attributes'=>array('target'=>'blank'))) );
but the hyperlink is encoded "node/386/group%3Frealname%3D%26uid%3D%26state%3DAll%26order%3Dcreated%26sort%3Ddesc" which I understand is because l() is supposed to generate urls from drupal paths.
Can I decode it before it's rendered or what's the proper way of inserting that hyperlink?

You should use query key for generating the path as shown below
<?php
global $base_url;
print l(
'',
$base_url . $node_url,
array(
'attributes' => array(
'id' => 'my-id',
'class' => 'my-class'
),
'query' => array(
'foo' => 'bar'
),
'fragment' => 'refresh',
'html' => TRUE
)
);
?>
This will generate a link like
<img src="http://www.example.com/files/image.jpg"/>
Source: https://api.drupal.org/api/drupal/includes%21common.inc/function/l/7.x

Just use this:
$link = l(t('A hyperlink'), '/node/386/group', array('attributes'=>array('target'=>'blank'))));
watchdog('my_module', 'Link !field_link.', array('!field_link' => $link));

Related

Array index is printed out when using 'add_args' in WooCommerce/Wordpress paginate_links()

I have been trying to scour the internet for the answer, but I didn't find it.
I have created a custom filter for WooCommerce archive pages, which is being loaded by ajax. All works fine, except for pagination.
When refreshing the page and having several parameters with same name (e.g. example.com/category/filter_fabric=cotton&filter_fabric=polyester), the pagination links only take into account the last parameter (in this case, polyester). I was able to get past this issue by going into loop/pagination.php in woocommerce and adding the following:
`//Get parameters from URL
$url_parameters = $_SERVER['QUERY_STRING'];
$paginate_add_args = proper_parse_str($url_parameters);
//#TODO: the "&" that are supposed to come from array within array/nested array, i.e. filter_fabric are becoming "%5B0%5D" instead of "&"`
Then I passed that argument in the 'add_args' of paginate_links() (also in loop/pagination.php) as following:
`<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array( // WPCS: XSS ok.
'base' => $base,
'format' => $format,
'add_args' => $paginate_add_args,
'current' => max( 1, $current ),
'total' => $total,
'prev_text' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>',
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
) ) );
?>
</nav>
`
This solved the issue in that all instances of my parameter are being loaded in the link, but the problem is that since my filter_fabric parameter becomes an array, it prints out the array index in the URL, as so:
example.com/category/page/2/filter_fabric%5B0%5D=cotton&filter_fabric%5B1%5D=polyester
Does anyone have any idea of how to solve this issue?
Anything would help greatly!
I have tried using the methods mentioned above.
UPDATE/EDIT #1
So I think I sort of managed to fix the pagination link by doing this method:
//Get parameters from URL
$url_parameters = $_SERVER['QUERY_STRING'];
$paginate_add_args = proper_parse_str($url_parameters);
# Function that turns the array items into string, like this
# e.g. array(filter_fabric => array(cotton, polyester, linen))
# into:
# array(filter_fabric => 'cotton&filter_fabric=polyester&filter_fabric=linen')
$walk = function(&$item, $key) {
if (is_array($item)) {
$item = implode('&' . $key . '=', $item);
}
};
array_walk($paginate_add_args, $walk);
//var_dump($array); //Outputs: 'cotton&filter_fabric=polyester'
This actually renders the pagination links correctly, with the link being:
'example.com/category/page/2/?filter_fabric=cotton&filter_fabric=polyester'
But...
When I actually click that link, the page loads with only the last parameter. It almost seems as if it redirects with only the last parameter, that is, into this:
'example.com/category/page/2/?filter_fabric=polyester'
Anyone have any idea why this happens?
UPDATE/EDIT #2
I think I managed to solve this issue by adding a remove_action('template_redirect', 'redirect_canonical')
Please see the answer in this link:
Adding multiple values to a query variable in WordPress
You can modify the output of the paginate_links() function to remove the array indices by encoding the parameters before adding them to the pagination links. You can do this using http_build_query() function. Here's how:
<nav class="woocommerce-pagination">
<?php
$paginate_add_args = http_build_query($paginate_add_args);
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array( // WPCS: XSS ok.
'base' => $base,
'format' => $format,
'add_args' => $paginate_add_args,
'current' => max( 1, $current ),
'total' => $total,
'prev_text' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>',
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
) ) );
?>
This will encode the URL parameters in the query string format, so it will not show the array indices in the URL.

Visual composer custom loop shortcode

I need to use custom loop from visual composer:
if( function_exists('vc_map') ) {
vc_map( array(
'base' => 'minimag_popular_post_custom',
'name' => esc_html__( 'Popular Post Custom', "minimag-toolkit" ),
'class' => '',
"category" => esc_html__("Minimag Theme", "minimag-toolkit"),
'params' => array(
array(
// this param
"type" => "loop",
"heading" => esc_html__("Display Custom Loop", "minimag-toolkit"),
"param_name" => "custom_loop",
)
),
) );
}
In past I've used vc_link which had the proper function to retrieve the value in the correct form: vc_build_link($href).
There is some function to extract the data from loop parameter? I've looked in the reference but I've not find nothing.
Here an example of the output that I need to parse:
size:8|order_by:date|order:DESC|post_type:post|categories:32,5|by_id:1537,1673
I need to have something like:
$myVar['size'] = 8;
$myVar['order_by'] = 'date';
$myVar['order'] = 'DESC';
$myVar['post_type'] = 'post';
$myVar['categories'] = array(32,5);
$myVar['by_id'] = array(1537,1673);
tested and working :)
list($args, $wp_query) = vc_build_loop_query($atts["custom_loop"]);
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
}
wp_reset_postdata();
If you know your query I like to create a shotcode in the functions.php of my child-theme to create that. You can pass parameters to create different output and you can use such a shortcode everywhere in your site.

Drupal 7 print l() query string showing in special characters

In D7, I want to pass the query string as like www.sitename/page-name?query_string[]=8, in hyperlink.
For this I written the below code:
<?php print l('Hello','page-name', array('html' => TRUE,'attributes' => array('class' => 'example-class',), 'query' => array('query_string[]=8' => ''))); ?>
But it shows like : /page-name?query_string%5B%5D%3D8
I tried to change your syntaxe according documentation page but always same result :
l('Hello','page-name',
array('html' => TRUE,
'attributes' => array('class' => 'example-class',),
'query' => array('query_string' => array('8', '9'))
)
);
display :
Hello
So when you try to get values , it's works or not ?

Visual Composer; custom elements won't load textarea_html/'content'

Currently using WordPress 4.4.2, I'm in the process of developing some custom Visual Composer elements.
It seems (however), that whenever I want to use a textarea_html param (So the end-user can use the wysiwyg editor) I cannot seem to grab it's contents when rendering the template.
Contents of 'titled_content_box.php'
// called during vc_before_init
function integrate_titled_content_box(){
register_titled_content_box();
add_shortcode( 'titled_content_box', 'titled_content_box_func');
}
//Mapping of titled-contentbox
function register_titled_content_box(){
vc_map( array(
"name" => __( "Content box with Title", "mytheme"),
"base" => "titled_content_box",
"class" => "",
"category" => "Content",
"params" => array(
array(
"type" => "textfield",
"holder" => "div",
"class" => "",
"heading" => __( "Title", "mytheme"),
"param_name" => "title",
"value" => __("Box title", "mytheme"),
"description" => __("The title covering the content box", "mytheme")
),
array(
"type" => "textarea_html",
"holder" => "div",
"class" => "",
"heading" => __( "Description", "mytheme"),
"param_name" => "content",
"value" => '<p>Placeholder</p>',
"description" => __("The content", "mytheme")
)
)
));
}
// Setting values where necessary and fetching the template
function titled_content_box_func( $atts ){
extract( shortcode_atts( array(
'title' => 'title',
'content' => 'content'
), $atts) );
return include_vc_template('titled_content_box.php', $atts);
}
add_action ( 'vc_before_init', 'integrate_titled_content_box');
contents of the template used at the return statement:
<div class="titled-content-box">
<div class="title"><span><?php echo $atts['title']; ?></span></div>
<div class="content">
<?php echo $atts['content']; ?>
</div>
</div>
Does anyone know why my content-field is not loaded? The element itself is loaded, I can use it in VC... even the Title will be loaded and if I replace the field with a textbox, all still works fine and dandy...
My end-user wants to format his content and is not able to use html formatting.
The only function not included is the 'include_vc_template' function, but all that does is pretty much fetching a string-defined php-file on a predetermined location and injects the $atts array. In all other elements I've made that works perfectly fine.
However, for completeness i'll include it here;
function include_vc_template($template, $atts){
if(is_file(__DIR__.'/vc_templates/'.$template)){
ob_start();
include __DIR__.'/vc_templates/'.$template;
return ob_get_clean();
}
return false;
}
As this is a project i'm working on in my spare-time I can't help but to feel annoyed by a functionality not working as-documented... Most searches I've done simply referred my back to wpbakery's knowledge base page for vc_map()... Any pointers at all would be great!
Update you template callback function to:
function titled_content_box_func( $atts, $content ) {
$atts = shortcode_atts( array(
'title' => 'title',
), $atts) );
$atts['content'] = $content;
return include_vc_template('titled_content_box.php', $atts);
}
Update: 07-11-2016:
I would recommend using also vc_map_get_attributes function, which also combines all default values with your provided values.
As you can see in previous PHP function we used title attribute with default value title which is not compatible with the default value from vc_map (__("Box title", "mytheme")) and actually this is an error.
To avoid that errors please use vc_map_get_attributes function for $atts variable.
function titled_content_box_func( $atts, $content, $tag ) {
$atts = vc_map_get_attributes($tag, $atts);
$atts['content'] = $content;
return include_vc_template('titled_content_box.php', $atts);
}
The content is outputted but not a 100% correct, because it also mixes up the HMTL and creates extra paragraphs.
correct code is:
function titled_content_box_func( $atts, $content = null, $tag ) {
$atts = shortcode_atts( array(
'title' => 'title',
), $atts) );
$content = wpb_js_remove_wpautop($content, true); // fix unclosed/unwanted paragraph tags in $content
return include_vc_template('titled_content_box.php', $atts);
}

Can their be actioncontroller and no view for button in yii

I have a the register button which I have created in cgridview I need to know whether can we have action in controller buuton and no view for that particular action for that button in yii
view user
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->countregister($_GET['id']),
'enablePagination' => true,
'filter'=>$model,
'columns'=>array(
'name',
'email',
array(
'class'=>'CButtonColumn',
'template'=>'{Register}{update}{view}',
'buttons'=>array(
'Register'=>array(
'label'=>'Register',
.'url'=>Yii::app()->createUrl('register/create',array( 'email'=>$data->email) )
)
),
),
),
)); ?>
controller user
public function actionCreate($email)
{
$model=$this->loadModel($email);
if($_SESSION['userid'])
{
$this->redirect('product/create',array( //line 1
'model'=>$model,'id'=>$model->productid,
));
}
//$this->redirect(array('display','id'=>$model->productid));
$this->redirect(array('user/login'));
}
i don get error but then the below line not the url iam looking
/localhost/test/index.php/register/create/product/create
it should be
/localhost/test/index.php/product/create/id/1
i think there's something wrong in line 1
Please let me know how do i resolve this
change this
$this->redirect('product/create',array( //line 1
'model'=>$model,'id'=>$model->productid,
to
$this->redirect(Yii::app()->createUrl('product/create',array( //line 1
'id'=>$model->productid))
You can parametrise your individual CButtonColumn instances:
array(
'class' => 'CButtonColumn',
'template' => '{Register}{view}{update}',
'buttons' => array(
'Register' => array(
'label' => 'Register',
'url'=>Yii::app()->createUrl('register/create', array('email' => $data->email)),
'visible'=>'$data->entries == 0',
),
'view' => array(
'visible'=>'$data->entries > 0',
)
)
),
But to answer you question after you updated your response (I was already typing it out)
You can use the raw type:
'type'=>'raw',
and the url becomes something like :
'url'=>'Yii::app()->createUrl("register/create",array( "email"=>$data->email) )'
thx to #let-me-see
source: http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/#hh2
this works
$this->redirect(array('product/create','id'=>$model->productid));

Resources