Extend link field output on Drupal Twig Template - drupal

I'm trying to figure out how to extend a common Link Field in Drupal 8 with an HTML entity (like »).
My first try was a preprocess function for the field. Unfortunately I didn't manage to set the html option of the link to true
Here's how I tried it
function MYTHEME_preprocess_field(&$variables) {
if ($variables['element']['#field_name'] == 'field_slideshow_link'){
foreach ($variables['items'] as $idx => $item) {
$variables['items'][$idx]['content']['#title'] = $variables['items'][$idx]['content']['#title'] . " <span>»</span>";
$variables['items'][$idx]['content']['#url']->setOption('html', true);
}
}
}
This didn't work. So the only solution I came up with was to manually generate a Link within the template. With just doesn't feel right. Here's how I did it
{{ node.field_slideshow_link.0.title }} <span>»</span>
Has anyone an idea how to solve this problem more elegantly?

Had trouble with this too. Found the following solution
$url = [
'#title' => new FormattableMarkup('&#text;', ['#text' => t('raquo')]);
//The rest
];
Did this with html elements like span so not sure if it will work with this. Let me know!

Related

Wordpress : qTranslate X language switcher with language code

I'm trying to make a basic language switcher with qTranslate X, something like :
FR | EN
There's a function to achieve that : qtranxf_generateLanguageSelectCode('text'); but it can only accept 'text', 'image' or 'both', so it doesn't fit to my needs : 'text' is the full name of the language.
How can I just show the language code ? Any idea to make a filter to do that ?
Try to add following script below translate code.
echo qtranxf_generateLanguageSelectCode('text');
<script>jQuery(document).ready(function(){ jQuery('.lang-en a span').html('EN'); jQuery('.lang-fr a span').html('FR'); })</script>
Serverside Solution:
Please find below Code which modify language name to language code without change in plugin code and you can do it by word press filter.
Paste below code into function.php file.
add_filter('template_include','start_buffer_EN',1);
function start_buffer_EN($template) {
ob_start('end_buffer_EN');
return $template;
}
function end_buffer_EN($buffer) {
return str_replace('<span>English</span>','<span>EN</span>',$buffer);
}
add_filter('template_include','start_buffer_FR',1);
function start_buffer_FR($template) {
ob_start('end_buffer_FR');
return $template;
}
function end_buffer_FR($buffer) {
return str_replace('<span>Français</span>','<span>FR</span>',$buffer);
}
You can change language name from wp-admin by edit language name directly..
Inspecting the plugin I found that generateLanguageSelectCode have more types than documented. So to use language codes you can simply just use the type 'short', like this:
qtranxf_generateLanguageSelectCode('short');
This might be a feature added since last answer.
Here is a overview of all the switcher types:
'text', 'image', 'both', 'short', 'css_only', 'custom', and 'dropdown'. I havn't looked into how the different types works, but you'll find them in qtranslate_widget.php in the plugin folder.
You could use widget for that
<?php the_widget('qTranslateXWidget', array('type' => 'custom', 'format' => '%c') );?>
(%c - Language 2-Letter Code)
It should be noted that if you would like to use dropdown type and 2-Letter Code - this won't work because format argument works only with 'custom' type. In this case I would go with Yehuda Tiram answer (especially if you have many languages and you don't know which languages your client will want to use).
More documentation here
A friend helped me with that and it's based on Ash Patel answer but in a cleaner way (IMHO) :
function my_qtranxf_generateLanguageSelectCode($style='', $id='') {
ob_start();
qtranxf_generateLanguageSelectCode($style, $id);
$o = ob_get_contents();
ob_end_clean();
return str_replace(array('English', 'Français'),array('EN', 'FR'), $o);
}
Why don't you just change the language name as per your needs?
It's possible in the language edit and does not affect anything.
I've done it using the following query and it is working fine for me.
<?php if (qtranxf_getLanguage() == 'ar') { ?>
<script>
jQuery(document).ready(function () {
var current_URL = jQuery(location).attr('href');
url = current_URL.replace('/ar/', '/en/')
jQuery('.languages-selection ul li a').attr('href', url)
});
</script>
<?php } elseif (qtranxf_getLanguage() == 'en') { ?>
<script>
jQuery(document).ready(function() {
var current_URL = jQuery(location).attr('href');
url = current_URL.replace('/en/', '/ar/')
jQuery('.languages-selection ul li a').attr('href', url)
});
</script>
<?php } ?>
https://qtranslatexteam.wordpress.com/faq/
For example, to show flag only in the top language menu item, enter #qtransLangSw?title=none, if in addition to this current language is not needed to be shown, enter #qtransLangSw?title=none&current=hidden, and so on.

How to change the number of comments per page for a single node in Drupal 7?

i would like to change the comments per page for only a single node. Let's say that the defaul number of comments per page for the content type "article" is 50 and i want change this to 10 only for the article with nid=171.
$node = menu_get_object('node',1); if($node && $node->nid = 171) {......}
Any hint? Thanks.
If you want to control the number of comments on individual pages, I would suggest installing Views module which allow for easy creation of any lists of things, including comments. With Views, you can create a block with comments for any node and use that instead of regular comments block.
Also, you can have several different blocks with different number of comments, and attach whatever block you want to whatever node. Using Context module would help too, it allows to assign blocks and other elements to pages with much more sophisticated controls than Drupal core block management.
Check out can this be helpful to you:
https://drupal.stackexchange.com/questions/7450/comment-per-page-settings
Since it's a form you have to alter it and change comments number from code.
i found a solution even if a bit rude. To disable the node's default comments i used a preprocess function in template.php file of my subtheme and disbled the $content['comments'] variable used in the comment-wrapper.tpl.php template:
function MYSUBTHEMENAME_preprocess_comment_wrapper(&$vars){
$node = menu_get_object();
if($node):
if($node->type === 'forum' && $node->nid == 171):
unset($vars['content']['comments']);
endif;
endif;
}
Note that i did it for comment wrapper variables/template and not for the node vfariables/template in order to unset the comments but to keep the comment form.
Then i created a view block of comments with a filter for the nid of the comment's node.
This works well for my purposes but if you find a more elegant way let me know. :)
I spent a time looking comment.module and found this in comment_form_node_type_form_alter
$form['comment']['comment_default_per_page'] = array(
'#type' => 'select',
'#title' => t('Comments per page'),
'#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
'#options' => _comment_per_page(),
);
So I put that in my template.php
function THEMENAME_preprocess_page(&$variables){
if (isset($variables['node']->type)) {
if ($variables['node']->type == 'forum') {
variable_set('comment_default_per_page_' . 'forum', 4);
}
}
}
It worked for me, I hope it helps.

Drupal 7 Views custom view template fields

I've successfully created a custom view template for my Drupal 7 site but am having issues adding attributes to the content which is outputted. I've searched high and low for the answer to this but to no avail.
I have a view called: views-view-fields--homepage-articles.tpl.php
I am printing content like :
$fields['title']->content
This is fine and expected, and outputs:
Title
But I want to add classes to it - how? I'm thinking I need to write a hook, but I cannot find this documented anywhere. At the moment my solution is a string replace:
<?php print str_replace('<a ', '<a class="brand-blue uppercase nodecoration"', $fields['title']->content); ?>
As you can imagine, this is not a satisfactory or long-term solution.
Many thanks!
You should be able to add the classes to the field using template_preprocess_views_view_fields().
Edit: Couldn't do it the way I thought, but you can overwrite the output of the field like so:
function MY_THEME_preprocess_views_view_fields(&$vars) {
$view = $vars['view'];
if ($view->name == 'node_listing') {
foreach ($vars['fields'] as $id => $field) {
if ($id == 'title') {
$field_output = l($view->result[$view->row_index]->node_title, 'node/'. $view->result[$view->row_index]->nid, array('attributes' => array('class' => 'brand-blue uppercase nodecoration')));
$vars['fields'][$id]->content = $field_output;
}
}
}
}
Have you tried using Semantic Views? https://drupal.org/project/semanticviews - that way you can override the classes within the UI instead of template files, may suit your needs better.

How to add warning text in Drupal comment form

I want a warning message displayed in the comment form when people try to add comments:
"Please write comments in correct
grammatical English, otherwise they
will not published"
How can I do it?
Here is how you can do it by using hook_form_alter in your own module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "comment_form":
$form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
break;
}
}
You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:
The following PHP code can be added to your theme's template.php file:
function YOURTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function YOURTHEME_comment_form($form) {
$output = '';
$output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
$output .= drupal_render($form);
return $output;
}
Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.
A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.
The above code is based on code from this page.
Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.
In Drupal 7 go to
admin/structure/types/manage/mycontenttype/comment/fields/comment_body
There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.

Get current page/url in a hook

In hook_css_alter, I want to disable style-sheets on specific pages.
Was ok for the front page requirement with following snippet:
function morin_css_alter(&$css) {
if(drupal_is_front_page()){
foreach ($css as $data => $item) {
unset($css[$data]);
}
}
}
Now I need to remove specific styles on a photo-album page. I have thought of a few ways to do this, but I'd like to do it the drupal way.
The closest function I have found to do this is drupal_get_destination() ,
I don't think it's meant for this but it does return the current path in an array, as shown by the following snippet added in css_alter hook.
echo " PATH = " . var_dump(drupal_get_destination());
Output: PATH = array(1) { ["destination"]=> string(6) "photos" }
Is this the recommended way of getting the path from inside a function/hook, or is there some variable in global namespace I should use instead?
You want http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/request_path/7.
For Drupal 6, one has to use $_GET['q'].

Resources