Drupal 7 Views custom view template fields - drupal

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.

Related

Extend link field output on Drupal Twig Template

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!

Customize drupal search result page

I have created a customized theme called "test", and I am trying to display the search results.
I added the function test_preprocess_search_results() to the template.php (copied the code from drupal page "function template_preprocess_search_results", then copy the search-result.tpl.php from search module to the "test"'s template folder.
function test_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
if (!empty($variables['module'])) {
$variables['module'] = check_plain($variables['module']);
}
foreach ($variables['results'] as $result) {
$variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
}
$variables['pager'] = theme('pager', array('tags' => NULL));
$variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}
I am new in drupal, my concern is how can I make the search result display in some certain div which claimed in my page.tpl.php file? Do something like <?php print render($page['search_result']); ?> in page.tpl.php div ? I am not sure how does the default theme know where to display the search result? Can anyone help thanks
ps: after what I did, and refresh the cache, nothing show up
For search results you should use the search-results.tpl.php (all results list) and search-result.tpl.php (single list item) templates. Copy them from ROOT/modules/search folder into your theme folder and override them as you prefer.

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.

Drupal hook_form_alter for Taxonomy admin

I created a module to do all my form altering called "form_mods". It's working for most situations but not for the Taxonomy page.
I'm targeting the form id of "taxonomy_overview_vocabularies". I'm trying to hide the link "edit vocabulary" for roles of "webmaster" and "dj".
My code is unsetting the $form array correctly, but Drupal is still displaying the "edit vocabulary" link.
function form_mods_form_alter($form, $form_state, $form_id) {
if($form_id == 'taxonomy_overview_vocabularies'){
global $user;
$hide=0;
$hideArray = array('webmaster', 'dj');
foreach($user->roles AS $key => $value){
if(in_array($value, $hideArray)){
$hide++;
}
}
if($hide){
foreach($form AS $vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
}
}
}
Very small PHP mistake,
when you want to change array members in a for each statement you have to pass them by reference & foreach($form AS &$vocab) otherwise the $vocab would be just a copy of the array
foreach($form AS &$vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
In addition to Amjad's answer, if you don't like using references, I would suggest another alternative:
foreach ($form as $key => $vocab) {
unset($form[$key]['edit']['#value']);
}
This way you avoid using references, and potential issues they may lead to.
Also note I removed the if statement, which is not useful (PHP can figure it out).
An array_map could also be considered.

Drupal tagging via tagadelic

this module does a good job at creating a tagcloud block - all good here. now id also like to have a page that lists all tags with next to each tag the number of posts that were tagged with this term. all terms are listed ok on http://example.com/?q=tagadelic/list/3 but i dont think tagadelic can add the number of posts per tag?
also, it seems tagadelic can just output one single block "tags in tags". whatever changes i make in the tagadelic configuration is applied to the tagadelic/list/3 url AND to the tagcloud block in the sidebar (the order of tags and number of tag levels)
does what i need require some custom module or are there others around that can achieve this? ive been playing around with Views 2 but cant quite get what I need
Use views and views_cloud for a much more flexible solution.
Edit: If you are having trouble with the views module, there is some very good in-browser instructions that come with it, but they require the advanced_help module.
For historical information:
Tagadelic can add the number of posts per tag, just fine. Assuming your theme is called "red":
/**
* theme function that renders the HTML for the tags
* #ingroup themable
*/
function red_tagadelic_weighted($terms) {
$output = '';
foreach ($terms as $term) {
$output .= l($term->name, taxonomy_term_path($term), array('attributes' => array('class' => "tagadelic level$term->weight", 'rel' => 'tag'))) .' ('. $term->count .') ';
}
return $output;
}

Resources