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

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 ?

Related

shortcode_atts not parsing custom values instead defaults loaded

I' making my own shortcode function and while the call of the shortcode works, and my page query within returns results - it never uses any settings but the defaults as if $att is null.
function test_shortcode( $atts ) {
$filter = shortcode_atts(
array(
'type' => 'major',
'sort' => 'name',
'size' => 'large',
'links' => 'yes',
),
$atts,
'customshortcode'
);
echo 'ATTS:';
print_r($atts);
echo'FILTER';
print_r($filter);
//code to query posts removed
}
add_shortcode( 'customshortcode', 'test_shortcode' );
In the post I can then add..
[customshortcode type:"other" size:"small" sort:"rand" links:"no"]
To see the result
ATTS
Array
(
[0] => type:"other"
[1] => size:"small"
[2] => sort:"rand"
[3] => links:"no"
)
FILTER
Array
(
[type] => major
[sort] => name
[size] => large
[links] => yes
)
and I can see the $atts values are received in the function but the $filter is not updated. I'm expecting both arrays to be the same at the point they are being printed out. As far as I can tell I'm following the coxed formatting here https://codex.wordpress.org/Function_Reference/shortcode_atts
You are passing attributes in the wrong way.
It should use = instead of :.
Please go through https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ to know more about shortcode with parameters.
Try with [customshortcode type="other" size="small" sort="rand" links="no"]

How to use $link for watchdog messages in 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));

URIs with german special characters don't work (error 404) in Zend Framework 2

I want to get a list of cities, where each city name is linked and refers a page for this city:
The links (created in the view script) look like this:
http://project.loc/catalog/Berlin (in the HTML source code url-encoded: Berlin)
http://project.loc/catalog/Erlangen (in the HTML source code url-encoded: Erlangen)
http://project.loc/catalog/Nürnberg (in the HTML source code url-encoded: N%C3%BCrnberg)
"Berlin", "Erlangen" etc. work, but if the city name contains a german special character (ä, ö, ü, Ä, Ö, Ü, or ß) like "Nürnberg", a 404 error occurs:
A 404 error occurred Page not found. The requested URL could not be
matched by routing. No Exception available
Why? And how to get this working?
Thanks in advance!
EDIT:
My router settings:
'router' => array(
'routes' => array(
'catalog' => array(
'type' => 'literal',
'options' => array(
'route' => '/catalog',
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-cities',
),
),
'may_terminate' => true,
'child_routes' => array(
'city' => array(
'type' => 'segment',
'options' => array(
'route' => '/:city',
'constraints' => array(
'city' => '[a-zA-ZäöüÄÖÜß0-9_-]*',
),
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-sports',
),
),
'may_terminate' => true,
'child_routes' => array(
// ...
),
),
),
),
),
),
You need to change your constraints, you can use a regular expression which will match UTF8 characters, something like this:
'/[\p{L}]+/u'
Notice the /u modifier (unicode).
EDIT:
The problem is resolved.
Explanation:
The RegEx Route maches the URIs with preg_match(...) (line 116 or 118 of Zend\Mvc\Router\Http\Regex). In order to mach a string with "special chars" (128+) one must pass the pattern modifier u to preg_match(...). Like this:
$thisRegex = '/catalog/(?<city>[\p{L}]*)';
$regexStr = '(^' . $thisRegex . '$)u'; // <-- here
$path = '/catalog/Nürnberg';
$matches = array();
preg_match($regexStr, $path, $matches);
And since RegEx Route passes a url-enccoded string to preg_match(...), it's furthermode needed to decode the string first:
$thisRegex = '/catalog/(?<city>[\p{L}]*)';
$regexStr = '(^' . $thisRegex . '$)u';
$path = rawurldecode('/catalog/N%C3%BCrnberg');
$matches = array();
preg_match($regexStr, $path, $matches);
These two steps are not provided in the RegEx Route, so that preg_match(...) gets a steing like '/catalog/N%C3%BCrnberg' and tries to mach it to a regex like '/catalog/(?<city>[\\p{L}]*)/u'
The solution is to use a custom RegEx Route. Here is an example.

how to display any message or data in drupal 7 .module file

how to display any message or data in mymodule.module file in drupal 7
i have used following line but it didn't display any thing
drupal_set_message(t('test message'));
aslo i want to display any variable data like for example $data = "hello"
then how to display this variable data in drupal 7
i am new to drupal , so if any one knows please let me know .
i have search a lot , but didn't get anything.
thanks in advance.
I have used folllowing code by creating module in drupal 7
<?php
function form_example_menu() {
$items = array();
$items['form_example/form'] = array(
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_example_form'),
'access arguments' => array('access content'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
function form_example_form($form, &$form_state) {
$form['price'] = array(
'#type' => 'textfield',
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
$form['form_example_form']['#submit'][] = 'form_example_form_submit';
return $form;
}
function form_example_form_validate(&$form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
function form_example_form_submit($form, &$form_state) {
$result = db_insert('test')->fields(array('price' => $form_state['values']['price'],))->execute();
drupal_set_message(t('Your Price was saved'));
}
In above code data is inserted in database , but message didn't displaying .
If you know , what is problem please let me know , i have search a lot for this problem
. Thanks in advance.
Here's the proper way to display some data in the message:
drupal_set_message(t('test message: !data', array('!data' => $data)));
As for the message not displaying, if other messages do display on your site, it sounds like your function isn't executing. I'd need more info on what you're attempting to do (including the code involved) to debug it.
The function watchdog is also available in Drupal 7
Here is an exemple of how you can use it:
watchdog('MyModule', '<pre>'. print_r($variable, TRUE) .'</pre>', array(), WATCHDOG_INFO, NULL);
You can watch the log in Reports -> Recent log messages (admin/reports/dblog) if the core module "Database logging" is activated.

How can I add subtitles to movie using flowplayer module?

I'm using flowplayer module to play videos on my site. I would like to add sutitles to some of the movies. How can I do it in Drupal?
I tried to add them like this:
$flowplayer = array(
'clip' => array(
'url' => str_replace('sites/default/files/videos', 'http://www.mysite.com/sites/default/files/videos', $node->field_video[0]['filepath']),
'captionURL' => str_replace('sites/default/files/videos', 'http://www.mysite.com/sites/default/files/videos', $node->field_caption[0]['filepath'])
),
....
Then the output is:
<param value="config={"clip":{"url":"http://www.mysite.com/sites/default/files/videos/video.flv","captionURL":"http://www.mysite.com/sites/default/files/videos/subtitles.srt","scaling":"fit"},...
However it says no stream found. When I erase "clip", the video is found.But how can I add subtitles?
I wonder if I need some plugin or what is wrong in my code?
Thanks.
Are you going to have more than one video? You might try using the playlist param which has support for titles. I'm not sure how you would display them (i'm doing something very similar at the moment, hopefully this is helpful). I'm using the Flowplayer API module but the js syntax should be similar.
$player = theme('flowplayer',array(
'clip' => array(
'duration' => 5,
'autoPlay' => true
),
'playlist' => array(
array('url' =>'http://localhost/episode_18.mp4','title' => 'Episode 18'),
array('url' =>'http://localhost/episode_19.mp4','title' => 'Episode 19'),
array('url' =>'http://localhost/6.jpg','title' => 'Image')
),
'plugins' => array(
'controls' => array(
'playlist' => true
)
)
)
);
return $player;

Resources